Kod lengkap kesan ini adalah seperti berikut Simpan kod ke fail HTML dan bukanya untuk melihat kesannya. Salin kod Kod adalah seperti berikut: HTML5 Kesan nyalaan kanvas<br>badan{margin: 0; padding: 0; }<br>#canvas-keleyi- com {paparan: blok;}<br> canvas><br>window.onload = function(){<br>var keleyi_canvas = document.getElementById("canvas- kel" "eyi-com"); <br>var ctx = keleyi_canvas.getContext("2d");<br>var W = window.innerWidth, H = window.innerHeight;<br>keleyi_canvas.width = W;<br>keleyi_canvas.height = H;< ;/p> <p>zarah var = [];<br>tetikus var = {};</p> <p>//Mari buat beberapa zarah sekarang<br>var particle_count = 100;<br>for(var i = 0; i < particle_count; i )<br>{<br>partikel.push(new particle( ));<br>}<br>keleyi_canvas.addEventListener('mousemove', track_mouse, false);</p> <p>fungsi track_mouse(e)<br>{<br>mouse.x = e.pageX;<br>mouse.y = e.pageY;<br>}</p> <p>zarah fungsi()<br>{<br>kelajuan ini = {x: -2.5 Math.random()*5, y: -15 Math.random()*10};<br>// lokasi = koordinat tetikus<br>//Sekarang nyalaan mengikuti koordinat tetikus<br>jika(mouse.x && mouse.y)<br>{<br>this.location = {x: mouse.x, y: mouse .y};<br>}<br>lain<br>{<br>lokasi ini = {x: W/2, y: H/2};<br>}<br>//julat jejari = 10 -30<br>ini.radius = 10 Math.random()*20;<br>//life range = 20-30<br>this.life = 20 Math.random()*10;<br>this. baki_hidup = this.life;<br>//colors<br>this.r = Math.round(Math.random()*255);<br>this.g = Math.round(Math.random()*255 );<br>this.b = Math.round(Math.random()*255);<br>}</p> <p>function draw()<br>{<br>ctx.globalCompositeOperation = "source-over";<br>ctx.fillStyle = "black";<br>ctx.fillRect(0, 0, W, H );<br>ctx.globalCompositeOperation = "lebih ringan";</p> <p>for(var i = 0; i < particles.length; i )<br>{<br>var p = particles[i];<br>ctx.beginPath();<br>p.opacity = Math.round(p.remaining_life/p.life*100)/100<br>var gradient = ctx.createRadialGradient(p.location.x, p.location.y, 0, p.location.x, p.location .y, p.radius);<br>gradient.addColorStop(0, "rgba(" p.r ", " p.g ", " p.b ", " p.opacity ")");<br>gradient.addColorStop(0.5, "rgba(" p.r ", " p.g ", " p.b ", " p.opacity ")");<br>gradient.addColorStop(1, "rgba(" p.r ", " p.g ", " p.b ", 0) ");<br>ctx.fillStyle = kecerunan;<br>ctx.arc(p.location.x, p.location.y, p.radius, Math.PI*2, false);<br>ctx.fill ();</p> <p><br>p.baki_hidup--;<br>p.radius--;<br>p.location.x = p.speed.x;<br>p.location.y = p.speed. y;</p> <p>if(p.remaining_life < 0 || p.radius < 0)<br>{<br>zarah[i] = new particle();<br>}<br>}<br>} </p> <p>setInterval(draw, 86);<br>}<br>