4000 前端成长之路—canvas实现跟随鼠标和跟随手指粒子特效
2017-07-20 03:30
543 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>粒子跟随特效</title> <style> *{ margin: 0; padding: 0; } body{ overflow: hidden; background: #000; } </style> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById("canvas"); canvas.width = document.documentElement.clientWidth; canvas.height = document.documentElement.clientHeight; var ctx = canvas.getContext("2d"); var flag = false; //启动的标志 var particles = []; var color=['white','red','orange','yellow','green','blue','indigo','purple']; animate(); function animate(){ setInterval(function(){ render(); area(); },1000/60) } canvas.onmousedown = function(event){ var e = event|| window.event; flag = true; do_spawn(e,35*Math.random()+35); }; canvas.onmouseup = function(){ flag = false; }; canvas.onmouseout = function(){ flag = false; }; canvas.onmousemove = function(event){ var e = event|| window.event; if(flag){ do_spawn(e,9); } }; canvas.ontouchstart=function(e){ flag = true; do_spawn(e,35*Math.random()+35); }; canvas.ontouchend=function(e){ flag = false; }; canvas.ontouchmove=function(e){ if(flag){ do_spawn(e,9); } }; function do_spawn(e,n){ //设置孵化器的生产数量 var x = e.clientX || e.touches[0].pageX; //鼠标坐标||移动端触摸坐标 var y = e.clientY || e.touches[0].pageY; for (var i=0;i<n;i++){ spawn(x,y); } } function spawn(x,y){ //孵化器,生成一个原点对象 var particle=new Particle(); particle.init(x,y); particles.push(particle); } function render(){ //把生成的原点渲染出来 ctx.clearRect(0,0,canvas.width,canvas.height); for(var i = 0,len = particles.length; i < len; i++){ particles[i].draw(); particles[i].update(); } } function area(){ //半径足够小的时候删除该点 var n = 0; for( var i = 0, l = particles.length; i < l; i++ ) { if (particles[i].r >1 ) { particles[n++] = particles[i]; } } while( particles.length > Math.min(700,n) ) { particles.pop(); } } function Particle(){} //构造函数,小球原型 Particle.prototype={ init:function(x,y){ //初始化小球各项数据 this.x = x || 0.0; this.y = y || 0.0; this.r = 10*Math.random() + 10 || 40; this.color = color[~~(Math.random()*8)]; this.theta = Math.random()*2*Math.PI; this.R = Math.random()*4 + 2; this.vx = Math.cos(this.theta)*this.R; this.vy = Math.sin(this.theta)*this.R; }, draw:function(){ //画出一个球的方法 ctx.beginPath(); ctx.arc(this.x,this.y,this.r,0,Math.PI*2); ctx.fillStyle = this.color; ctx.fill(); }, update:function(){ //改变球的各项属相的方法 this.x += this.vx; this.y += this.vy; this.vx += Math.cos(this.theta)*.1; this.vy += Math.sin(this.theta)*.1; this.vx *= .94; this.vy *= .94; //给速度设置一个衰减系数 this.r *=.92; this.color = color[~~(Math.random()*8)]; //动态改变圆点的颜色达到闪烁的效果 } }; </script> </body> </html>
相关文章推荐
- JS实现可爱的鼠标跟随特效
- 基于CSS3和jQuery实现跟随鼠标方位的Hover特效
- JavaScript实现文字跟随鼠标特效
- canvas实现粒子特效
- JS+html5实现跟随鼠标移动而散开的粒子效果
- canvas实现跟随鼠标旋转的箭头
- 【前端】javascript实现鼠标跟随特效
- unity3d实现一个人物跟随鼠标点击地面那点就移动那点,实现点击特效加人物动画方向的切换。
- [Html5 canvas]鼠标悬停吸附粒子背景特效
- JavaScript实现文字跟随鼠标特效
- canvas实现粒子线条炫酷特效
- 原生JS+HTML5实现跟随鼠标一起流动的粒子动画效果
- html5跟随鼠标炫酷网站引导页粒子动画特效
- JavaScript实现鼠标点击导航栏变色特效
- 用js实现简单的鼠标跟随效果
- 使用 Cocos2d-x 和粒子编辑器实现“天天爱消除”场景特效
- 用canvas实现鼠标拖动绘制矩形框
- 一款由html5 canvas实现五彩小圆圈背景特效
- 用canvas实现鼠标拖动绘制矩形框
- canvas 绘制跟随鼠标移动的线条