html5使用canvas實現(xiàn)跟隨光標(biāo)跳動的火焰效果
來源:易賢網(wǎng) 閱讀:1646 次 日期:2016-07-11 11:00:50
溫馨提示:易賢網(wǎng)小編為您整理了“html5使用canvas實現(xiàn)跟隨光標(biāo)跳動的火焰效果”,方便廣大網(wǎng)友查閱!

本效果的完整代碼如下,把代碼保存到HTML文件中打開也能查看效果,火焰會跟隨光標(biāo):

代碼如下:

<!DOCTYPE HTML>

<head>

<meta charset=utf-8" />

<title>HTML5 Canvas火焰效果</title>

<style type="text/css">

body{margin: 0; padding: 0;}

#canvas-keleyi-com {display: block;}

</style>

</head>

<body>

<canvas id="canvas-keleyi-com"></canvas>

<script type="text/javascript">

window.onload = function(){

var keleyi_canvas = document.getElementById("canvas-kel"+"eyi-com");

var ctx = keleyi_canvas.getContext("2d");

var W = window.innerWidth, H = window.innerHeight;

keleyi_canvas.width = W;

keleyi_canvas.height = H;</p> <p>var particles = [];

var mouse = {};</p> <p>//Lets create some particles now

var particle_count = 100;

for(var i = 0; i < particle_count; i++)

{

particles.push(new particle());

}

keleyi_canvas.addEventListener('mousemove', track_mouse, false);</p> <p>function track_mouse(e)

{

mouse.x = e.pageX;

mouse.y = e.pageY;

}</p> <p>function particle()

{

this.speed = {x: -2.5+Math.random()*5, y: -15+Math.random()*10};

//location = mouse coordinates

//Now the flame follows the mouse coordinates

if(mouse.x && mouse.y)

{

this.location = {x: mouse.x, y: mouse.y};

}

else

{

this.location = {x: W/2, y: H/2};

}

//radius range = 10-30

this.radius = 10+Math.random()*20;

//life range = 20-30

this.life = 20+Math.random()*10;

this.remaining_life = this.life;

//colors

this.r = Math.round(Math.random()*255);

this.g = Math.round(Math.random()*255);

this.b = Math.round(Math.random()*255);

}</p> <p>function draw()

{

ctx.globalCompositeOperation = "source-over";

ctx.fillStyle = "black";

ctx.fillRect(0, 0, W, H);

ctx.globalCompositeOperation = "lighter";</p> <p>for(var i = 0; i < particles.length; i++)

{

var p = particles[i];

ctx.beginPath();

p.opacity = Math.round(p.remaining_life/p.life*100)/100

var gradient = ctx.createRadialGradient(p.location.x, p.location.y, 0, p.location.x, p.location.y, p.radius);

gradient.addColorStop(0, "rgba("+p.r+", "+p.g+", "+p.b+", "+p.opacity+")");

gradient.addColorStop(0.5, "rgba("+p.r+", "+p.g+", "+p.b+", "+p.opacity+")");

gradient.addColorStop(1, "rgba("+p.r+", "+p.g+", "+p.b+", 0)");

ctx.fillStyle = gradient;

ctx.arc(p.location.x, p.location.y, p.radius, Math.PI*2, false);

ctx.fill();</p> <p>

p.remaining_life--;

p.radius--;

p.location.x += p.speed.x;

p.location.y += p.speed.y;</p> <p>if(p.remaining_life < 0 || p.radius < 0)

{

particles[i] = new particle();

}

}

}</p> <p>setInterval(draw, 86);

}

</script>

</body>

</html>

更多信息請查看網(wǎng)頁制作
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)