新聞中心
最近想弄一個(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):
- update:function(time){
- this.x += this.vx*time;
- this.y += this.vy*time;
- if(!this.globleDown&&this.y>0){
- var yc = this.toy - this.y;
- var xc = this.tox - this.x;
- this.jl = Math.sqrt(xc*xc+yc*yc);
- var za = 20;
- var ax = za*(xc/this.jl),
- ay = za*(yc/this.jl),
- vx = (this.vx+ax*time)*0.97,
- vy = (this.vy+ay*time)*0.97;
- this.vx = vx;
- this.vy = vy;
- }else {
- var gravity = 9.8;
- var vy = this.vy+gravity*time;
- if(this.y>canvas.height){
- vy = -vy*0.7;
- }
- this.vy = vy;
- }
- },
粒子總共有兩種狀態(tài),一種是自由落體,一種就是受到吸力。自由落體就不說(shuō)了。說(shuō)吸力之前先貼出粒子的屬性:
- var Dot = function(x,y,vx,vy,tox,toy,color){
- this.x=x;
- this.y=y;
- this.vx=vx;
- this.vy=vy;
- this.nextox = tox;
- this.nextoy = toy;
- this.color = color;
- this.visible = true;
- this.globleDown = false;
- this.setEnd(tox , toy);
- }
- setEnd:function(tox , toy){
- this.tox = tox;
- this.toy = toy;
- var yc = this.toy - this.y;
- var xc = this.tox - this.x;
- },
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)了,就這段
- var ax = za*(xc/this.jl),
- ay = za*(yc/this.jl),
有了水平加速度和垂直加速度后,接下來(lái)就更簡(jiǎn)單了,直接計(jì)算水平速度和垂直速度的增量,從而改變水平速度和垂直速度的值
- vx = (this.vx+ax*time)*0.97,
- vy = (this.vy+ay*time)*0.97;
之所以要乘于0.97是為了模擬能量損耗,粒子才會(huì)減速。time是每一幀的時(shí)間差
計(jì)算出速度后就更新粒子位置就行了。
- this.x += this.vx*time;
- 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ù)量。
- this.osCanvas = document.createElement("canvas");
- var osCtx = this.osCanvas.getContext("2d");
- this.osCanvas.width = 1000;
- this.osCanvas.height = 150;
- osCtx.textAlign = "center";
- osCtx.textBaseline = "middle";
- osCtx.font="70px 微軟雅黑,黑體 bold";
- osCtx.fillStyle = "#1D181F"
- osCtx.fillText("WelCome" , this.osCanvas.width/2 , this.osCanvas.height/2-40);
- osCtx.fillText("To wAxes' HOME" , this.osCanvas.width/2 , this.osCanvas.height/2+40);
- var bigImageData = osCtx.getImageData(0,0,this.osCanvas.width,this.osCanvas.height);
- dots = [];
- for(var x=0;x
- for(var y=0;y
- var i = (y*bigImageData.width + x)*4;
- if(bigImageData.data[i+3]>128){
- var dot = new Dot(
- Math.random()>0.5?Math.random()*20+10:Math.random()*20+canvas.width-40,
- -Math.random()*canvas.height*2,
- 0,
- 0,
- x+(canvas.width/2-this.osCanvas.width/2),
- y+(canvas.height/2-this.osCanvas.height/2),
- "rgba("+bigImageData.data[i]+","+bigImageData.data[i+1]+","+bigImageData.data[i+2]+",1)"
- );
- dot.setEnd(canvas.width/2,canvas.height/2)
- dots.push(dot);
- }
- }
- }
通過(guò)循環(huán)獲取到粒子的位置xy值后,把位置賦給粒子,成為粒子的目的地。然后動(dòng)畫(huà)開(kāi)始,就可以做出文字圖片粒子化的效果了。
#p#
下面貼出動(dòng)畫(huà)實(shí)現(xiàn)的js代碼。如果對(duì)其他代碼也有興趣的,可以直接看控制臺(tái)哈,沒(méi)壓縮的。
- var part_1 = (function(w){
- var dots = [],DOT_SIZE = 2,cube=null;
- var Dot = function(x,y,vx,vy,tox,toy,color){
- this.x=x;
- this.y=y;
- this.vx=vx;
- this.vy=vy;
- this.nextox = tox;
- this.nextoy = toy;
- this.color = color;
- this.visible = true;
- this.globleDown = false;
- this.setEnd(tox , toy);
- }
- Dot.prototype = {
- paint:function(){
- ctx.fillStyle=this.color;
- ctx.fillRect(this.x-DOT_SIZE/2 , this.y-DOT_SIZE/2 , DOT_SIZE , DOT_SIZE);
- },
- setEnd:function(tox , toy){
- this.tox = tox;
- this.toy = toy;
- var yc = this.toy - this.y;
- var xc = this.tox - this.x;
- // this.initjl = Math.sqrt(xc*xc+yc*yc);
- },
- update:function(time){
- this.x += this.vx*time;
- this.y += this.vy*time;
- if(!this.globleDown&&this.y>0){
- var yc = this.toy - this.y;
- var xc = this.tox - this.x;
- this.jl = Math.sqrt(xc*xc+yc*yc);
- var za = 20;
- var ax = za*(xc/this.jl),
- ay = za*(yc/this.jl),
- vx = (this.vx+ax*time)*0.97,
- vy = (this.vy+ay*time)*0.97;
- this.vx = vx;
- this.vy = vy;
- // if(Math.abs(this.vx)<1&&Math.abs(this.vy)<1){
- // this.y = this.toy
- // this.x = this.tox
- // }
- }else {
- var gravity = 9.8;
- var vy = this.vy+gravity*time;
- if(this.y>canvas.height){
- vy = -vy*0.7;
- }
- this.vy = vy;
- }
- },
- loop:function(time){
- this.update(time);
- this.paint();
- }
- }
- var animate = function(){
- this.state = "before"
- }
- var ap = animate.prototype;
- ap.init = function(){
- this.osCanvas = document.createElement("canvas");
- var osCtx = this.osCanvas.getContext("2d");
- this.osCanvas.width = 1000;
- this.osCanvas.height = 150;
- osCtx.textAlign = "center";
- osCtx.textBaseline = "middle";
- osCtx.font="70px 微軟雅黑,黑體 bold";
- osCtx.fillStyle = "#1D181F"
- osCtx.fillText("WelCome" , this.osCanvas.width/2 , this.osCanvas.height/2-40);
- osCtx.fillText("To wAxes' HOME" , this.osCanvas.width/2 , this.osCanvas.height/2+40);
- var bigImageData = osCtx.getImageData(0,0,this.osCanvas.width,this.osCanvas.height);
- dots = [];
- for(var x=0;x
- for(var y=0;y
- var i = (y*bigImageData.width + x)*4;
- if(bigImageData.data[i+3]>128){
- var dot = new Dot(
- Math.random()>0.5?Math.random()*20+10:Math.random()*20+canvas.width-40,
- -Math.random()*canvas.height*2,
- 0,
- 0,
- x+(canvas.width/2-this.osCanvas.width/2),
- y+(canvas.height/2-this.osCanvas.height/2),
- "rgba("+bigImageData.data[i]+","+bigImageData.data[i+1]+","+bigImageData.data[i+2]+",1)"
- );
- dot.setEnd(canvas.width/2,canvas.height/2)
- dots.push(dot);
- }
- }
- }
- console.log(dots.length)
- }
- ap.changeState = function(){
- var osCtx = this.osCanvas.getContext("2d");
- osCtx.clearRect(0,0,this.osCanvas.width,this.osCanvas.height);
- this.osCanvas.width = 460;
- this.osCanvas.height = 100;
- osCtx.fillStyle="#5C5656"
- osCtx.fillRect(20,20,60,60)
- drawLogo(this.osCanvas , osCtx);
- var bigImageData = osCtx.getImageData(0,0,this.osCanvas.width,this.osCanvas.height);
- var index=0;
- dots.sort(function(a , b){
- return Math.random()-Math.random();
- })
- for(var x=0;x
- for(var y=0;y
- var i = (y*bigImageData.width + x)*4;
- if(bigImageData.data[i+3]>128){
- var d = dots[index];
- if(d){
- d.setEnd(x+(canvas.width/2-300) , y+50)
- d.color = "rgba("+bigImageData.data[i]+","+bigImageData.data[i+1]+","+bigImageData.data[i+2]+",1)";
- index++
- }
- }
- }
- }
- setTimeout(function(){
- var endindex = index;
- for(var i=0;i
- if(dots[index]){
- var d = dots[index];
- d.globleDown = true;
- d.vx = Math.random()*100-50;
- }
- index++;
- }
- } , 2000)
- }
- function endState(){
- canvas.width = 600;
- canvas.height = 100;
- canvas.style.display="block";
- canvas.style.top = "50px";
- canvas.style.left = (window.innerWidth-canvas.width)/2+"px";
- cube = new Cube(50);
- cube._initVector(50,50);
- }
- function drawLogo(canvas , ctx){
- ctx.textAlign = "center";
- ctx.textBaseline = "middle";
- ctx.font="65px 微軟雅黑,黑體 bold"
- ctx.fillStyle="#E06D2F"
- ctx.fillText("DEMO" , 300 , canvas.height/2)
- ctx.font="40px 微軟雅黑,黑體 bold"
- ctx.fillStyle="#405159"
- ctx.fillText("吖猩的" , 160 , canvas.height/2)
- ctx.fillText("小窩" , 420 , canvas.height/2)
- }
- var num = 0;
- ap.update = function(time){
- time = time/100;
- if(this.state==="first"||this.state==="before"){
- var completeNum = 0;
- dots.forEach(function(dot){
- if(dot.visible) dot.loop(time);
- if(dot.jl<5){
- completeNum++
- }
- });
- if(completeNum>=5*dots.length/6){
- if(this.state==="before"){
- this.state = "first";
- dots.forEach(function(dot){
- dot.setEnd(dot.nextox , dot.nextoy);
- });
- }else {
- this.state = "second";
- this.changeState();
- }
- }
- }else if(this.state==="second"){
- var completeNum = 0,
- allnum = 0;
- dots.forEach(function(dot){
- if(dot.visible) dot.loop(time);
- if(dot.globleDown){
- allnum++;
- if(Math.abs(dot.y-canvas.height)<2){
- completeNum++
- }
- }
- });
- if(completeNum===allnum&&allnum!==0){
- this.state = "third";
- part_2.animate();
- endState();
- }
- }else if(this.state==="third"){
- cube.update();
- drawLogo(canvas , ctx);
- }
- }
- return new animate();
- })(window)
標(biāo)題名稱(chēng):canvas學(xué)習(xí)筆記:小小滴公式,大大滴樂(lè)趣
網(wǎng)頁(yè)URL:http://m.5511xx.com/article/cojjccg.html


咨詢(xún)
建站咨詢(xún)
