-
Notifications
You must be signed in to change notification settings - Fork 4
/
move.min.js
9 lines (9 loc) · 4.43 KB
/
move.min.js
1
2
3
4
5
6
7
8
9
(function(){function f(a){a||(a={});_.defaults(a,{x:0,y:0,z:0});this.x=a.x;this.y=a.y;this.z=a.z}function e(a){var b=this;a||(a={});_.defaults(a,{trace:!1,speed:1,draw:e.prototype.draw,preDraw:function(a){},postDraw:function(a){},setContext:function(a){}});this.systems=[];if(!a.context&&!a.draw)throw"Move.Controller requires a 2D context or custom draw function.";_.each("trace context preDraw postDraw draw setContext speed".split(" "),function(c){b[c]=a[c]});_.bindAll(this,"step")}function h(a){var b=
this;a||(a={});_.defaults(a,{newParticle:function(){return new k},numParticles:0,rules:[],preDraw:function(a){},postDraw:function(a){},preUpdate:function(a){},postUpdate:function(a){},setContext:function(a){},onDeath:function(){},init:function(){}});this.particles=[];this.lastStep=0;_.each("newParticle rules trace preDraw postDraw setContext onDeath numParticles preUpdate postUpdate".split(" "),function(c){b[c]=a[c]});a.init.call(this);this.reset()}function k(a){var b=this;a||(a={});this.defaults=
{pos:new f,vel:new f,r:255,g:0,b:0,a:1,size:2,trail:0,draw:k.prototype.draw.bind(this),preUpdate:function(a){},postUpdate:function(a){},isDead:function(){return!1},init:function(){}};_.defaults(a,this.defaults);a.origPos||(a.origPos=new f(a.pos));this.prevPos=[];_.each("draw isDead trail r g b a size preUpdate postUpdate pos vel origPos".split(" "),function(c){b[c]=a[c]});a.init.call(this)}var l,m=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame;
this.Move=l={};f.prototype.add=function(a){this.x+=a.x;this.y+=a.y;this.z+=a.z;return this};f.prototype.sub=function(a){this.x-=a.x;this.y-=a.y;this.z-=a.z;return this};f.prototype.multiplyScalar=function(a){this.x*=a;this.y*=a;this.z*=a;return this};f.prototype.length=function(){return Math.sqrt(this.lengthSq())};f.prototype.lengthSq=function(){return this.x*this.x+this.y*this.y+this.z*this.z};l.Vector=f;e.prototype.update=function(a){_.each(this.systems,function(b){b.update(a)})};e.prototype.reset=
function(){_.each(this.systems,function(a){a.reset()})};e.prototype.addSystem=function(a){this.systems.push(a);return this};e.prototype.start=function(){this.paused=!1;this.step(0)};e.prototype.pause=function(){this.paused=!0};e.prototype.step=function(a){var b=0;a&&(b=0.001*(a-this.lastStep),this.lastStep=a);0.1>b&&(this.update(b*this.speed),this.draw(this.context));this.paused||m(this.step)};e.prototype.clear=function(){this.context.clearRect(0,0,this.context.canvas.width,this.context.canvas.height)};
e.prototype.draw=function(a){a.save();this.trace||this.clear();this.setContext(a);this.preDraw(a);_.each(this.systems,function(b){b.draw(a)});this.postDraw(a);a.restore()};l.Controller=e;h.prototype.reset=function(){this.particles=[];for(var a=0;a<this.numParticles;a++)this.addParticle()};h.prototype.addRule=function(a){this.rules.push(a)};h.prototype.draw=function(a){this.setContext(a);this.preDraw(a);_.each(this.particles,function(b,c){b.drawAll(a)});this.postDraw(a)};h.prototype.update=function(a){var b=
this;this.preUpdate(a);_.each(this.particles,function(c,d){_.each(b.rules,function(b){b(c,d,a)});c.update(a)&&(b.particles[d]=null,b.particles=_.compact(b.particles),b.onDeath())});this.postUpdate(a)};h.prototype.addParticle=function(a){this.particles.push(a||this.newParticle(this.particles.length))};l.System=h;k.prototype.draw=function(a,b,c){a.beginPath();a.arc(c.x,c.y,this.size,0,2*Math.PI);a.fillStyle="rgba("+this.r+", "+this.g+", "+this.b+", "+this.a*b+")";a.fill()};k.prototype.drawAll=function(a){var b=
this;_.each(this.prevPos,function(c,d){b.draw(a,d/b.trail,c)});this.draw(a,1,this.pos)};k.prototype.update=function(a){var b;this.preUpdate(a);b=new f(this.vel);this.trail&&(this.prevPos&&this.prevPos.length===this.trail&&(this.prevPos=_.rest(this.prevPos)),this.prevPos.push(new f(this.pos)));this.pos.add(b.multiplyScalar(a));this.postUpdate(a);return this.isDead()};l.Particle=k;l.Rules={gravity:function(a){return function(b,c,d){b.vel.y+=a*d}},resistance:function(a){return function(b,c,d){b.vel.multiplyScalar(1-
a*d)}},attract:function(a,b){return function(c,d,e){d=b||c.origPos;var g=new f(c.pos);g.sub(d);c.vel.sub(g.multiplyScalar(e*a))}},magnet:function(a,b){return function(c,d,e){var g=b||c.origPos;d=new f(c.pos);d.sub(g);g=d.lengthSq();0.01>g||c.vel.sub(d.multiplyScalar(e*a/g))}},wall:function(a,b){return function(c,d,e){d=c.vel[b]*e;e=c.pos[b];var f=a+c.size*(0<d?-1:1);e>f!=e+d>f&&(c.pos[b]=2*f-d-e,c.vel[b]=-c.vel[b])}}}}).call(this);