日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
canvas學(xué)習(xí)筆記:小小滴公式,大大滴樂(lè)趣

最近想弄一個(gè)網(wǎng)頁(yè),把自己學(xué)HTML5過(guò)程中做的部分DEMO放上去做集合,但是,如果就僅僅做個(gè)網(wǎng)頁(yè)把所有DEMO一個(gè)一個(gè)排列又覺(jué)得太難看了。就想,既然學(xué)了canvas,那就來(lái)折騰下瀏覽器,做個(gè)小小的開(kāi)場(chǎng)動(dòng)畫(huà)吧。

成都創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、陸河網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁(yè)面制作、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為陸河等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

開(kāi)場(chǎng)動(dòng)畫(huà)的效果,想了一會(huì),決定用粒子,因?yàn)橛X(jué)得粒子比較好玩。還記得以前我寫(xiě)的***篇技術(shù)博文,就是講文字圖片粒子化的:文字圖片粒子化 , 那時(shí)就僅僅做的是直線(xiàn)運(yùn)動(dòng),順便加了一點(diǎn)3D效果。運(yùn)動(dòng)公式很簡(jiǎn)單。所以就想這個(gè)開(kāi)場(chǎng)動(dòng)畫(huà)就做的更動(dòng)感一些吧。

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

效果是不是比直線(xiàn)的運(yùn)動(dòng)更加動(dòng)感呢?而且也確實(shí)很簡(jiǎn)單,別忘了這篇博文的題目,小小滴公式,大大滴樂(lè)趣。要做出這樣的效果,用的就僅僅是我們初中。。或者高中時(shí)候的物理知識(shí),加速運(yùn)動(dòng),減速運(yùn)動(dòng)的公式啦。所以確實(shí)是小小滴公式。樓主很喜歡折騰一些酷炫的東西,雖然可能平時(shí)工作上用不上,但是,這樂(lè)趣確實(shí)很讓人著迷啊。而且,做下這些也可以加強(qiáng)一下編程的思維能力哈。

廢話(huà)不多說(shuō),進(jìn)入主題啦。就簡(jiǎn)單的解釋一下原理吧~~~

粒子運(yùn)動(dòng)的核心代碼就這么一點(diǎn):

 
 
  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.         },

粒子總共有兩種狀態(tài),一種是自由落體,一種就是受到吸力。自由落體就不說(shuō)了。說(shuō)吸力之前先貼出粒子的屬性:

 
 
  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之類(lèi)知不知道都無(wú)所謂,只是暫時(shí)保存變量的。tox,和toy就是粒子的目的地位置。

首先,先給予所有粒子一個(gè)目的地,這個(gè)目的地下面再會(huì)說(shuō)。也就是要粒子到達(dá)的地方,然后再定義一個(gè)變量za作為加速度,具體數(shù)值的話(huà),就自己多測(cè)試下就會(huì)有大概參數(shù)的了,我設(shè)成20,感覺(jué)就差不多了。za是粒子和目的地之間連線(xiàn)的加速度,所以,我們通過(guò)粒子的位置和目的地的位置,通過(guò)簡(jiǎn)單的三角函數(shù),就可以把粒子的水平加速度和垂直加速度求出來(lái)了,就這段

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

有了水平加速度和垂直加速度后,接下來(lái)就更簡(jiǎn)單了,直接計(jì)算水平速度和垂直速度的增量,從而改變水平速度和垂直速度的值

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

之所以要乘于0.97是為了模擬能量損耗,粒子才會(huì)減速。time是每一幀的時(shí)間差

計(jì)算出速度后就更新粒子位置就行了。

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

因?yàn)榱W釉陲w行過(guò)程中,與目的地之間的連線(xiàn)方向是不停改變的,所以每一幀都要重新計(jì)算粒子的水平加速度和垂直加速度。

運(yùn)動(dòng)原理就是如此,是否很簡(jiǎn)單呢。

運(yùn)動(dòng)原理說(shuō)完了,再扯一下上面那個(gè)動(dòng)畫(huà)的具體實(shí)現(xiàn)吧:動(dòng)畫(huà)初始化,在一個(gè)離屏canvas上把想要的字或者圖片畫(huà)出來(lái),然后再通過(guò)getImageData這個(gè)方法獲取離屏canvas的像素。然后用一個(gè)循環(huán),把離屏canvas中有繪制的區(qū)域找出來(lái),因?yàn)閕mageData里的data值就是一個(gè)rgba數(shù)組,所以我們判斷***一個(gè)的值也就是透明度大于128就是有繪制過(guò)的區(qū)域。然后獲取該區(qū)域的xy值,為了防止粒子對(duì)象過(guò)多導(dǎo)致頁(yè)面卡頓,所以我們就限制一下粒子的數(shù)量,取像素的時(shí)候x值和y值每次遞增2,從而減少粒子數(shù)量。

 
 
  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.         }

通過(guò)循環(huán)獲取到粒子的位置xy值后,把位置賦給粒子,成為粒子的目的地。然后動(dòng)畫(huà)開(kāi)始,就可以做出文字圖片粒子化的效果了。

#p#

下面貼出動(dòng)畫(huà)實(shí)現(xiàn)的js代碼。如果對(duì)其他代碼也有興趣的,可以直接看控制臺(tái)哈,沒(méi)壓縮的。

 
 
  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)

標(biāo)題名稱(chēng):canvas學(xué)習(xí)筆記:小小滴公式,大大滴樂(lè)趣
網(wǎng)頁(yè)URL:http://m.5511xx.com/article/cojjccg.html