function Particle(t,i,e){this.init(t,i,e)}Particle.prototype={init:function(t,i,e){this.alive=!0,this.radius=e||10,this.wander=.15,this.theta=random(TWO_PI),this.drag=.92,this.color="#fff",this.x=t||0,this.y=i||0,this.vx=0,this.vy=0},move:function(){this.x+=this.vx,this.y+=this.vy,this.vx*=this.drag,this.vy*=this.drag,this.theta+=random(-.5,.5)*this.wander,this.vx+=.1*sin(this.theta),this.vy+=.1*cos(this.theta),this.radius*=.96,this.alive=this.radius>.5},draw:function(t){t.beginPath(),t.arc(this.x,this.y,this.radius,0,TWO_PI),t.fillStyle=this.color,t.fill()}};var MAX_PARTICLES=80,COLOURS=["#0AF2E4","#FFC450","#FFE44F","#5284FF","#FF68A5"],particles=[],pool=[],demo=Sketch.create({container:$(".wrapper")[0]});demo.setup=function(){var t,i,e;for(t=0;20>t;t++)i=.5*demo.width+random(-100,100),e=.5*demo.height+random(-100,100),demo.spawn(i,e)},demo.spawn=function(t,i){particles.length>=MAX_PARTICLES&&pool.push(particles.shift()),particle=pool.length?pool.pop():new Particle,particle.init(t,i,random(5,10)),particle.wander=random(.5,2),particle.color=random(COLOURS),particle.drag=random(.9,.99),theta=random(TWO_PI),force=random(1,4),particle.vx=sin(theta)*force,particle.vy=cos(theta)*force,particles.push(particle)},demo.update=function(){var t,i;for(t=particles.length-1;t>=0;t--)i=particles[t],i.alive?i.move():pool.push(particles.splice(t,1)[0])},demo.draw=function(){demo.globalCompositeOperation="lighter";for(var t=particles.length-1;t>=0;t--)particles[t].draw(demo)},demo.mousemove=function(){var t,i,e,o,a;for(e=0,a=demo.touches.length;a>e;e++)for(t=demo.touches[e],i=random(1,2),o=0;i>o;o++)demo.spawn(t.x,t.y)};


Back to home | File page

Subscribe | Register | Login | N