HTML5中怎么用Canvas實(shí)現(xiàn)超炫酷煙花綻放動(dòng)畫(huà)

這篇文章主要介紹了HTML5中怎么用Canvas實(shí)現(xiàn)超炫酷煙花綻放動(dòng)畫(huà)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇HTML5中怎么用Canvas實(shí)現(xiàn)超炫酷煙花綻放動(dòng)畫(huà)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

創(chuàng)新互聯(lián)專注于漯河網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供漯河營(yíng)銷型網(wǎng)站建設(shè),漯河網(wǎng)站制作、漯河網(wǎng)頁(yè)設(shè)計(jì)、漯河網(wǎng)站官網(wǎng)定制、小程序開(kāi)發(fā)服務(wù),打造漯河網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供漯河網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

HTML代碼:

XML/HTML Code復(fù)制內(nèi)容到剪貼板

<div id=”gui”></div>  

<div id=”canvas-container”> <div id=”mountains2&Prime;></div>    

<div id=”mountains1&Prime;></div><div id=”skyline”></div> </div>  

HTML的結(jié)構(gòu)非常簡(jiǎn)單,即構(gòu)造了一個(gè)canvas容器,我們會(huì)利用JS在這個(gè)容器中生成一個(gè)Canvas對(duì)象。看最后的JS代碼你就會(huì)知道了。

CSS代碼:

CSS Code復(fù)制內(nèi)容到剪貼板

#canvas-container { background: #000 url(bg.jpg); height: 400px; left: 50%; margin: -200px 0 0 -300px; position: absolute; top: 50%; width: 600px; z-index: 2;   

} canvas { cursor: crosshair; display: block; position: relative; z-index: 3;   

} canvas:active { cursor: crosshair;   

} #skyline { background: url(skyline.png) repeat-x 50% 0; bottombottom: 0; height: 135px; left: 0; position: absolute; width: 100%; z-index: 1;       

} #mountains1 { background: url(mountains1.png) repeat-x 40% 0; bottombottom: 0; height: 200px; left: 0; position: absolute; width: 100%; z-index: 1;       

} #mountains2 { background: url(mountains2.png) repeat-x 30% 0; bottombottom: 0; height: 250px; left: 0; position: absolute; width: 100%; z-index: 1;       

} #gui { rightright: 0; position: fixed; top: 0; z-index: 3;   

}  

CSS代碼沒(méi)什么特別,主要也就定義一下背景色和邊框之類的。

接下來(lái)是最重要的Javascript代碼。

Javascript代碼:

JavaScript Code復(fù)制內(nèi)容到剪貼板

self.init = function(){       

    self.dt = 0;   

        self.oldTime = Date.now();   

        self.canvas = document.createElement('canvas');                   

        self.canvasContainer = $('#canvas-container'); var canvasContainerDisabled = document.getElementById('canvas-container');   

        self.canvas.onselectstart = function() { return false;   

        };   

        self.canvas.width = self.cw = 600;   

        self.canvas.height = self.ch = 400;       

        self.particles = [];       

        self.partCount = 30;   

        self.fireworks = [];       

        self.mx = self.cw/2;   

        self.my = self.ch/2;   

        self.currentHue = 170;   

        self.partSpeed = 5;   

        self.partSpeedVariance = 10;   

        self.partWind = 50;   

        self.partFriction = 5;   

        self.partGravity = 1;   

        self.hueMin = 150;   

        self.hueMax = 200;   

        self.fworkSpeed = 2;   

        self.fworkAccel = 4;   

        self.hueVariance = 30;   

        self.flickerDensity = 20;   

        self.showShockwave = false;   

        self.showTarget = true;   

        self.clearAlpha = 25;   

        self.canvasContainer.append(self.canvas);   

        self.ctx = self.canvas.getContext('2d');   

        self.ctx.lineCap = 'round';   

        self.ctx.lineJoin = 'round';   

        self.lineWidth = 1;   

        self.bindEvents();               

        self.canvasLoop();   

        self.canvas.onselectstart = function() { return false;   

        };   

    };  

這段JS代碼主要是往canvas容器中構(gòu)造一個(gè)Canvas對(duì)象,并且對(duì)這個(gè)canvas對(duì)象的外觀以及動(dòng)畫(huà)屬性作了初始化。

JavaScript Code復(fù)制內(nèi)容到剪貼板

var Particle = function(x, y, hue){ this.x = x; this.y = y; this.coordLast = [   

            {x: x, y: y},   

            {x: x, y: y},   

            {x: x, y: y}   

        ]; this.angle = rand(0, 360); this.speed = rand(((self.partSpeed - self.partSpeedVariance) <= 0) ? 1 : self.partSpeed - self.partSpeedVariance, (self.partSpeed + self.partSpeedVariance)); this.friction = 1 - self.partFriction/100; this.gravity = self.partGravity/2; this.hue = rand(hue-self.hueVariance, hue+self.hueVariance); this.brightness = rand(50, 80); this.alpha = rand(40,100)/100; this.decay = rand(10, 50)/1000; this.wind = (rand(0, self.partWind) - (self.partWind/2))/25; this.lineWidth = self.lineWidth;   

    };   

    Particle.prototype.update = function(index){ var radians = this.angle * Math.PI / 180; var vx = Math.cos(radians) * this.speed; var vy = Math.sin(radians) * this.speed + this.gravity; this.speed *= this.friction; this.coordLast[2].x = this.coordLast[1].x; this.coordLast[2].y = this.coordLast[1].y; this.coordLast[1].x = this.coordLast[0].x; this.coordLast[1].y = this.coordLast[0].y; this.coordLast[0].x = this.x; this.coordLast[0].y = this.y; this.x += vx * self.dt; this.y += vy * self.dt; this.angle += this.wind; this.alpha -= this.decay; if(!hitTest(0,0,self.cw,self.ch,this.x-this.radius, this.y-this.radius, this.radius*2, this.radius*2) || this.alpha < .05){                       

            self.particles.splice(index, 1);       

        }               

    };   

    Particle.prototype.draw = function(){ var coordRand = (rand(1,3)-1);   

        self.ctx.beginPath();                                   

        self.ctx.moveTo(Math.round(this.coordLast[coordRand].x), Math.round(this.coordLast[coordRand].y));   

        self.ctx.lineTo(Math.round(this.x), Math.round(this.y));   

        self.ctx.closePath();                   

        self.ctx.strokeStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+this.alpha+')';   

        self.ctx.stroke(); if(self.flickerDensity > 0){ var inverseDensity = 50 - self.flickerDensity; if(rand(0, inverseDensity) === inverseDensity){   

                self.ctx.beginPath();   

                self.ctx.arc(Math.round(this.x), Math.round(this.y), rand(this.lineWidth,this.lineWidth+3)/2, 0, Math.PI*2, false)  self.ctx.closePath(); var randAlpha = rand(50,100)/100;   

                self.ctx.fillStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+randAlpha+')';   

                self.ctx.fill();   

            }       

        }   

    };  

這段JS代碼的功能是實(shí)現(xiàn)煙花爆炸后的小顆粒的繪制,從draw方法中可以看出,創(chuàng)建幾個(gè)隨機(jī)點(diǎn),煙花顆粒即可在這個(gè)范圍的隨機(jī)點(diǎn)中散落。

JavaScript Code復(fù)制內(nèi)容到剪貼板

var Firework = function(startX, startY, targetX, targetY){ this.x = startX; this.y = startY; this.startX = startX; this.startY = startY; this.hitX = false; this.hitY = false; this.coordLast = [   

            {x: startX, y: startY},   

            {x: startX, y: startY},   

            {x: startX, y: startY}   

        ]; this.targetX = targetX; this.targetY = targetY; this.speed = self.fworkSpeed; this.angle = Math.atan2(targetY - startY, targetX - startX); this.shockwaveAngle = Math.atan2(targetY - startY, targetX - startX)+(90*(Math.PI/180)); this.acceleration = self.fworkAccel/100; this.hue = self.currentHue; this.brightness = rand(50, 80); this.alpha = rand(50,100)/100; this.lineWidth = self.lineWidth; this.targetRadius = 1;   

    };   

    Firework.prototype.update = function(index){   

        self.ctx.lineWidth = this.lineWidth;   

        vx = Math.cos(this.angle) * this.speed,   

        vy = Math.sin(this.angle) * this.speed; this.speed *= 1 + this.acceleration; this.coordLast[2].x = this.coordLast[1].x; this.coordLast[2].y = this.coordLast[1].y; this.coordLast[1].x = this.coordLast[0].x; this.coordLast[1].y = this.coordLast[0].y; this.coordLast[0].x = this.x; this.coordLast[0].y = this.y; if(self.showTarget){ if(this.targetRadius < 8){ this.targetRadius += .25 * self.dt;   

            } else { this.targetRadius = 1 * self.dt;       

            }   

        } if(this.startX >= this.targetX){ if(this.x + vx <= this.targetX){ this.x = this.targetX; this.hitX = true;   

            } else { this.x += vx * self.dt;   

            }   

        } else { if(this.x + vx >= this.targetX){ this.x = this.targetX; this.hitX = true;   

            } else { this.x += vx * self.dt;   

            }   

        } if(this.startY >= this.targetY){ if(this.y + vy <= this.targetY){ this.y = this.targetY; this.hitY = true;   

            } else { this.y += vy * self.dt;   

            }   

        } else { if(this.y + vy >= this.targetY){ this.y = this.targetY; this.hitY = true;   

            } else { this.y += vy * self.dt;   

            }   

        } if(this.hitX && this.hitY){ var randExplosion = rand(0, 9);   

            self.createParticles(this.targetX, this.targetY, this.hue);   

            self.fireworks.splice(index, 1);                       

        }   

    };   

    Firework.prototype.draw = function(){   

        self.ctx.lineWidth = this.lineWidth; var coordRand = (rand(1,3)-1);                       

        self.ctx.beginPath();                               

        self.ctx.moveTo(Math.round(this.coordLast[coordRand].x), Math.round(this.coordLast[coordRand].y));   

        self.ctx.lineTo(Math.round(this.x), Math.round(this.y));   

        self.ctx.closePath();   

        self.ctx.strokeStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+this.alpha+')';   

        self.ctx.stroke(); if(self.showTarget){   

            self.ctx.save();   

            self.ctx.beginPath();   

            self.ctx.arc(Math.round(this.targetX), Math.round(this.targetY), this.targetRadius, 0, Math.PI*2, false)   

            self.ctx.closePath();   

            self.ctx.lineWidth = 1;   

            self.ctx.stroke();   

            self.ctx.restore();   

        } if(self.showShockwave){   

            self.ctx.save();   

            self.ctx.translate(Math.round(this.x), Math.round(this.y));   

            self.ctx.rotate(this.shockwaveAngle);   

            self.ctx.beginPath();   

            self.ctx.arc(0, 0, 1*(this.speed/5), 0, Math.PI, true);   

            self.ctx.strokeStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+rand(25, 60)/100+')';   

            self.ctx.lineWidth = this.lineWidth;   

            self.ctx.stroke();   

            self.ctx.restore();   

        }                                    

    };  

這段JS代碼是創(chuàng)建煙花實(shí)例的,我們也可以從draw方法中看出,當(dāng)我們鼠標(biāo)點(diǎn)擊畫(huà)布中的某點(diǎn)時(shí),煙花發(fā)射的目的地就在那個(gè)點(diǎn)上。

關(guān)于“HTML5中怎么用Canvas實(shí)現(xiàn)超炫酷煙花綻放動(dòng)畫(huà)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“HTML5中怎么用Canvas實(shí)現(xiàn)超炫酷煙花綻放動(dòng)畫(huà)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章標(biāo)題:HTML5中怎么用Canvas實(shí)現(xiàn)超炫酷煙花綻放動(dòng)畫(huà)
本文來(lái)源:http://muchs.cn/article30/pphdpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、電子商務(wù)、網(wǎng)站改版移動(dòng)網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)