火焰鼠标跟随 canvas
来源:互联网 发布:linux tac命令 编辑:程序博客网 时间:2024/04/28 15:43
<!DOCTYPE HTML><html><head><meta charset="utf-8" /><title>HTML5 Canvas火焰效果</title><style>.body{ }#myCanvas{ position: absolute; left: 0px; top: 0px;}</style><script>window.onload = function(){var canvas = document.getElementById("myCanvas");var ctx = canvas.getContext("2d");//Make the canvas occupy the full pagevar W = window.innerWidth, H = window.innerHeight;canvas.width = W;canvas.height = H;var particles = [];var mouse = {};//Lets create some particles nowvar particle_count = 50;for(var i = 0; i < particle_count; i++){particles.push(new particle());}//finally some mouse trackingcanvas.addEventListener('mousemove', track_mouse, false);function track_mouse(e){//since the canvas = full page the position of the mouse //relative to the document will sufficemouse.x = e.pageX;mouse.y = e.pageY;}function particle(){//speed, life, location, life, colors//speed.x range = -2.5 to 2.5 //speed.y range = -15 to -5 to make it move upwards//lets change the Y speed to make it look like a flamethis.speed = {x: -2.5+Math.random()*5, y: -15+Math.random()*10};//location = mouse coordinates//Now the flame follows the mouse coordinatesif(mouse.x && mouse.y){this.location = {x: mouse.x, y: mouse.y};}else{this.location = {x: W/2, y: H/2};}//radius range = 10-30this.radius = 10+Math.random()*20;//life range = 20-30this.life = 20+Math.random()*10;this.remaining_life = this.life;//colorsthis.r = Math.round(Math.random()*255);this.g = Math.round(Math.random()*255);this.b = Math.round(Math.random()*255);}function draw(){//Painting the canvas black//Time for lighting magic//particles are painted with "lighter"//In the next frame the background is painted normally without blending to the //previous framectx.globalCompositeOperation = "source-over";ctx.fillStyle = "black";ctx.fillRect(0, 0, W, H);ctx.globalCompositeOperation = "lighter";for(var i = 0; i < particles.length; i++){var p = particles[i];ctx.beginPath();//changing opacity according to the life.//opacity goes to 0 at the end of life of a particlep.opacity = Math.round(p.remaining_life/p.life*100)/100//a gradient instead of white fillvar 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();//lets move the particlesp.remaining_life--;p.radius--;p.location.x += p.speed.x;p.location.y += p.speed.y;//regenerate particlesif(p.remaining_life < 0 || p.radius < 0){//a brand new particle replacing the dead oneparticles[i] = new particle();}}}setInterval(draw, 30);}</script></head><body> <img src="css/logo_topbar.png" id="scream"><canvas id="myCanvas">Your browser does not support the HTML5 canvas tag.</canvas></body></html>
效果图:
0 0
- 火焰鼠标跟随 canvas
- HTML5 Canvas火焰闪烁动画 火焰跟随鼠标
- canvas 响应鼠标火焰喷射
- canvas 火焰
- canvas+javascript动画初级应用-鼠标捕捉跟随
- 前端成长之路—canvas实现跟随鼠标和跟随手指粒子特效
- 跟随鼠标的萤火虫
- 时钟跟随鼠标
- 鼠标跟随的问题
- DIV跟随鼠标显示
- 鼠标跟随文字
- 鼠标跟随代码
- 时钟跟随鼠标JS
- 跟随鼠标的时钟
- 鼠标跟随的div
- 跟随鼠标的文字
- 跟随鼠标图片
- 鼠标文字跟随效果
- Android 上传图片到服务器
- 图像算法---贝塞尔曲线拟合
- [UML系列一]UML总览
- 使用协议Protocol实现ViewController之间传值
- 如何使用libsvm进行分类(一)
- 火焰鼠标跟随 canvas
- 【C++ STL应用与实现】0: 感恩STL——STL, ACM和年轻的我们
- hdu-2043 密码
- 文本框去空格,jquery用在不同浏览器
- js验证银行卡号
- 网卡初步认识
- jni的简单使用
- 利用X-UA-Compatible定义网页在IE下的兼容状态
- iOS 对国际化语言的支持.