动态背景-粒子模型 - 图1
最终效果如上图所示

项目需要做一个动态背景。最开始采用轮播背景图的方式进行实现个人觉得没啥意义,二版采用js动态生成不规则图形美工觉得不好看,三版采用生成运动的物体也是最终版的前生。

使用canvas进行实现,引用jquery外部链接。

使用css,主要就定义一下canvas的背景色

  1. *{margin: 0;padding:0;}
  2. html,body{height:100%;width:100%;}
  3. canvas{display: block;}
  4. #myCanvas{background:#001022;}
  5. #myButtons{bottom:20px;left:20px;position:absolute;}
  6. #myButtons button{padding:15px;}

html代码部分,div模块的内容主要用于调试,在使用时不用包含该模块。直接用canvas就可以,注意id需要与js中保持一致,canvas的宽度与高度在js的resize方法中会重写。

  1. <canvas id="myCanvas" width="500" height="500"></canvas>
  2. <!--控制模块-->
  3. <div id="myButtons">
  4. <button id="refreshBtn">刷新</button>
  5. <button id="acceleBtn">加速</button>
  6. <button id="startAnimation">Start</button>
  7. <button id="stopAnimation">Stop</button>
  8. </div>

下面是控制模块js代码,这段代码不是主要的你可以忽略不计。

  1. /*控制模块*/
  2. $("#refreshBtn").click(function(event) {myGetAsteroid()});//刷新
  3. $("#acceleBtn").click(function(event) {animate()});//加速
  4. var playAnimation = true;
  5. var startButton = $('#startAnimation');
  6. var stopButton = $('#stopAnimation');
  7. startButton.hide();
  8. startButton.click(function() {
  9. $(this).hide();
  10. stopButton.show();
  11. playAnimation=true;
  12. animate();
  13. });
  14. stopButton.click(function(){
  15. $(this).hide();
  16. startButton.show();
  17. playAnimation=false;
  18. });

定义canvas与宽高度,当窗口大小发生变化是需要更新画布大小以免物体元素移动到边界之外。

  1. var canvas=$('#myCanvas');
  2. var context=canvas.get(0).getContext("2d");
  3. var canvasWidth = canvas.width();
  4. var canvasHeight= canvas.height();
  5. /*更新画布大小*/
  6. $(window).resize(resizeCanvas);
  7. function resizeCanvas(){
  8. canvas.attr("width",$(window).get(0).innerWidth);
  9. canvas.attr("height",$(window).get(0).innerHeight);
  10. canvasWidth=canvas.width();
  11. canvasHeight=canvas.height();
  12. }

定义物体属性

  1. var Asteroid = function(x,y,radius,iscolor,vx,vy,ax,ay){
  2. this.x=x;
  3. this.y=y;
  4. this.radius=radius;//大小
  5. this.iscolor=iscolor;//颜色
  6. this.vx=vx;//x轴速度
  7. this.vy=vy;//y轴速度
  8. this.ax=ax;//x轴加速度
  9. this.ay=ay;//y轴加速度
  10. }

创建物体实体

  1. var asteroids = new Array();
  2. var getAsteroid = function() {
  3. for (var i = 0; i < 100; i++) {//循环需要创个的物体数量
  4. var x=20+(Math.random()*(canvasWidth-40));
  5. var y=20+(Math.random()*(canvasHeight-40));
  6. var radius=2+Math.random()*10;
  7. var iscolor='rgba('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+',0.54)';
  8. var vx=Math.random()*4-2;
  9. var vy=Math.random()*4-2;
  10. var ax=Math.random()*0.2-0.1;
  11. var ay=Math.random()*0.2-0.1;
  12. asteroids.push(new Asteroid(x,y,radius,iscolor,vx,vy,ax,ay));
  13. };
  14. }

重构物体实体,此方法只为控制模块服务,在使用时可抛弃该方法。

  1. var myGetAsteroid = function () {
  2. for (var i = 0; i < asteroids.length; i++) {
  3. asteroids[i].x=20+(Math.random()*(canvasWidth-40));
  4. asteroids[i].y=20+(Math.random()*(canvasHeight-40));
  5. asteroids[i].radius=2+Math.random()*10;
  6. asteroids[i].iscolor='rgba('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+',0.54)';
  7. asteroids[i].vx=Math.random()*4-2;
  8. asteroids[i].vy=Math.random()*4-2;
  9. asteroids[i].ax=Math.random()*0.2-0.1;
  10. asteroids[i].ay=Math.random()*0.2-0.1;
  11. }
  12. }

更新画布

  1. function animate(){
  2. context.clearRect(0,0,canvasWidth,canvasHeight);
  3. var asteroidsLength = asteroids.length;
  4. for (var i = 0; i < asteroidsLength; i++) {
  5. var tmpAsteroid = asteroids[i];
  6. /*设置边界,防止物体运行到画布之外*/
  7. if(tmpAsteroid.x - tmpAsteroid.radius < 0){
  8. tmpAsteroid.x = tmpAsteroid.radius;
  9. tmpAsteroid.vx *=-1;
  10. tmpAsteroid.ax *=-1;
  11. }else if(tmpAsteroid.x + tmpAsteroid.radius > canvasWidth){
  12. tmpAsteroid.x = canvasWidth - tmpAsteroid.radius;
  13. tmpAsteroid.vx *=-1;
  14. tmpAsteroid.ax *=-1;
  15. }
  16. if(tmpAsteroid.y - tmpAsteroid.radius < 0){
  17. tmpAsteroid.y = tmpAsteroid.radius;
  18. tmpAsteroid.vy *=-1;
  19. tmpAsteroid.ay *=-1;
  20. }else if(tmpAsteroid.y + tmpAsteroid.radius > canvasHeight){
  21. tmpAsteroid.y = canvasHeight - tmpAsteroid.radius;
  22. tmpAsteroid.vy *=-1;
  23. tmpAsteroid.ay *=-1;
  24. }
  25. /*限制物体最大移动速度*/
  26. if(Math.abs(tmpAsteroid.vx)<10){
  27. tmpAsteroid.vx+=tmpAsteroid.ax
  28. }
  29. if(Math.abs(tmpAsteroid.vy)<10){
  30. tmpAsteroid.vy+=tmpAsteroid.ay
  31. }
  32. /*摩擦力*/
  33. if(Math.abs(tmpAsteroid.vx)>0.1){
  34. tmpAsteroid.vx*=0.9;
  35. }else{
  36. tmpAsteroid.vx*=0.1;
  37. }
  38. if(Math.abs(tmpAsteroid.vy)>0.1){
  39. tmpAsteroid.vy*=0.9;
  40. }else{
  41. tmpAsteroid.vy*=0.1;
  42. }
  43. context.beginPath();
  44. for (var j = asteroidsLength-1; j > 0; j--) {
  45. var p2 = asteroids[j];
  46. var a = tmpAsteroid.x - p2.x
  47. var b = tmpAsteroid.y - p2.y
  48. var dist = Math.sqrt((a * a) + (b * b)).toFixed(2);
  49. if (dist < 100) {
  50. context.moveTo(tmpAsteroid.x + tmpAsteroid.vx, tmpAsteroid.y + tmpAsteroid.vy);
  51. context.lineTo(p2.x + p2.vx, p2.y + p2.vy);
  52. }
  53. }
  54. context.stroke();
  55. context.strokeStyle = 'rgba(204,204,204,0.5)';
  56. context.closePath();
  57. var tx = tmpAsteroid.x+=tmpAsteroid.vx;
  58. var ty = tmpAsteroid.y+=tmpAsteroid.vy;
  59. var tr = tmpAsteroid.radius;
  60. context.beginPath();
  61. context.fillStyle = tmpAsteroid.iscolor;
  62. context.arc(tx,ty,tr,0,Math.PI*2,false);
  63. context.closePath();
  64. context.fill();
  65. }
  66. if(playAnimation){
  67. requestAnimationFrame(animate);
  68. }
  69. }

代码文件
http://download.csdn.net/detail/u012793146/9483578