canvas学习笔记:小小滴公式,大大滴乐趣

最近想弄一个网页,把自己学HTML5过程中做的部分DEMO放上去做集合,但是,如果就仅仅做个网页把所有DEMO一个一个排列又觉得太难看了。就想,既然学了canvas,那就来折腾下浏览器,做个小小的开场动画吧。

成都创新互联公司专注于企业全网营销推广、网站重做改版、陆河网站定制设计、自适应品牌网站建设、H5页面制作商城建设、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为陆河等各大城市提供网站开发制作服务。

开场动画的效果,想了一会,决定用粒子,因为觉得粒子比较好玩。还记得以前我写的***篇技术博文,就是讲文字图片粒子化的:文字图片粒子化 , 那时就仅仅做的是直线运动,顺便加了一点3D效果。运动公式很简单。所以就想这个开场动画就做的更动感一些吧。

先上DEMO:http://2.axescanvas.sinaapp.com/demoHome/index.html

效果是不是比直线的运动更加动感呢?而且也确实很简单,别忘了这篇博文的题目,小小滴公式,大大滴乐趣。要做出这样的效果,用的就仅仅是我们初中。。或者高中时候的物理知识,加速运动,减速运动的公式啦。所以确实是小小滴公式。楼主很喜欢折腾一些酷炫的东西,虽然可能平时工作上用不上,但是,这乐趣确实很让人着迷啊。而且,做下这些也可以加强一下编程的思维能力哈。

废话不多说,进入主题啦。就简单的解释一下原理吧~~~

粒子运动的核心代码就这么一点:

 
 
  1. update:function(time){
  2.             this.x += this.vx*time;
  3.             this.y += this.vy*time;
  4.             if(!this.globleDown&&this.y>0){
  5.                 var yc = this.toy - this.y;
  6.                 var xc = this.tox - this.x;
  7.                 this.jl = Math.sqrt(xc*xc+yc*yc);
  8.                 var za = 20;
  9.                 var ax = za*(xc/this.jl),
  10.                     ay = za*(yc/this.jl),
  11.                     vx = (this.vx+ax*time)*0.97,
  12.                     vy = (this.vy+ay*time)*0.97;
  13.                 this.vx = vx;
  14.                 this.vy = vy;
  15.             }else {
  16.                 var gravity = 9.8;
  17.                 var vy = this.vy+gravity*time;
  18.                 if(this.y>canvas.height){
  19.                     vy = -vy*0.7;
  20.                 }
  21.                 this.vy = vy;
  22.             }
  23.         },

粒子总共有两种状态,一种是自由落体,一种就是受到吸力。自由落体就不说了。说吸力之前先贴出粒子的属性:

 
 
  1. var Dot = function(x,y,vx,vy,tox,toy,color){
  2.         this.x=x;
  3.         this.y=y;
  4.         this.vx=vx;
  5.         this.vy=vy;
  6.         this.nextox = tox;
  7.         this.nextoy = toy;
  8.         this.color = color;
  9.         this.visible = true;
  10.         this.globleDown = false;
  11.         this.setEnd(tox , toy);
  12.     }
  13. setEnd:function(tox , toy){
  14.             this.tox = tox;
  15.             this.toy = toy;
  16.             var yc = this.toy - this.y;
  17.             var xc = this.tox - this.x;
  18.         },

x,y就是粒子的位置,vx是粒子水平速度,vy是粒子的垂直速度,nexttox之类知不知道都无所谓,只是暂时保存变量的。tox,和toy就是粒子的目的地位置。

首先,先给予所有粒子一个目的地,这个目的地下面再会说。也就是要粒子到达的地方,然后再定义一个变量za作为加速度,具体数值的话,就自己多测试下就会有大概参数的了,我设成20,感觉就差不多了。za是粒子和目的地之间连线的加速度,所以,我们通过粒子的位置和目的地的位置,通过简单的三角函数,就可以把粒子的水平加速度和垂直加速度求出来了,就这段

 
 
  1. var ax = za*(xc/this.jl),
  2.  ay = za*(yc/this.jl),

有了水平加速度和垂直加速度后,接下来就更简单了,直接计算水平速度和垂直速度的增量,从而改变水平速度和垂直速度的值

 
 
  1. vx = (this.vx+ax*time)*0.97,
  2. vy = (this.vy+ay*time)*0.97;

之所以要乘于0.97是为了模拟能量损耗,粒子才会减速。time是每一帧的时间差

计算出速度后就更新粒子位置就行了。

 
 
  1. this.x += this.vx*time;
  2. this.y += this.vy*time;

因为粒子在飞行过程中,与目的地之间的连线方向是不停改变的,所以每一帧都要重新计算粒子的水平加速度和垂直加速度。

运动原理就是如此,是否很简单呢。

运动原理说完了,再扯一下上面那个动画的具体实现吧:动画初始化,在一个离屏canvas上把想要的字或者图片画出来,然后再通过getImageData这个方法获取离屏canvas的像素。然后用一个循环,把离屏canvas中有绘制的区域找出来,因为imageData里的data值就是一个rgba数组,所以我们判断***一个的值也就是透明度大于128就是有绘制过的区域。然后获取该区域的xy值,为了防止粒子对象过多导致页面卡顿,所以我们就限制一下粒子的数量,取像素的时候x值和y值每次递增2,从而减少粒子数量。

 
 
  1. this.osCanvas = document.createElement("canvas");
  2.         var osCtx = this.osCanvas.getContext("2d");
  3.         this.osCanvas.width = 1000;
  4.         this.osCanvas.height = 150;
  5.         osCtx.textAlign = "center";
  6.         osCtx.textBaseline = "middle";
  7.         osCtx.font="70px 微软雅黑,黑体 bold";
  8.         osCtx.fillStyle = "#1D181F"
  9.         osCtx.fillText("WelCome" , this.osCanvas.width/2 , this.osCanvas.height/2-40);
  10.         osCtx.fillText("To wAxes' HOME" , this.osCanvas.width/2 , this.osCanvas.height/2+40);
  11.         var bigImageData = osCtx.getImageData(0,0,this.osCanvas.width,this.osCanvas.height);
  12.         dots = [];
  13.         for(var x=0;x
  14.             for(var y=0;y
  15.                 var i = (y*bigImageData.width + x)*4;
  16.                 if(bigImageData.data[i+3]>128){
  17.                     var dot = new Dot(
  18.                         Math.random()>0.5?Math.random()*20+10:Math.random()*20+canvas.width-40,
  19.                         -Math.random()*canvas.height*2,
  20.                         0,
  21.                         0,
  22.                         x+(canvas.width/2-this.osCanvas.width/2),
  23.                         y+(canvas.height/2-this.osCanvas.height/2),
  24.                         "rgba("+bigImageData.data[i]+","+bigImageData.data[i+1]+","+bigImageData.data[i+2]+",1)"
  25.                     );
  26.                     dot.setEnd(canvas.width/2,canvas.height/2)
  27.                     dots.push(dot);
  28.                 }
  29.             }
  30.         }

通过循环获取到粒子的位置xy值后,把位置赋给粒子,成为粒子的目的地。然后动画开始,就可以做出文字图片粒子化的效果了。

#p#

下面贴出动画实现的js代码。如果对其他代码也有兴趣的,可以直接看控制台哈,没压缩的。

 
 
  1. var part_1 = (function(w){
  2.     var dots = [],DOT_SIZE = 2,cube=null;
  3.     var Dot = function(x,y,vx,vy,tox,toy,color){
  4.         this.x=x;
  5.         this.y=y;
  6.         this.vx=vx;
  7.         this.vy=vy;
  8.         this.nextox = tox;
  9.         this.nextoy = toy;
  10.         this.color = color;
  11.         this.visible = true;
  12.         this.globleDown = false;
  13.         this.setEnd(tox , toy);
  14.     }
  15.     Dot.prototype = {
  16.         paint:function(){
  17.             ctx.fillStyle=this.color;
  18.             ctx.fillRect(this.x-DOT_SIZE/2 , this.y-DOT_SIZE/2 , DOT_SIZE , DOT_SIZE);
  19.         },
  20.         setEnd:function(tox , toy){
  21.             this.tox = tox;
  22.             this.toy = toy;
  23.             var yc = this.toy - this.y;
  24.             var xc = this.tox - this.x;
  25.             // this.initjl = Math.sqrt(xc*xc+yc*yc);
  26.         },
  27.         update:function(time){
  28.             this.x += this.vx*time;
  29.             this.y += this.vy*time;
  30.             if(!this.globleDown&&this.y>0){
  31.                 var yc = this.toy - this.y;
  32.                 var xc = this.tox - this.x;
  33.                 this.jl = Math.sqrt(xc*xc+yc*yc);
  34.                 var za = 20;
  35.                 var ax = za*(xc/this.jl),
  36.                     ay = za*(yc/this.jl),
  37.                     vx = (this.vx+ax*time)*0.97,
  38.                     vy = (this.vy+ay*time)*0.97;
  39.                 this.vx = vx;
  40.                 this.vy = vy;
  41.                 // if(Math.abs(this.vx)<1&&Math.abs(this.vy)<1){
  42.                 //     this.y = this.toy
  43.                 //     this.x = this.tox
  44.                 // }
  45.             }else {
  46.                 var gravity = 9.8;
  47.                 var vy = this.vy+gravity*time;
  48.                 if(this.y>canvas.height){
  49.                     vy = -vy*0.7;
  50.                 }
  51.                 this.vy = vy;
  52.             }
  53.         },
  54.         loop:function(time){
  55.             this.update(time);
  56.             this.paint();
  57.         }
  58.     }
  59.     
  60.     var animate = function(){
  61.         this.state = "before"
  62.     }
  63.     var ap = animate.prototype;
  64.     ap.init = function(){
  65.         this.osCanvas = document.createElement("canvas");
  66.         var osCtx = this.osCanvas.getContext("2d");
  67.         this.osCanvas.width = 1000;
  68.         this.osCanvas.height = 150;
  69.         osCtx.textAlign = "center";
  70.         osCtx.textBaseline = "middle";
  71.         osCtx.font="70px 微软雅黑,黑体 bold";
  72.         osCtx.fillStyle = "#1D181F"
  73.         osCtx.fillText("WelCome" , this.osCanvas.width/2 , this.osCanvas.height/2-40);
  74.         osCtx.fillText("To wAxes' HOME" , this.osCanvas.width/2 , this.osCanvas.height/2+40);
  75.         var bigImageData = osCtx.getImageData(0,0,this.osCanvas.width,this.osCanvas.height);
  76.         dots = [];
  77.         for(var x=0;x
  78.             for(var y=0;y
  79.                 var i = (y*bigImageData.width + x)*4;
  80.                 if(bigImageData.data[i+3]>128){
  81.                     var dot = new Dot(
  82.                         Math.random()>0.5?Math.random()*20+10:Math.random()*20+canvas.width-40,
  83.                         -Math.random()*canvas.height*2,
  84.                         0,
  85.                         0,
  86.                         x+(canvas.width/2-this.osCanvas.width/2),
  87.                         y+(canvas.height/2-this.osCanvas.height/2),
  88.                         "rgba("+bigImageData.data[i]+","+bigImageData.data[i+1]+","+bigImageData.data[i+2]+",1)"
  89.                     );
  90.                     dot.setEnd(canvas.width/2,canvas.height/2)
  91.                     dots.push(dot);
  92.                 }
  93.             }
  94.         }
  95.         console.log(dots.length)
  96.     }
  97.     ap.changeState = function(){
  98.         var osCtx = this.osCanvas.getContext("2d");
  99.         osCtx.clearRect(0,0,this.osCanvas.width,this.osCanvas.height);
  100.         this.osCanvas.width = 460;
  101.         this.osCanvas.height = 100;
  102.         osCtx.fillStyle="#5C5656"
  103.         osCtx.fillRect(20,20,60,60)
  104.         drawLogo(this.osCanvas , osCtx);
  105.         var bigImageData = osCtx.getImageData(0,0,this.osCanvas.width,this.osCanvas.height);
  106.         var index=0;
  107.         dots.sort(function(a , b){
  108.             return Math.random()-Math.random();
  109.         })
  110.         for(var x=0;x
  111.             for(var y=0;y
  112.                 var i = (y*bigImageData.width + x)*4;
  113.                 if(bigImageData.data[i+3]>128){
  114.                         var d = dots[index];
  115.                         if(d){
  116.                             d.setEnd(x+(canvas.width/2-300) , y+50)
  117.                             d.color = "rgba("+bigImageData.data[i]+","+bigImageData.data[i+1]+","+bigImageData.data[i+2]+",1)";
  118.                             index++
  119.                         }
  120.                 }
  121.             }
  122.         }
  123.         setTimeout(function(){
  124.             var endindex = index;
  125.             for(var i=0;i
  126.                 if(dots[index]){
  127.                     var d = dots[index];
  128.                     
  129.                     d.globleDown = true;
  130.                     d.vx = Math.random()*100-50;
  131.                 }
  132.                 index++;
  133.             }
  134.         } , 2000)
  135.     }
  136.     function endState(){
  137.         canvas.width = 600;
  138.         canvas.height = 100;
  139.         canvas.style.display="block";
  140.         canvas.style.top = "50px";
  141.         canvas.style.left = (window.innerWidth-canvas.width)/2+"px";
  142.         cube = new Cube(50);
  143.         cube._initVector(50,50);
  144.     }
  145.     function drawLogo(canvas , ctx){
  146.         ctx.textAlign = "center";
  147.         ctx.textBaseline = "middle";
  148.         ctx.font="65px 微软雅黑,黑体 bold"
  149.         ctx.fillStyle="#E06D2F"
  150.         ctx.fillText("DEMO" , 300 , canvas.height/2)
  151.         ctx.font="40px 微软雅黑,黑体 bold"
  152.         ctx.fillStyle="#405159"
  153.         ctx.fillText("吖猩的" , 160 , canvas.height/2)
  154.         ctx.fillText("小窝" , 420 , canvas.height/2)
  155.     }
  156.     var num = 0;
  157.     ap.update = function(time){
  158.         time = time/100;
  159.         if(this.state==="first"||this.state==="before"){
  160.             var completeNum = 0;
  161.             dots.forEach(function(dot){
  162.                 if(dot.visible) dot.loop(time);
  163.                 if(dot.jl<5){
  164.                     completeNum++
  165.                 }
  166.             });
  167.             if(completeNum>=5*dots.length/6){
  168.                 
  169.                 if(this.state==="before"){
  170.                     this.state = "first";
  171.                     dots.forEach(function(dot){
  172.                         dot.setEnd(dot.nextox , dot.nextoy);
  173.                     });
  174.                 }else {
  175.                     this.state = "second";
  176.                     this.changeState();
  177.                 }
  178.             }
  179.         }else if(this.state==="second"){
  180.             var completeNum = 0,
  181.                 allnum = 0;
  182.             dots.forEach(function(dot){
  183.                 if(dot.visible) dot.loop(time);
  184.                 if(dot.globleDown){
  185.                     allnum++;
  186.                     if(Math.abs(dot.y-canvas.height)<2){
  187.                         completeNum++
  188.                     }
  189.                 }
  190.             });
  191.             if(completeNum===allnum&&allnum!==0){
  192.                 this.state = "third";
  193.                 part_2.animate();
  194.                 endState();
  195.             }
  196.         }else if(this.state==="third"){
  197.             cube.update();
  198.             drawLogo(canvas , ctx);
  199.         }
  200.     }
  201.     return new animate();
  202. })(window)

当前文章:canvas学习笔记:小小滴公式,大大滴乐趣
标题来源:http://www.hantingmc.com/qtweb/news28/491328.html

成都网站建设公司_创新互联,为您提供做网站网站制作标签优化Google网站内链静态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联