-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathspp.min.js
More file actions
29 lines (29 loc) · 15.8 KB
/
spp.min.js
File metadata and controls
29 lines (29 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var SPP=SPP||{REVISION:"Release1.0",AUTHOR:"flashhawk",BLOG:"flashquake.cn",github:"https://github.com/flashhawk",weibo:"http://weibo.com/flashawk"};SPP.frameTime=0;SPP.inherit=function(a,b){a.superClass=b;a.prototype=Object.create(b.prototype);a.prototype.constructor=a};SPP.extend=function(a,b){if(!b||"object"!==typeof b)return a;for(var c=Object.keys(b),d=c.length;d--;)a[c[d]]=b[c[d]];return a};
(function(){for(var a=0,b=["ms","moz","webkit","o"],c=0;c<b.length&&!window.requestAnimationFrame;++c)window.requestAnimationFrame=window[b[c]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[b[c]+"CancelAnimationFrame"]||window[b[c]+"CancelRequestAnimationFrame"];void 0===window.requestAnimationFrame&&(window.requestAnimationFrame=function(b,c){var e=Date.now(),f=Math.max(0,16-(e-a)),h=window.setTimeout(function(){b(e+f)},f);a=e+f;return h});window.cancelAnimationFrame=window.cancelAnimationFrame||
function(a){window.clearTimeout(a)}})();SPP.MathUtils=function(){};SPP.MathUtils.toDegree=function(a){return 180*(a/Math.PI)};SPP.MathUtils.toRadian=function(a){return a/180*Math.PI};SPP.MathUtils.sinD=function(a){return Math.sin(SPP.MathUtils.toRadian(a))};SPP.MathUtils.asinD=function(a){return SPP.MathUtils.toDegree(Math.asin(a))};SPP.MathUtils.cosD=function(a){return Math.cos(SPP.MathUtils.toRadian(a))};SPP.MathUtils.acosD=function(a){return SPP.MathUtils.toDegree(Math.acos(a))};SPP.MathUtils.tanD=function(a){return Math.tan(SPP.MathUtils.toRadian(a))};
SPP.MathUtils.atanD=function(a){return SPP.MathUtils.toDegree(Math.atan(a))};SPP.MathUtils.atan2D=function(a,b){return SPP.MathUtils.toDegree(Math.atan2(a,b))};SPP.MathUtils.random=function(a){return a*Math.random()>>0};SPP.Event=function(a){this.type=a;this.target=null};SPP.Event.prototype={constructor:SPP.Event,clone:function(){return new SPP.Event(this.type,this.target)}};SPP.EventDispatcher=function(){};
SPP.EventDispatcher.prototype={constructor:SPP.EventDispatcher,apply:function(a){a.addEventListener=SPP.EventDispatcher.prototype.addEventListener;a.removeEventListener=SPP.EventDispatcher.prototype.removeEventListener;a.removeAllEventListeners=SPP.EventDispatcher.prototype.removeAllEventListeners;a.dispatchEvent=SPP.EventDispatcher.prototype.dispatchEvent;a.on=SPP.EventDispatcher.prototype.addEventListener;a.once=SPP.EventDispatcher.prototype.once},addEventListener:function(a,b){void 0===this._listeners&&
(this._listeners={});var c=this._listeners;void 0===c[a]&&(c[a]=[]);-1===c[a].indexOf(b)&&c[a].push(b)},dispatchEvent:function(a){if(void 0!==this._listeners){var b=this._listeners[a.type];if(void 0!=b){a.target=this;for(var b=b.slice(),c=0,d=b.length;c<d;c++)b[c].call(this,a)}}},removeEventListener:function(a,b){if(void 0!==this._listeners){var c=this._listeners,d=c[a].indexOf(b);-1!==d&&c[a].splice(d,1)}},removeAllEventListeners:function(){if(void 0!==this._listeners){var a=this._listeners,b;for(b in a)delete a[b]}}};
SPP.EventDispatcher.prototype.on=SPP.EventDispatcher.prototype.addEventListener;SPP.EventDispatcher.prototype.once=function(a,b){function c(d){this.removeEventListener(a,c);b.call(this,d)}this.addEventListener(a,c)};SPP.Rectangle=function(a,b,c,d){this.x=a||0;this.y=b||0;this.width=c||0;this.height=d||0};
SPP.Rectangle.prototype={constructor:SPP.Rectangle,top:function(){return this.y},bottom:function(){return this.y+this.height},left:function(){return this.x},right:function(){return this.x+this.width},topLeft:function(){return new SPP.Point(this.x,this.y)},bottomRight:function(){return new SPP.Point(this.right(),this.bottom())},clone:function(){return SPP.Rectangle(this.x,this.y,this.width,this.height)},toString:function(){return"[Rectangle (x\x3d"+this.x+" y\x3d"+this.y+" width\x3d"+this.width+" height\x3d"+
this.height+")]"}};SPP.Vector2D=function(a,b){this.x=a||0;this.y=b||0};
SPP.Vector2D.prototype={constructor:SPP.Vector2D,toString:function(){return"[Vector2D (x\x3d"+this.x+" y\x3d"+this.y+")]"},clone:function(){return new SPP.Vector2D(this.x,this.y)},copy:function(a){this.x=a.x;this.y=a.y;return this},reset:function(a,b){this.x=a||0;this.y=b||0;return this},equals:function(a){return this.x==a.x&&this.y==a.y},add:function(a){this.x+=a.x;this.y+=a.y;return this},addNew:function(a){return new SPP.Vector2D(this.x+a.x,this.y+a.y)},addVectors:function(a,b){this.x=a.x+b.x;
this.y=a.y+b.y;return this},sub:function(a){this.x-=a.x;this.y-=a.x;return this},subNew:function(a){return new SPP.Vector2D(this.x-a.x,this.y-a.y)},subVectors:function(a,b){this.x=a.x-b.x;this.y=a.y-b.y;return this},negate:function(){this.x*=-1;this.y*=-1;return this},negateX:function(){this.x*=-1;return this},negateY:function(){this.y*=-1;return this},negateNew:function(){return new SPP.Vector2D(-this.x,-this.y)},scale:function(a){this.x*=a;this.y*=a;return this},scaleX:function(a){this.x*=a;return this},
scaleY:function(a){this.y*=a;return this},scaleNew:function(a){return new SPP.Vector2D(this.x*a,this.y*a)},getLength:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},setLength:function(a){a/=this.getLength();this.scale(a);return this},getAngle:function(){return SPP.MathUtils.atan2D(this.y,this.x)},setAngle:function(a){var b=this.getLength();this.x=b*SPP.MathUtils.cosD(a);this.y=b*SPP.MathUtils.sinD(a);return this},rotate:function(a){var b=SPP.MathUtils.sinD(a);a=SPP.MathUtils.cosD(a);var c=
this.x,d=this.y;this.x=c*a-d*b;this.y=c*b+d*a;return this},rotateNew:function(a){var b=this.clone();b.rotate(a);return b},dot:function(a){return this.x*a.x+this.y*a.y},projection:function(a){var b=this.angleBetween(a),b=this.getLength()*SPP.MathUtils.cosD(b)/a.getLength();return a.scaleNew(b)},normalizeNew:function(){return this.clone().normalize()},normalize:function(){0!=this.getLength()?this.scale(1/this.getLength()):this.scale(0);return this},getNormal:function(){return new SPP.Vector2D(-this.y,
this.x)},isPerpTo:function(a){return 0==this.dot(a)},angleBetween:function(a){a=this.dot(a)/(this.getLength()*a.getLength());return SPP.MathUtils.acosD(a)},distanceTo:function(a){return Math.sqrt(Math.pow(this.x-a.x,2)+Math.pow(this.y-a.y,2))}};SPP.VectorPool={__vectors:[],get:function(){return 0<this.__vectors.length?this.__vectors.pop():this._addToPool()},recycle:function(a){a.reset(0,0);this.__vectors.push(a)},_addToPool:function(){for(var a=0;50>a;a++)this.__vectors.push(new SPP.Vector2D);return new SPP.Vector2D}};SPP.Particle=function(){this.group=null;this.life=Infinity;this._forcesMap={};this.extra={};this.zone=this.onUpdate=null};
SPP.Particle.prototype={constructor:SPP.Particle,init:function(a,b,c){0>=c&&(c=-Infinity);this.position=SPP.VectorPool.get();this.velocity=SPP.VectorPool.get();this.acceleration=SPP.VectorPool.get();this.damp=SPP.VectorPool.get();this.damp.reset(0.1,0.1);this.life=c||Infinity;a instanceof Array&&(a=a[0]+Math.random()*a[1]);b instanceof Array&&(b=b[0]+Math.random()*b[1]);this.position.reset(a,b)},getForce:function(a){return this._forcesMap[a]},addForce:function(a,b){this._forcesMap[a]=b},removeForce:function(a){this._forcesMap[a].target===
this&&(this._forcesMap[a].life=0);delete this._forcesMap[a]},removeAllForces:function(){for(var a in this._forcesMap)this.removeForce(a)},render:function(){this.update();this.onUpdate&&this.onUpdate.apply(this);if(null!=this.group){var a=this.group.getForceMap(),b;for(b in a)a[b].render(this)||delete a[b]}for(b in this._forcesMap)this._forcesMap[b].render(this)||this.removeForce(b);this.velocity.add(this.acceleration);this.acceleration.reset(0,0);this.velocity.x*=1-this.damp.x;this.velocity.y*=1-
this.damp.y;this.position.add(this.velocity);this.zone&&this.zone.render(this);this.life-=SPP.frameTime;0<=this.life||(this.dispatchEvent(new SPP.Event("dead")),this.reset())},update:function(){},dealloc:function(){SPP.VectorPool.recycle(this.position);SPP.VectorPool.recycle(this.velocity);SPP.VectorPool.recycle(this.acceleration);SPP.VectorPool.recycle(this.damp);this.group=this.zone=this.onUpdate=this.extra=this._forcesMap=this.life=this.damp=this.acceleration=this.velocity=this.position=null},
reset:function(){SPP.VectorPool.recycle(this.position);SPP.VectorPool.recycle(this.velocity);SPP.VectorPool.recycle(this.acceleration);SPP.VectorPool.recycle(this.damp);this.damp=this.acceleration=this.velocity=this.position=null;this.life=0;this.removeAllForces();this.removeAllEventListeners();for(var a in this.extra)delete this.extra[a];this.group=this.zone=this.onUpdate=null}};SPP.extend(SPP.Particle.prototype,SPP.EventDispatcher.prototype);SPP.Pool=function(){var a=[];this.get=function(b){for(var c in a)if(a[c].constructor===b)return a.splice(c,1)[0];return new b};this.recycle=function(b){a.push(b)};this.getObjects=function(){return a};this.dealloc=function(){for(var b=0,c=a.length;b<c;b++)a[b].dealloc();a.length=0;a=null}};SPP.forcePool=new SPP.Pool;SPP.particlePool=new SPP.Pool;SPP.Group=function(a){var b=a,c={};this.createParticle=function(a){a=b.createParticle(a);a.group=this;return a};this.getForceMap=function(){return c};this.addForce=function(a,b){c[a]=b};this.removeForce=function(a){delete c[a]};this.removeAllForces=function(){for(var a in c)delete c[a]};this.dealloc=function(){this.removeAllForces();c=b=null}};SPP.ParticleSystem=function(){var a=[],b=[],c=[],d=null,g=!1;this.getParticles=function(){return a};this.getForces=function(){return b};this.createParticle=function(b){b=SPP.particlePool.get(b);a.push(b);return b};this.createForce=function(a){a=SPP.forcePool.get(a);b.push(a);return a};this.createGroup=function(){var a=new SPP.Group(this);c.push(a);return a};this.destroyGroup=function(b){var d=c.indexOf(b);if(-1!=d){for(var g=a.length;0<g--;)a[g].parent===b&&(SPP.particlePool.recycle(a[g]),a.splice(g,
1));c.splice(d,1);b.dealloc()}};this.destroyAllGroup=function(){for(var b=a.length;0<b--;)null!=a[b].parent&&(SPP.particlePool.recycle(a[b]),a.splice(b,1));for(var d=0,b=c.length;d<b;d++)c[d].dealloc();c.length=0};this.render=function(){if(g){SPP.frameTime=0.001*(Date.now()-d);d=Date.now();for(var c=a.length,f=0;f<c;f++)a[f].render();for(;0<c--;)0>=a[c].life&&(SPP.particlePool.recycle(a[c]),a.splice(c,1));for(c=b.length;0<c--;)0>=b[c].life&&(SPP.forcePool.recycle(b[c]),b.splice(c,1))}};this.start=
function(){d=Date.now();g=!0};this.stop=function(){g=!1};this.destroy=function(){this.stop();g=d=null;for(var e=0,f=a.length;e<f;e++)a[e].dealloc();e=0;for(f=c.length;e<f;e++)c[e].dealloc();e=0;for(f=b.length;e<f;e++)b[e].dealloc();c.length=0;c=null;a.length=0;a=null}};SPP.Zone=function(a,b){this.boundary=a||null;this.type=b||SPP.Zone.OFFSCREEN;this.bounceIntensity=2};SPP.Zone.OFFSCREEN="_offscreen";SPP.Zone.BOUNCE="_bounce";
SPP.Zone.prototype={constructor:SPP.Zone,render:function(a){this[this.type](a)},_bounce:function(a){if(a.position.x<this.boundary.left()||a.position.x>this.boundary.right())a.position.x=a.position.x<this.boundary.left()?this.boundary.left():this.boundary.right(),a.velocity.scaleX(-this.bounceIntensity);if(a.position.y<this.boundary.top()||a.position.y>this.boundary.bottom())a.position.y=a.position.y<this.boundary.top()?this.boundary.top():this.boundary.bottom(),a.velocity.scaleY(-this.bounceIntensity);
a.acceleration.scale(0)},_offscreen:function(a){a.position.x<this.boundary.left()&&(a.position.x=this.boundary.right());a.position.x>this.boundary.right()&&(a.position.x=this.boundary.left());a.position.y<this.boundary.top()&&(a.position.y=this.boundary.bottom());a.position.y>this.boundary.bottom()&&(a.position.y=this.boundary.top())}};SPP.Force=function(){this.value=SPP.VectorPool.get();this.life=Infinity;this.target=null};
SPP.Force.prototype={constructor:SPP.Force,init:function(a,b,c){this.value.reset(a,b);this.life=c||Infinity},render:function(a){this.update(a);return 0>(this.life-=SPP.frameTime)?(this.dispatchEvent(new SPP.Event("dead")),this.reset(),!1):!0},update:function(a){a.acceleration.add(this.value)},dealloc:function(){this.value=null;this.life=void 0;this.target=null},reset:function(){SPP.VectorPool.recycle(this.value);this.life=Infinity;this.target=null}};SPP.extend(SPP.Force.prototype,SPP.EventDispatcher.prototype);SPP.Attraction=function(){SPP.Force.call(this)};SPP.inherit(SPP.Attraction,SPP.Force);SPP.Attraction.prototype.init=function(a,b,c,d){SPP.Force.prototype.init.call(this,0,0,d);this.maxValue=b;this.r=c;this.attractionPosition=a};SPP.Attraction.prototype.update=function(a){this.attractionPosition.distanceTo(a.position)<this.r?this.value.reset(0,0):(this.value.subVectors(this.attractionPosition,a.position),this.value.normalize().setLength(this.maxValue));a.acceleration.add(this.value)};
SPP.Attraction.prototype.dealloc=function(){SPP.Force.prototype.dealloc.apply(this);this.r=this.maxValue=void 0;this.attractionPosition=null};SPP.Attraction.prototype.reset=function(){SPP.Force.prototype.reset.apply(this);this.r=this.maxValue=void 0;this.attractionPosition=null};SPP.Brownian=function(){SPP.Force.call(this)};SPP.inherit(SPP.Brownian,SPP.Force);SPP.Brownian.prototype.init=function(a,b,c){SPP.Force.prototype.init.call(this,0,0,c);this.maxValue=a;this.cycle=b;this.pastTime=0;this.value.reset((2*Math.random()-1)*this.maxValue,(2*Math.random()-1)*this.maxValue)};
SPP.Brownian.prototype.update=function(a){this.pastTime+=SPP.frameTime;this.pastTime>=this.cycle&&(this.value.reset((2*Math.random()-1)*this.maxValue,(2*Math.random()-1)*this.maxValue),this.pastTime=0);a.acceleration.add(this.value)};SPP.Brownian.prototype.dealloc=function(){SPP.Force.prototype.dealloc.apply(this);this.pastTime=this.cycle=this.maxValue=void 0};SPP.Brownian.prototype.reset=function(){SPP.Force.prototype.reset.apply(this);this.pastTime=this.cycle=this.maxValue=void 0};SPP.SimpleBrownian=function(){SPP.Force.call(this)};SPP.inherit(SPP.SimpleBrownian,SPP.Force);SPP.SimpleBrownian.prototype.init=function(a,b){SPP.Force.prototype.init.call(this,0,0,b);this.maxValue=a};SPP.SimpleBrownian.prototype.update=function(a){this.value.reset((2*Math.random()-1)*this.maxValue,(2*Math.random()-1)*this.maxValue);a.acceleration.add(this.value)};SPP.SimpleBrownian.prototype.dealloc=function(){SPP.Force.prototype.dealloc.apply(this);this.cycle=this.maxValue=void 0};
SPP.SimpleBrownian.prototype.reset=function(){SPP.Force.prototype.reset.apply(this);this.cycle=this.maxValue=void 0};SPP.Repulsion=function(){SPP.Force.call(this)};SPP.inherit(SPP.Repulsion,SPP.Force);SPP.Repulsion.prototype.init=function(a,b,c,d){SPP.Force.prototype.init.call(this,0,0,d);this.maxValue=b;this.r=c;this.repulsionPosition=a};SPP.Repulsion.prototype.update=function(a){this.repulsionPosition.distanceTo(a.position)>this.r?this.value.reset(0,0):(this.value.subVectors(a.position,this.repulsionPosition),this.value.normalize().setLength(this.maxValue));a.acceleration.add(this.value)};
SPP.Repulsion.prototype.dealloc=function(){SPP.Force.prototype.dealloc.apply(this);this.r=this.maxValue=void 0;this.repulsionPosition=null};SPP.Repulsion.prototype.reset=function(){SPP.Force.prototype.reset.apply(this);this.r=this.maxValue=void 0;this.repulsionPosition=null};SPP.SpriteImage=function(){SPP.Particle.call(this);this.texture=this.context=null;this.scale=1;this.rotation=0;this.alpha=1;this.regY=this.regX=0.5};SPP.inherit(SPP.SpriteImage,SPP.Particle);
SPP.SpriteImage.prototype.update=function(){null==this.context||null==this.texture||(this.context.save(),this.context.setTransform(1,0,0,1,0,0),this.context.globalAlpha=this.alpha,this.context.translate(this.position.x,this.position.y),this.context.rotate(this.rotation),this.context.scale(this.scale,this.scale),this.context.drawImage(this.texture,0,0,this.texture.width,this.texture.height,-this.texture.width*this.regX,-this.texture.height*this.regY,this.texture.width,this.texture.height),this.context.restore())};
SPP.SpriteImage.prototype.init=function(a,b,c,d,g){SPP.Particle.prototype.init.apply(this,[a,b,c]);this.context=g;this.texture=d};SPP.SpriteImage.prototype.reset=function(){SPP.Particle.prototype.reset.apply(this);this.texture=this.context=null;this.scale=1;this.rotation=0;this.alpha=1;this.regY=this.regX=0.5};SPP.SpriteImage.prototype.width=function(){return this.texture.width*this.scale};SPP.SpriteImage.prototype.height=function(){return this.texture.height*this.scale};