From cd5599bcc36dad561b786762fe522f0892e24f50 Mon Sep 17 00:00:00 2001 From: A-JIE Date: Fri, 3 Jun 2016 19:21:42 +0800 Subject: [PATCH] Proton v2.1.0 update * modification event.js * fix many bug * upgrade new pool * increase USE_CLOCK option turned off by default euler integration --- build/config | 5 +- build/proton-1.0.0.min.js | 10 - build/{proton-1.0.0.js => proton-2.1.0.js} | 1214 +++++++++-------- build/proton-2.1.0.min.js | 10 + example/behaviour/attraction/attraction1.html | 3 +- example/behaviour/attraction/attraction2.html | 3 +- example/behaviour/collision/collision.html | 3 +- .../behaviour/collision/collisionBall.html | 3 +- example/behaviour/color/color.html | 3 +- example/behaviour/custom/circle.html | 3 +- example/behaviour/force/force.html | 3 +- example/behaviour/force/star.html | 3 +- example/behaviour/gravitywell/linebug.html | 3 +- .../behaviour/randomDirft/randomdirft1.html | 3 +- .../behaviour/randomDirft/randomdirft2.html | 15 +- .../behaviour/randomDirft/randomdirft3.html | 3 +- example/behaviour/repulsion/fireflies.html | 3 +- example/behaviour/repulsion/repulsion.html | 3 +- example/behaviour/rotate/rotate.html | 3 +- .../emitter/followEmitter/followEmitter.html | 3 +- example/emitter/followEmitter/mouseDown.html | 3 +- example/game/colliejs/collie.html | 3 +- example/game/crafty/asteroids.html | 3 +- example/game/easeljs/easeljs.html | 3 +- example/game/quarkjs/squirrel.html | 3 +- example/helloworld/emitter/emitter.html | 3 +- example/initialize/imagetarget/Spitfire.html | 3 +- example/initialize/imagetarget/fire2.html | 3 +- .../initialize/imagetarget/imagetarget.html | 3 +- example/lib/config.js | 3 +- example/render/custom/pixijs.html | 11 +- example/render/dom/canvasVSdom.html | 3 +- example/render/dom/domrender.html | 3 +- example/render/dom/jpgmove.html | 3 +- example/render/dom/webglVSdom.html | 3 +- example/render/easeljs/easeljs.html | 3 +- example/render/pixel/google.html | 3 +- example/render/pixel/pixelrender.html | 3 +- example/render/pixel/thousand.html | 3 +- example/render/webgl/webglrender.html | 3 +- example/sparks/bigfire/bigfire.html | 3 +- example/sparks/bomb/bomb.html | 3 +- example/sparks/drugs/drugs.html | 3 +- .../sparks/eightDiagrams/eightDiagrams.html | 3 +- example/sparks/firework/fireworks.html | 9 +- example/sparks/sun/fireball.html | 3 +- example/sparks/sun/sun.html | 3 +- example/zone/circlezone/circlezone.html | 3 +- example/zone/imagezone/imagezone.html | 3 +- example/zone/linezone/bound.html | 3 +- example/zone/pointzone/pointzone.html | 3 +- src/core/ParticlePool.js | 54 - src/core/Pool.js | 75 + src/core/Proton.js | 265 ++-- src/debug/Debug.js | 2 +- src/emitter/Emitter.js | 20 +- src/events/Event.js | 15 - src/events/EventDispatcher.js | 174 ++- src/render/BaseRender.js | 25 +- src/render/EaselRender.js | 107 +- src/render/PixiRender.js | 4 +- src/utils/CanvasUtil.js | 94 +- src/utils/Util.js | 464 ++++--- 63 files changed, 1405 insertions(+), 1300 deletions(-) delete mode 100644 build/proton-1.0.0.min.js rename build/{proton-1.0.0.js => proton-2.1.0.js} (82%) create mode 100644 build/proton-2.1.0.min.js delete mode 100644 src/core/ParticlePool.js create mode 100644 src/core/Pool.js delete mode 100644 src/events/Event.js diff --git a/build/config b/build/config index b1784bb..aafad3b 100644 --- a/build/config +++ b/build/config @@ -1,4 +1,4 @@ -version = 1.0.0 +version = 2.1.0 source_dir = ../src/ output_full = proton-{version}.js output_min = proton-{version}.min.js @@ -20,13 +20,12 @@ files ----- core/Proton.js events/EventDispatcher.js -events/Event.js utils/Util.js utils/WebGLUtil.js utils/DomUtil.js utils/MStack.js core/Particle.js -core/ParticlePool.js +core/Pool.js math/MathUtils.js math/NumericalIntegration.js math/Vector2D.js diff --git a/build/proton-1.0.0.min.js b/build/proton-1.0.0.min.js deleted file mode 100644 index eddc71d..0000000 --- a/build/proton-1.0.0.min.js +++ /dev/null @@ -1,10 +0,0 @@ -/*! - * Proton v1.0.0 - * https://github.com/a-jie/Proton - * - * Copyright 2011-2016, A-JIE - * Licensed under the MIT license - * http://www.opensource.org/licenses/mit-license - * - */ -(function(a,b){function bf(a,b,c,d){bf._super_.call(this),this.reset(a,b,c,d)}function be(a,b,c,d){be._super_.call(this),this.x=a,this.y=b,this.width=c,this.height=d}function bd(a,b){bd._super_.call(this),this.x=a,this.y=b}function bc(a,b,c){bc._super_.call(this),this.x=a,this.y=b,this.radius=c,this.angle=0,this.center={x:this.x,y:this.y}}function bb(a,b,d,e,f){bb._super_.call(this),d-a>=0?(this.x1=a,this.y1=b,this.x2=d,this.y2=e):(this.x1=d,this.y1=e,this.x2=a,this.y2=b),this.dx=this.x2-this.x1,this.dy=this.y2-this.y1,this.minx=Math.min(this.x1,this.x2),this.miny=Math.min(this.y1,this.y2),this.maxx=Math.max(this.x1,this.x2),this.maxy=Math.max(this.y1,this.y2),this.dot=this.x2*this.y1-this.x1*this.y2,this.xxyy=this.dx*this.dx+this.dy*this.dy,this.gradient=this.getGradient(),this.length=this.getLength(),this.direction=c.Util.initValue(f,">")}function ba(){this.vector=new c.Vector2D(0,0),this.random=0,this.crossType="dead",this.alert=!0}function _(a,b){_._super_.call(this,a,b),this.gl=this.element.getContext("experimental-webgl",{antialias:!0,stencil:!1,depth:!1}),this.gl||alert("Sorry your browser do not suppest WebGL!"),this.initVar(),this.setMaxRadius(),this.initShaders(),this.initBuffers(),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA),this.gl.enable(this.gl.BLEND)}function $(a,b,c){$._super_.call(this,a,b),this.context=this.element.getContext("2d"),this.imageData=null,this.rectangle=null,this.rectangle=c,this.createImageData(c)}function Z(a,b){Z._super_.call(this,a,b),this.stroke=null,this.context=this.element.getContext("2d"),this.bufferCache={}}function Y(a,b,c){Y._super_.call(this,a,b),this.stroke=c}function X(a,b){X._super_.call(this,a,b),this.stroke=null}function W(a,b,c){this.proton=a,this.element=b,this.stroke=c}function V(a,b,d){this.element=d,this.type=c.Util.initValue(a,"canvas"),this.proton=b,this.renderer=this.getRenderer()}function T(b,d,e){this.mouseTarget=c.Util.initValue(b,a),this.ease=c.Util.initValue(d,.7),this._allowEmitting=!1,this.initEventHandler(),T._super_.call(this,e)}function S(a){this.selfBehaviours=[],S._super_.call(this,a)}function R(a){this.initializes=[],this.particles=[],this.behaviours=[],this.emitTime=0,this.emitTotalTimes=-1,this.damping=.006,this.bindEmitter=!0,this.rate=new c.Rate(1,.1),R._super_.call(this,a),this.id="emitter_"+R.ID++}function Q(a,b,d,e){Q._super_.call(this,d,e),this.distanceVec=new c.Vector2D,this.centerPoint=c.Util.initValue(a,new c.Vector2D),this.force=c.Util.initValue(this.normalizeValue(b),100),this.name="GravityWell"}function P(a,b,c,d){P._super_.call(this,c,d),this.reset(a,b),this.name="Color"}function O(a,b,c,d,e){O._super_.call(this,d,e),this.reset(a,b,c),this.name="Rotate"}function N(a,b,c,d){N._super_.call(this,c,d),this.reset(a,b),this.name="Scale"}function M(a,b,c,d){M._super_.call(this,c,d),this.reset(a,b),this.name="Alpha"}function L(a,b,c,d){L._super_.call(this,c,d),this.reset(a,b),this.name="CrossZone"}function K(a,b,c,d,e){K._super_.call(this,d,e),this.reset(a,b,c),this.name="Collision"}function J(a,b,c){J._super_.call(this,0,a,b,c),this.name="Gravity"}function I(a,b,c,d,e){I._super_.call(this,a,b,c,d,e),this.force*=-1,this.name="Repulsion"}function H(a,b,c,d,e){H._super_.call(this,d,e),this.reset(a,b,c),this.time=0,this.name="RandomDrift"}function G(a,b,d,e,f){G._super_.call(this,e,f),this.targetPosition=c.Util.initValue(a,new c.Vector2D),this.radius=c.Util.initValue(d,1e3),this.force=c.Util.initValue(this.normalizeValue(b),100),this.radiusSq=this.radius*this.radius,this.attractionForce=new c.Vector2D,this.lengthSq=0,this.name="Attraction"}function F(a,b,d,e){F._super_.call(this,d,e),this.force=this.normalizeForce(new c.Vector2D(a,b)),this.name="Force"}function E(a,b,d){E._super_.call(this),this.image=this.setSpanValue(a),this.w=c.Util.initValue(b,20),this.h=c.Util.initValue(d,this.w)}function D(a,b,d){D._super_.call(this),this.radius=c.Util.setSpanValue(a,b,d)}function C(a,b,d){C._super_.call(this),this.massPan=c.Util.setSpanValue(a,b,d)}function B(a,b,d){B._super_.call(this),this.rPan=c.Util.setSpanValue(a),this.thaPan=c.Util.setSpanValue(b),this.type=c.Util.initValue(d,"vector")}function A(a){A._super_.call(this),this.zone=c.Util.initValue(a,new c.PointZone)}function z(a,b,d){z._super_.call(this),this.lifePan=c.Util.setSpanValue(a,b,d)}function x(){}function w(a,b){this.numPan=c.Util.initValue(a,1),this.timePan=c.Util.initValue(b,1),this.numPan=c.Util.setSpanValue(this.numPan),this.timePan=c.Util.setSpanValue(this.timePan),this.startTime=0,this.nextTime=0,this.init()}function v(a,b){this.id="Behaviour_"+v.id++,this.life=c.Util.initValue(a,Infinity),this.easing=c.ease.setEasingByName(b),this.age=0,this.energy=1,this.dead=!1,this.parents=[],this.name="Behaviour"}function t(a,b,c,d){this.x=a,this.y=b,this.width=c,this.height=d,this.bottom=this.y+this.height,this.right=this.x+this.width}function s(a){c.Util.isArray(a)?this.colorArr=a:this.colorArr=[a]}function r(a,b,d){this.isArray=!1,c.Util.isArray(a)?(this.isArray=!0,this.a=a):(this.a=c.Util.initValue(a,1),this.b=c.Util.initValue(b,this.a),this.center=c.Util.initValue(d,!1))}function l(a,b){this.proParticleCount=c.Util.initValue(a,0),this.releaseTime=c.Util.initValue(b,-1),this.poolList=[],this.timeoutID=0;for(var d=0;d0&&(this.timeoutID=setTimeout(this.release,this.releaseTime/1e3))}function k(a){this.id="particle_"+k.ID++,this.reset(!0),c.Util.setPrototypeByObject(this,a)}function j(){this.mats=[],this.size=0;for(var a=0;a<20;a++)this.mats.push(c.Mat3.create([0,0,0,0,0,0,0,0,0]))}function f(a){this.type=a.type,this.particle=a.particle,this.emitter=a.emitter}function d(){this.initialize()}function c(a,b){this.proParticleCount=c.Util.initValue(a,c.POOL_MAX),this.integrationType=c.Util.initValue(b,c.EULER),this.emitters=[],this.renderers=[],this.time=0,this.oldTime=0,c.pool=new c.ParticlePool(this.proParticleCount),c.integrator=new c.NumericalIntegration(this.integrationType)}c.POOL_MAX=1e3,c.TIME_STEP=60,c.MEASURE=100,c.EULER="euler",c.RK2="runge-kutta2",c.RK4="runge-kutta4",c.VERLET="verlet",c.PARTICLE_CREATED="partilcleCreated",c.PARTICLE_UPDATE="partilcleUpdate",c.PARTICLE_SLEEP="particleSleep",c.PARTICLE_DEAD="partilcleDead",c.PROTON_UPDATE="protonUpdate",c.PROTON_UPDATE_AFTER="protonUpdateAfter",c.EMITTER_ADDED="emitterAdded",c.EMITTER_REMOVED="emitterRemoved",c.amendChangeTabsBug=!0,c.TextureBuffer={},c.TextureCanvasBuffer={},c.prototype={addRender:function(a){a.proton=this,this.renderers.push(a.proton)},addEmitter:function(a){this.emitters.push(a),a.parent=this,this.dispatchEvent(new c.Event({type:c.EMITTER_ADDED,emitter:a}))},removeEmitter:function(a){var b=this.emitters.indexOf(a);this.emitters.splice(b,1),a.parent=null,this.dispatchEvent(new c.Event({type:c.EMITTER_REMOVED,emitter:a}))},update:function(){this.dispatchEvent(new c.Event({type:c.PROTON_UPDATE})),this.oldTime||(this.oldTime=(new Date).getTime());var a=(new Date).getTime();this.elapsed=(a-this.oldTime)/1e3,c.amendChangeTabsBug&&this.amendChangeTabsBug(),this.oldTime=a;if(this.elapsed>0)for(var b=0;b.5&&(this.oldTime=(new Date).getTime(),this.elapsed=0)},getCount:function(){var a=0,b=this.emitters.length;for(var c=0;c>b;return a+1},makeTranslation:function(a,b){return[1,0,0,0,1,0,a,b,1]},makeRotation:function(a){var b=Math.cos(a),c=Math.sin(a);return[b,-c,0,c,b,0,0,0,1]},makeScale:function(a,b){return[a,0,0,0,b,0,0,0,1]},matrixMultiply:function(a,b){var c=a[0],d=a[1],e=a[2],f=a[3],g=a[4],h=a[5],i=a[6],j=a[7],k=a[8],l=b[0],m=b[1],n=b[2],o=b[3],p=b[4],q=b[5],r=b[6],s=b[7],t=b[8];return[c*l+d*o+e*r,c*m+d*p+e*s,c*n+d*q+e*t,f*l+g*o+h*r,f*m+g*p+h*s,f*n+g*q+h*t,i*l+j*o+k*r,i*m+j*p+k*s,i*n+j*q+k*t]}};c.WebGLUtil=h;var i=i||{createCanvas:function(a,b,c,d){var e=document.createElement("canvas"),f=d?d:"absolute";e.id=a,e.width=b,e.height=c,e.style.position=f,e.style.opacity=0,this.transformDom(e,-500,-500,0,0);return e},transformDom:function(a,b,c,d,e){a.style.WebkitTransform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)",a.style.MozTransform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)",a.style.OTransform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)",a.style.msTransform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)",a.style.transform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)"}};c.DomUtil=i,j.prototype.set=function(a,b){b==0?c.Mat3.set(a,this.mats[0]):c.Mat3.multiply(this.mats[b-1],a,this.mats[b]),this.size=Math.max(this.size,b+1)},j.prototype.push=function(a){this.size==0?c.Mat3.set(a,this.mats[0]):c.Mat3.multiply(this.mats[this.size-1],a,this.mats[this.size]),this.size++},j.prototype.pop=function(){this.size>0&&this.size--},j.prototype.top=function(){return this.mats[this.size-1]},c.MStack=j,k.ID=0,k.prototype={getDirection:function(){return Math.atan2(this.v.x,-this.v.y)*(180/Math.PI)},reset:function(a){this.life=Infinity,this.age=0,this.energy=1,this.dead=!1,this.sleep=!1,this.target=null,this.sprite=null,this.parent=null,this.mass=1,this.radius=10,this.alpha=1,this.scale=1,this.rotation=0,this.color=null,this.easing=c.ease.setEasingByName(c.easeLinear),a?(this.transform={},this.p=new c.Vector2D,this.v=new c.Vector2D,this.a=new c.Vector2D,this.old={p:new c.Vector2D,v:new c.Vector2D,a:new c.Vector2D},this.behaviours=[]):(c.Util.destroyObject(this.transform),this.p.set(0,0),this.v.set(0,0),this.a.set(0,0),this.old.p.set(0,0),this.old.v.set(0,0),this.old.a.set(0,0),this.removeAllBehaviours()),this.transform.rgb={r:255,g:255,b:255};return this},update:function(a,b){if(!this.sleep){this.age+=a;var c=this.behaviours.length,d;for(d=0;d=this.life)this.destroy();else{var e=this.easing(this.age/this.life);this.energy=Math.max(1-e,0)}},addBehaviour:function(a){this.behaviours.push(a),a.hasOwnProperty("parents")&&a.parents.push(this),a.initialize(this)},addBehaviours:function(a){var b=a.length,c;for(c=0;c-1){var a=this.behaviours.splice(b,1);a.parents=null}},removeAllBehaviours:function(){c.Util.destroyArray(this.behaviours)},destroy:function(){this.removeAllBehaviours(),this.energy=0,this.dead=!0,this.parent=null}},c.Particle=k,l.prototype={create:function(a){return a?new newTypeParticle:new c.Particle},getCount:function(){return this.poolList.length},add:function(){return this.poolList.push(this.create())},get:function(){return this.poolList.length===0?this.create():this.poolList.pop().reset()},set:function(a){if(this.poolList.length0)return Math.PI/2;if(this.y<0)return-Math.PI/2},getComponent:function(a){switch(a){case 0:return this.x;case 1:return this.y;default:throw new Error("index is out of range: "+a)}},copy:function(a){this.x=a.x,this.y=a.y;return this},add:function(a,c){if(c!==b)return this.addVectors(a,c);this.x+=a.x,this.y+=a.y;return this},addXY:function(a,b){this.x+=a,this.y+=b;return this},addVectors:function(a,b){this.x=a.x+b.x,this.y=a.y+b.y;return this},addScalar:function(a){this.x+=a,this.y+=a;return this},sub:function(a,c){if(c!==b)return this.subVectors(a,c);this.x-=a.x,this.y-=a.y;return this},subVectors:function(a,b){this.x=a.x-b.x,this.y=a.y-b.y;return this},multiplyScalar:function(a){this.x*=a,this.y*=a;return this},divideScalar:function(a){a!==0?(this.x/=a,this.y/=a):this.set(0,0);return this},min:function(a){this.x>a.x&&(this.x=a.x),this.y>a.y&&(this.y=a.y);return this},max:function(a){this.xb.x&&(this.x=b.x),this.yb.y&&(this.y=b.y);return this},negate:function(){return this.multiplyScalar(-1)},dot:function(a){return this.x*a.x+this.y*a.y},lengthSq:function(){return this.x*this.x+this.y*this.y},length:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},normalize:function(){return this.divideScalar(this.length())},distanceTo:function(a){return Math.sqrt(this.distanceToSquared(a))},rotate:function(a){var b=this.x,c=this.y;this.x=b*Math.cos(a)+c*Math.sin(a),this.y=-b*Math.sin(a)+c*Math.cos(a);return this},distanceToSquared:function(a){var b=this.x-a.x,c=this.y-a.y;return b*b+c*c},setLength:function(a){var b=this.length();b!==0&&a!==b&&this.multiplyScalar(a/b);return this},lerp:function(a,b){this.x+=(a.x-this.x)*b,this.y+=(a.y-this.y)*b;return this},equals:function(a){return a.x===this.x&&a.y===this.y},toArray:function(){return[this.x,this.y]},clear:function(){this.x=0,this.y=0;return this},clone:function(){return new c.Vector2D(this.x,this.y)}},c.Vector2D=p;var q=function(a,b){this.r=Math.abs(a)||0,this.tha=b||0};q.prototype={set:function(a,b){this.r=a,this.tha=b;return this},setR:function(a){this.r=a;return this},setTha:function(a){this.tha=a;return this},copy:function(a){this.r=a.r,this.tha=a.tha;return this},toVector:function(){return new c.Vector2D(this.getX(),this.getY())},getX:function(){return this.r*Math.sin(this.tha)},getY:function(){return-this.r*Math.cos(this.tha)},normalize:function(){this.r=1;return this},equals:function(a){return a.r===this.r&&a.tha===this.tha},toArray:function(){return[this.r,this.tha]},clear:function(){this.r=0,this.tha=0;return this},clone:function(){return new c.Polar2D(this.r,this.tha)}},c.Polar2D=q,r.prototype={getValue:function(a){return this.isArray?this.a[Math.floor(this.a.length*Math.random())]:this.center?c.MathUtils.randomFloating(this.a,this.b,a):c.MathUtils.randomAToB(this.a,this.b,a)}},c.Span=r,c.getSpan=function(a,b,d){return new c.Span(a,b,d)},c.Util.inherits(s,c.Span),s.prototype.getValue=function(){var a=this.colorArr[Math.floor(this.colorArr.length*Math.random())];return a=="random"||a=="Random"?c.MathUtils.randomColor():a},c.ColorSpan=s,t.prototype={contains:function(a,b){return a<=this.right&&a>=this.x&&b<=this.bottom&&b>=this.y?!0:!1}},c.Rectangle=t;var u=u||{create:function(a){var b=new Float32Array(9);a&&this.set(a,b);return b},set:function(a,b){for(var c=0;c<9;c++)b[c]=a[c];return b},multiply:function(a,b,c){var d=a[0],e=a[1],f=a[2],g=a[3],h=a[4],i=a[6],j=a[7],k=b[0],l=b[1],m=b[2],n=b[3],o=b[4],p=b[6],q=b[7];c[0]=k*d+l*g,c[1]=k*e+l*h,c[2]=f*m,c[3]=n*d+o*g,c[4]=n*e+o*h,c[6]=p*d+q*g+i,c[7]=p*e+q*h+j;return c},inverse:function(a,b){var c=a[0],d=a[1],e=a[3],f=a[4],g=a[6],h=a[7],i=f,j=-e,k=h*e-f*g,l=c*i+d*j,m;m=1/l,b[0]=i*m,b[1]=-d*m,b[3]=j*m,b[4]=c*m,b[6]=k*m,b[7]=(-h*c+d*g)*m;return b},multiplyVec2:function(a,b,c){var d=b[0],e=b[1];c[0]=d*a[0]+e*a[3]+a[6],c[1]=d*a[1]+e*a[4]+a[7];return c}};c.Mat3=u,v.id=0,v.prototype={reset:function(a,b){this.life=c.Util.initValue(a,Infinity),this.easing=c.Util.initValue(b,c.ease.setEasingByName(c.easeLinear))},normalizeForce:function(a){return a.multiplyScalar(c.MEASURE)},normalizeValue:function(a){return a*c.MEASURE},initialize:function(a){},applyBehaviour:function(a,b,c){this.age+=b;if(this.age>=this.life||this.dead)this.energy=0,this.dead=!0,this.destroy();else{var d=this.easing(a.age/a.life);this.energy=Math.max(1-d,0)}},destroy:function(){var a,b=this.parents.length,c;for(c=0;c=this.nextTime){this.startTime=0,this.nextTime=this.timePan.getValue();return this.numPan.b==1?this.numPan.getValue(!1)>.5?1:0:this.numPan.getValue(!0)}return 0}},c.Rate=w,x.prototype.reset=function(){},x.prototype.init=function(a,b){b?this.initialize(b):this.initialize(a)},x.prototype.initialize=function(a){},c.Initialize=x;var y={initialize:function(a,b,d){var e=d.length,f;for(f=0;f4e-6&&this.lengthSq=this.delay&&(a.a.addXY(c.MathUtils.randomAToB(-this.panFoce.x,this.panFoce.x),c.MathUtils.randomAToB(-this.panFoce.y,this.panFoce.y)),this.time=0)},c.RandomDrift=H,c.Util.inherits(I,c.Attraction),I.prototype.reset=function(a,b,c,d,e){I._super_.prototype.reset.call(this,a,b,c,d,e),this.force*=-1},c.Repulsion=I,c.Util.inherits(J,c.Force),J.prototype.reset=function(a,b,c){J._super_.prototype.reset.call(this,0,a,b,c)},c.Gravity=J,c.G=J,c.Util.inherits(K,c.Behaviour),K.prototype.reset=function(a,b,d,e,f){this.emitter=c.Util.initValue(a,null),this.mass=c.Util.initValue(b,!0),this.callback=c.Util.initValue(d,null),this.collisionPool=[],this.delta=new c.Vector2D,e&&K._super_.prototype.reset.call(this,e,f)},K.prototype.applyBehaviour=function(a,b,c){var d=this.emitter?this.emitter.particles.slice(c):this.pool.slice(c),e,f,g,h,i,j=d.length;for(var k=0;k-1&&this.initializes.splice(b,1)},R.prototype.removeInitializers=function(){c.Util.destroyArray(this.initializes)},R.prototype.addBehaviour=function(){var a=arguments.length,b;for(b=0;b-1&&this.behaviours.splice(b,1)},R.prototype.removeAllBehaviours=function(){c.Util.destroyArray(this.behaviours)},R.prototype.integrate=function(a){var b=1-this.damping;c.integrator.integrate(this,a,b);var d=this.particles.length,e;for(e=0;e=this.life||this.dead)&&this.destroy(),this.emitting(a),this.integrate(a);var b,d=this.particles.length,e;for(e=d-1;e>=0;e--)b=this.particles[e],b.dead&&(c.pool.set(b),this.particles.splice(e,1),this.dispatchEvent(new c.Event({type:c.PARTICLE_DEAD,particle:b})))},R.prototype.setupParticle=function(a,b,d){var e=this.initializes,f=this.behaviours;b&&(b instanceof Array?e=b:e=[b]),d&&(d instanceof Array?f=d:f=[d]),c.InitializeUtil.initialize(this,a,e),a.addBehaviours(f),a.parent=this,this.particles.push(a)},R.prototype.destroy=function(){this.dead=!0,this.emitTotalTimes=-1,this.particles.length==0&&(this.removeInitializers(),this.removeAllBehaviours(),this.parent&&this.parent.removeEmitter(this))},c.Emitter=R,c.Util.inherits(S,c.Emitter),S.prototype.addSelfBehaviour=function(){var a=arguments.length,b;for(b=0;b-1&&this.selfBehaviours.splice(b,1)},S.prototype.update=function(a){S._super_.prototype.update.call(this,a);if(!this.sleep){var b=this.selfBehaviours.length,c;for(c=0;cthis.element.width||c<0||c>this.elementwidth)){var f=((c>>0)*a.width+(b>>0))*4;a.data[f]=e.r,a.data[f+1]=e.g,a.data[f+2]=e.b,a.data[f+3]=d.alpha*255}},$.prototype.onParticleDead=function(a){},c.PixelRender=$,c.Util.inherits(_,c.BaseRender),_.prototype.resize=function(a,b){this.umat[4]=-2,this.umat[7]=1,this.smat[0]=1/a,this.smat[4]=1/b,this.mstack.set(this.umat,0),this.mstack.set(this.smat,1),this.gl.viewport(0,0,a,b),this.element.width=a,this.element.height=b},_.prototype.setMaxRadius=function(a){this.circleCanvasURL=this.createCircle(a)},_.prototype.getVertexShader=function(){var a=["uniform vec2 viewport;","attribute vec2 aVertexPosition;","attribute vec2 aTextureCoord;","uniform mat3 tMat;","varying vec2 vTextureCoord;","varying float alpha;","void main() {","vec3 v = tMat * vec3(aVertexPosition, 1.0);","gl_Position = vec4(v.x, v.y, 0, 1);","vTextureCoord = aTextureCoord;","alpha = tMat[0][2];","}"].join("\n");return a},_.prototype.getFragmentShader=function(){var a=["precision mediump float;","varying vec2 vTextureCoord;","varying float alpha;","uniform sampler2D uSampler;","uniform vec4 color;","uniform bool useTexture;","uniform vec3 uColor;","void main() {","vec4 textureColor = texture2D(uSampler, vTextureCoord);","gl_FragColor = textureColor * vec4(uColor, 1.0);","gl_FragColor.w *= alpha;","}"].join("\n");return a},_.prototype.initVar=function(){this.mstack=new c.MStack,this.umat=c.Mat3.create([2,0,1,0,-2,0,-1,1,1]),this.smat=c.Mat3.create([.01,0,1,0,.01,0,0,0,1]),this.texturebuffers={}},_.prototype.start=function(){_._super_.prototype.start.call(this),this.resize(this.element.width,this.element.height)},_.prototype.blendEquation=function(a){this.gl.blendEquation(this.gl[a])},_.prototype.blendFunc=function(a,b){this.gl.blendFunc(this.gl[a],this.gl[b])},_.prototype.getShader=function(a,b,c){var d;c?d=a.createShader(a.FRAGMENT_SHADER):d=a.createShader(a.VERTEX_SHADER),a.shaderSource(d,b),a.compileShader(d);if(!a.getShaderParameter(d,a.COMPILE_STATUS)){alert(a.getShaderInfoLog(d));return null}return d},_.prototype.initShaders=function(){var a=this.getShader(this.gl,this.getFragmentShader(),!0),b=this.getShader(this.gl,this.getVertexShader(),!1);this.sprogram=this.gl.createProgram(),this.gl.attachShader(this.sprogram,b),this.gl.attachShader(this.sprogram,a),this.gl.linkProgram(this.sprogram),this.gl.getProgramParameter(this.sprogram,this.gl.LINK_STATUS)||alert("Could not initialise shaders"),this.gl.useProgram(this.sprogram),this.sprogram.vpa=this.gl.getAttribLocation(this.sprogram,"aVertexPosition"),this.sprogram.tca=this.gl.getAttribLocation(this.sprogram,"aTextureCoord"),this.gl.enableVertexAttribArray(this.sprogram.tca),this.gl.enableVertexAttribArray(this.sprogram.vpa),this.sprogram.tMatUniform=this.gl.getUniformLocation(this.sprogram,"tMat"),this.sprogram.samplerUniform=this.gl.getUniformLocation(this.sprogram,"uSampler"),this.sprogram.useTex=this.gl.getUniformLocation(this.sprogram,"useTexture"),this.sprogram.color=this.gl.getUniformLocation(this.sprogram,"uColor"),this.gl.uniform1i(this.sprogram.useTex,1)},_.prototype.initBuffers=function(){this.unitIBuffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.unitIBuffer);var a=[0,3,1,0,2,3];this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(a),this.gl.STATIC_DRAW);var b=[];for(var c=0;c<100;c++)b.push(c);idx=new Uint16Array(b),this.unitI33=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.unitI33),this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER,idx,this.gl.STATIC_DRAW),b=[];for(c=0;c<100;c++)b.push(c,c+1,c+2);idx=new Uint16Array(b),this.stripBuffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.stripBuffer),this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER,idx,this.gl.STATIC_DRAW)},_.prototype.createCircle=function(a){this.circleCanvasRadius=c.WebGLUtil.nhpot(c.Util.initValue(a,32));var b=c.DomUtil.createCanvas("circle_canvas",this.circleCanvasRadius*2,this.circleCanvasRadius*2),d=b.getContext("2d");d.beginPath(),d.arc(this.circleCanvasRadius,this.circleCanvasRadius,this.circleCanvasRadius,0,Math.PI*2,!0),d.closePath(),d.fillStyle="#FFF",d.fill();return b.toDataURL()},_.prototype.setImgInCanvas=function(a){var b=a.target.width,d=a.target.height,e=c.WebGLUtil.nhpot(a.target.width),f=c.WebGLUtil.nhpot(a.target.height),g=a.target.width/e,h=a.target.height/f;this.texturebuffers[a.transform.src]||(this.texturebuffers[a.transform.src]=[this.gl.createTexture(),this.gl.createBuffer(),this.gl.createBuffer()]),a.transform.texture=this.texturebuffers[a.transform.src][0],a.transform.vcBuffer=this.texturebuffers[a.transform.src][1],a.transform.tcBuffer=this.texturebuffers[a.transform.src][2],this.gl.bindBuffer(this.gl.ARRAY_BUFFER,a.transform.tcBuffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array([0,0,g,0,0,h,h,h]),this.gl.STATIC_DRAW),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,a.transform.vcBuffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array([0,0,b,0,0,d,b,d]),this.gl.STATIC_DRAW);var i=a.transform.canvas.getContext("2d"),j=i.getImageData(0,0,e,f);this.gl.bindTexture(this.gl.TEXTURE_2D,a.transform.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,j),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR_MIPMAP_NEAREST),this.gl.generateMipmap(this.gl.TEXTURE_2D),a.transform.textureLoaded=!0,a.transform.textureWidth=b,a.transform.textureHeight=d},_.prototype.setStroke=function(a,b){},_.prototype.onProtonUpdate=function(){},_.prototype.onParticleCreated=function(a){var b=this;a.transform.textureLoaded=!1,a.transform.tmat=c.Mat3.create(),a.transform.tmat[8]=1,a.transform.imat=c.Mat3.create(),a.transform.imat[8]=1,a.target?c.Util.getImage(a.target,a,!0,function(a){b.setImgInCanvas.call(b,a),a.transform.oldScale=1}):c.Util.getImage(this.circleCanvasURL,a,!0,function(a){b.setImgInCanvas.call(b,a),a.transform.oldScale=a.radius/b.circleCanvasRadius})},_.prototype.onParticleUpdate=function(a){a.transform.textureLoaded&&(this.updateMatrix(a),this.gl.uniform3f(this.sprogram.color,a.transform.rgb.r/255,a.transform.rgb.g/255,a.transform.rgb.b/255),this.gl.uniformMatrix3fv(this.sprogram.tMatUniform,!1,this.mstack.top()),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,a.transform.vcBuffer),this.gl.vertexAttribPointer(this.sprogram.vpa,2,this.gl.FLOAT,!1,0,0),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,a.transform.tcBuffer),this.gl.vertexAttribPointer(this.sprogram.tca,2,this.gl.FLOAT,!1,0,0),this.gl.bindTexture(this.gl.TEXTURE_2D,a.transform.texture),this.gl.uniform1i(this.sprogram.samplerUniform,0),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.unitIBuffer),this.gl.drawElements(this.gl.TRIANGLES,6,this.gl.UNSIGNED_SHORT,0),this.mstack.pop())},_.prototype.onParticleDead=function(a){},_.prototype.updateMatrix=function(a){var b=c.WebGLUtil.makeTranslation(-a.transform.textureWidth/2,-a.transform.textureHeight/2),d=c.WebGLUtil.makeTranslation(a.p.x,a.p.y),e=a.rotation*(Math.PI/180),f=c.WebGLUtil.makeRotation(e),g=a.scale*a.transform.oldScale,h=c.WebGLUtil.makeScale(g,g),i=c.WebGLUtil.matrixMultiply(b,h);i=c.WebGLUtil.matrixMultiply(i,f),i=c.WebGLUtil.matrixMultiply(i,d),c.Mat3.inverse(i,a.transform.imat),i[2]=a.alpha,this.mstack.push(i)},c.WebGLRender=_,ba.prototype={getPosition:function(){},crossing:function(a){}},c.Zone=ba,c.Util.inherits(bb,c.Zone),bb.prototype.getPosition=function(){this.random=Math.random(),this.vector.x=this.x1+this.random*this.length*Math.cos(this.gradient),this.vector.y=this.y1+this.random*this.length*Math.sin(this.gradient);return this.vector},bb.prototype.getDirection=function(a,b){var c=this.dy,d=-this.dx,e=this.dot,f=d==0?1:d;return(c*a+d*b+e)*f>0?!0:!1},bb.prototype.getDistance=function(a,b){var c=this.dy,d=-this.dx,e=this.dot,f=c*a+d*b+e;return f/Math.sqrt(this.xxyy)},bb.prototype.getSymmetric=function(a){var b=a.getGradient(),c=this.getGradient(),d=2*(c-b),e=a.x,f=a.y;a.x=e*Math.cos(d)-f*Math.sin(d),a.y=e*Math.sin(d)+f*Math.cos(d);return a},bb.prototype.getGradient=function(){return Math.atan2(this.dy,this.dx)},bb.prototype.getRange=function(a,b){var c=Math.abs(this.getGradient());c<=Math.PI/4?a.p.xthis.minx&&b():a.p.ythis.miny&&b()},bb.prototype.getLength=function(){return Math.sqrt(this.dx*this.dx+this.dy*this.dy)},bb.prototype.crossing=function(a){var b=this;this.crossType=="dead"?this.direction==">"||this.direction=="R"||this.direction=="right"||this.direction=="down"?this.getRange(a,function(){b.getDirection(a.p.x,a.p.y)&&(a.dead=!0)}):this.getRange(a,function(){b.getDirection(a.p.x,a.p.y)||(a.dead=!0)}):this.crossType=="bound"?this.getRange(a,function(){b.getDistance(a.p.x,a.p.y)<=a.radius&&(b.dx==0?a.v.x*=-1:b.dy==0?a.v.y*=-1:b.getSymmetric(a.v))}):this.crossType=="cross"&&this.alert&&(alert("Sorry lineZone does not support cross method"),this.alert=!1)},c.LineZone=bb,c.Util.inherits(bc,c.Zone),bc.prototype.getPosition=function(){this.random=Math.random(),this.angle=Math.PI*2*Math.random(),this.vector.x=this.x+this.random*this.radius*Math.cos(this.angle),this.vector.y=this.y+this.random*this.radius*Math.sin(this.angle);return this.vector},bc.prototype.setCenter=function(a,b){this.center.x=a,this.center.y=b},bc.prototype.crossing=function(a){var b=a.p.distanceTo(this.center);this.crossType=="dead"?b-a.radius>this.radius&&(a.dead=!0):this.crossType=="bound"?b+a.radius>=this.radius&&this.getSymmetric(a):this.crossType=="cross"&&this.alert&&(alert("Sorry CircleZone does not support cross method"),this.alert=!1)},bc.prototype.getSymmetric=function(a){var b=a.v.getGradient(),c=this.getGradient(a),d=2*(c-b),e=a.v.x,f=a.v.y;a.v.x=e*Math.cos(d)-f*Math.sin(d),a.v.y=e*Math.sin(d)+f*Math.cos(d)},bc.prototype.getGradient=function(a){return-Math.PI/2+Math.atan2(a.p.y-this.center.y,a.p.x-this.center.x)},c.CircleZone=bc,c.Util.inherits(bd,c.Zone),bd.prototype.getPosition=function(){this.vector.x=this.x,this.vector.y=this.y;return this.vector},bd.prototype.crossing=function(a){this.alert&&(alert("Sorry PointZone does not support crossing method"),this.alert=!1)},c.PointZone=bd,c.Util.inherits(be,c.Zone),be.prototype.getPosition=function(){this.vector.x=this.x+Math.random()*this.width,this.vector.y=this.y+Math.random()*this.height;return this.vector},be.prototype.crossing=function(a){this.crossType=="dead"?(a.p.x+a.radiusthis.x+this.width&&(a.dead=!0),a.p.y+a.radiusthis.y+this.height&&(a.dead=!0)):this.crossType=="bound"?(a.p.x-a.radiusthis.x+this.width&&(a.p.x=this.x+this.width-a.radius,a.v.x*=-1),a.p.y-a.radiusthis.y+this.height&&(a.p.y=this.y+this.height-a.radius,a.v.y*=-1)):this.crossType=="cross"&&(a.p.x+a.radiusthis.x+this.width&&a.v.x>=0&&(a.p.x=this.x-a.radius),a.p.y+a.radiusthis.y+this.height&&a.v.y>=0&&(a.p.y=this.y-a.radius))},c.RectZone=be,c.Util.inherits(bf,c.Zone),bf.prototype.reset=function(a,b,d,e){this.imageData=a,this.x=c.Util.initValue(b,0),this.y=c.Util.initValue(d,0),this.d=c.Util.initValue(e,2),this.vectors=[],this.setVectors()},bf.prototype.setVectors=function(){var a,b,c=this.imageData.width,d=this.imageData.height;for(a=0;a>0)*c+(a>>0))*4;this.imageData.data[e+3]>0&&this.vectors.push({x:a+this.x,y:b+this.y})}return this.vector},bf.prototype.getBound=function(a,b){var c=((b>>0)*this.imageData.width+(a>>0))*4;return this.imageData.data[c+3]>0?!0:!1},bf.prototype.getPosition=function(){return this.vector.copy(this.vectors[Math.floor(Math.random()*this.vectors.length)])},bf.prototype.getColor=function(a,b){a-=this.x,b-=this.y;var c=((b>>0)*this.imageData.width+(a>>0))*4;return{r:this.imageData.data[c],g:this.imageData.data[c+1],b:this.imageData.data[c+2],a:this.imageData.data[c+3]}},bf.prototype.crossing=function(a){this.crossType=="dead"?this.getBound(a.p.x-this.x,a.p.y-this.y)?a.dead=!0:a.dead=!1:this.crossType=="bound"&&(this.getBound(a.p.x-this.x,a.p.y-this.y)||a.v.negate())},c.ImageZone=bf;var bg=function(){if(a.console&&a.console.log){var b=arguments;if(typeof arguments[0]=="string")if(arguments[0].indexOf("+")==0){var c=parseInt(arguments[0]);bg.once 0) { - for (var i = 0; i < this.emitters.length; i++) { - this.emitters[i].update(this.elapsed); - } - } - - this.dispatchEvent(new Proton.Event({ - type : Proton.PROTON_UPDATE_AFTER - })); - }, - - amendChangeTabsBug : function() { - if (this.elapsed > .5) { - this.oldTime = new Date().getTime(); - this.elapsed = 0; - } - }, - - getCount : function() { - var total = 0; - var length = this.emitters.length; - for (var i = 0; i < length; i++) { - total += this.emitters[i].particles.length; - } - return total; - }, - - destroy : function() { - var length = this.emitters.length; - for (var i = 0; i < length; i++) { - this.emitters[i].destroy(); - delete this.emitters[i]; - } - - this.emitters = []; - this.time = 0; - this.oldTime = 0; - Proton.pool.release(); - } - }; - - window.Proton = Proton; + //the max particle number in pool + Proton.POOL_MAX = 1000; + Proton.TIME_STEP = 60; + Proton.USE_CLOCK = false; + //1:100 + Proton.MEASURE = 100; + Proton.EULER = 'euler'; + Proton.RK2 = 'runge-kutta2'; + Proton.RK4 = 'runge-kutta4'; + Proton.VERLET = 'verlet'; + + Proton.PARTICLE_CREATED = 'partilcleCreated'; + Proton.PARTICLE_UPDATE = 'partilcleUpdate'; + Proton.PARTICLE_SLEEP = 'particleSleep'; + Proton.PARTICLE_DEAD = 'partilcleDead'; + Proton.PROTON_UPDATE = 'protonUpdate'; + Proton.PROTON_UPDATE_AFTER = 'protonUpdateAfter'; + Proton.EMITTER_ADDED = 'emitterAdded'; + Proton.EMITTER_REMOVED = 'emitterRemoved'; + + Proton.amendChangeTabsBug = true; + Proton.TextureBuffer = {}; + Proton.TextureCanvasBuffer = {}; + + /** + * Proton is a html5 particle engine + * + * @class Proton + * @constructor + */ + function Proton(proParticleCount, integrationType) { + this.integrationType = Proton.Util.initValue(integrationType, Proton.EULER); + this.emitters = []; + this.renderers = []; + this.time = 0; + this.oldTime = 0; + + Proton.pool = new Proton.Pool(100); + Proton.integrator = new Proton.NumericalIntegration(this.integrationType); + } + + + Proton.prototype = { + /** + * add a type of Renderer + * + * @method addRender + * @param {Renderer} render + */ + addRender: function(render) { + render.proton = this; + this.renderers.push(render.proton); + }, + /** + * add the Emitter + * + * @method addEmitter + * @param {Emitter} emitter + */ + addEmitter: function(emitter) { + this.emitters.push(emitter); + emitter.parent = this; + + this.dispatchEvent(Proton.EMITTER_ADDED, emitter); + }, + + removeEmitter: function(emitter) { + var index = this.emitters.indexOf(emitter); + this.emitters.splice(index, 1); + emitter.parent = null; + + this.dispatchEvent(Proton.EMITTER_REMOVED, emitter); + }, + + update: function() { + this.dispatchEvent(Proton.PROTON_UPDATE); + + if (Proton.USE_CLOCK) { + if (!this.oldTime) + this.oldTime = new Date().getTime(); + + var time = new Date().getTime(); + this.elapsed = (time - this.oldTime) / 1000; + if (Proton.amendChangeTabsBug) + this.amendChangeTabsBug(); + this.oldTime = time; + } else { + this.elapsed = 0.0167; + } + + if (this.elapsed > 0) { + for (var i = 0; i < this.emitters.length; i++) { + this.emitters[i].update(this.elapsed); + } + } + + this.dispatchEvent(Proton.PROTON_UPDATE_AFTER); + }, + + amendChangeTabsBug: function() { + if (this.elapsed > .5) { + this.oldTime = new Date().getTime(); + this.elapsed = 0; + } + }, + + getCount: function() { + var total = 0; + var length = this.emitters.length; + for (var i = 0; i < length; i++) { + total += this.emitters[i].particles.length; + } + return total; + }, + + destroy: function() { + var length = this.emitters.length; + for (var i = 0; i < length; i++) { + this.emitters[i].destroy(); + delete this.emitters[i]; + } + + this.emitters = []; + this.time = 0; + this.oldTime = 0; + Proton.pool.release(); + } + }; + + window.Proton = Proton; /* @@ -152,330 +147,344 @@ * **/ - function EventDispatcher() { - this.initialize(); - }; - - var p = EventDispatcher.prototype; - - EventDispatcher.initialize = function(target) { - target.addEventListener = p.addEventListener; - target.removeEventListener = p.removeEventListener; - target.removeAllEventListeners = p.removeAllEventListeners; - target.hasEventListener = p.hasEventListener; - target.dispatchEvent = p.dispatchEvent; - }; - - p._listeners = null; - - p.initialize = function() { - }; - - p.addEventListener = function(type, listener) { - var listeners = this._listeners; - if (!listeners) { - listeners = this._listeners = {}; - } else { - this.removeEventListener(type, listener); - } - var arr = listeners[type]; - if (!arr) { - arr = listeners[type] = []; - } - arr.push(listener); - return listener; - }; - - p.removeEventListener = function(type, listener) { - var listeners = this._listeners; - if (!listeners) { - return; - } - var arr = listeners[type]; - if (!arr) { - return; - } - for (var i = 0, l = arr.length; i < l; i++) { - if (arr[i] == listener) { - if (l == 1) { - delete (listeners[type]); - }// allows for faster checks. - else { - arr.splice(i, 1); - } - break; - } - } - }; - - p.removeAllEventListeners = function(type) { - if (!type) { - this._listeners = null; - } else if (this._listeners) { - delete (this._listeners[type]); - } - }; - - p.dispatchEvent = function(eventObj) { - var ret = false, listeners = this._listeners; - if (eventObj && listeners) { - var arr = listeners[eventObj.type]; - if (!arr) - return ret; - - arr = arr.slice(); - // to avoid issues with items being removed or added during the dispatch - for (var i = 0, l = arr.length; i < l; i++) { - var o = arr[i]; - ret = ret || o(eventObj); - } - } - return !!ret; - }; - - p.hasEventListener = function(type) { - var listeners = this._listeners; - return !!(listeners && listeners[type]); - }; - - Proton.EventDispatcher = EventDispatcher; - Proton.EventDispatcher.initialize(Proton.prototype); - - - - function Event(pObj) { - this.type = pObj['type']; - this.particle = pObj['particle']; - this.emitter = pObj['emitter']; - } - - - Event.PARTICLE_CREATED = Proton.PARTICLE_CREATED; - Event.PARTICLE_UPDATA = Proton.PARTICLE_UPDATA; - Event.PARTICLE_SLEEP = Proton.PARTICLE_SLEEP; - Event.PARTICLE_DEAD = Proton.PARTICLE_DEAD; - - Proton.Event = Event; - - - - var Util = Util || { - initValue : function(value, defaults) { - var value = (value != null && value != undefined) ? value : defaults; - return value; - }, - - isArray : function(value) { - return typeof value === 'object' && value.hasOwnProperty('length'); - }, - - destroyArray : function(array) { - array.length = 0; - }, - - destroyObject : function(obj) { - for (var o in obj) - delete obj[o]; - }, - - getVector2D : function(postionOrX, y) { - if ( typeof (postionOrX) == 'object') { - return postionOrX; - } else { - var vector2d = new Proton.Vector2D(postionOrX, y); - return vector2d; - } - }, - - judgeVector2D : function(pOBJ) { - var result = ''; - if (pOBJ.hasOwnProperty('x') || pOBJ.hasOwnProperty('y') || pOBJ.hasOwnProperty('p') || pOBJ.hasOwnProperty('position')) - result += 'p'; - if (pOBJ.hasOwnProperty('vx') || pOBJ.hasOwnProperty('vx') || pOBJ.hasOwnProperty('v') || pOBJ.hasOwnProperty('velocity')) - result += 'v'; - if (pOBJ.hasOwnProperty('ax') || pOBJ.hasOwnProperty('ax') || pOBJ.hasOwnProperty('a') || pOBJ.hasOwnProperty('accelerate')) - result += 'a'; - - return result; - }, - - setVector2DByObject : function(target, pOBJ) { - if (pOBJ.hasOwnProperty('x')) - target.p.x = pOBJ['x']; - - if (pOBJ.hasOwnProperty('y')) - target.p.y = pOBJ['y']; - - if (pOBJ.hasOwnProperty('vx')) - target.v.x = pOBJ['vx']; - - if (pOBJ.hasOwnProperty('vy')) - target.v.y = pOBJ['vy']; - - if (pOBJ.hasOwnProperty('ax')) - target.a.x = pOBJ['ax']; - - if (pOBJ.hasOwnProperty('ay')) - target.a.y = pOBJ['ay']; - - if (pOBJ.hasOwnProperty('p')) - particle.p.copy(pOBJ['p']); - - if (pOBJ.hasOwnProperty('v')) - particle.v.copy(pOBJ['v']); - - if (pOBJ.hasOwnProperty('a')) - particle.a.copy(pOBJ['a']); - - if (pOBJ.hasOwnProperty('position')) - particle.p.copy(pOBJ['position']); - - if (pOBJ.hasOwnProperty('velocity')) - particle.v.copy(pOBJ['velocity']); - - if (pOBJ.hasOwnProperty('accelerate')) - particle.a.copy(pOBJ['accelerate']); - }, - //强行添加属性 - addPrototypeByObject : function(target, prototypeObject, filters) { - for (var singlePrototype in prototypeObject ) { - if (filters) { - if (filters.indexOf(singlePrototype) < 0) - target[singlePrototype] = Proton.Util.getSpanValue(prototypeObject[singlePrototype]); - } else { - target[singlePrototype] = Proton.Util.getSpanValue(prototypeObject[singlePrototype]); - } - } - - return target; - }, - //set prototype - setPrototypeByObject : function(target, prototypeObject, filters) { - for (var singlePrototype in prototypeObject ) { - if (target.hasOwnProperty(singlePrototype)) { - if (filters) { - if (filters.indexOf(singlePrototype) < 0) - target[singlePrototype] = Proton.Util.getSpanValue(prototypeObject[singlePrototype]); - } else { - target[singlePrototype] = Proton.Util.getSpanValue(prototypeObject[singlePrototype]); - } - } - } - - return target; - }, - - setSpanValue : function(a, b, c) { - if ( a instanceof Proton.Span) { - return a; - } else { - if (!b) { - return new Proton.Span(a); - } else { - if (!c) - return new Proton.Span(a, b); - else - return new Proton.Span(a, b, c); - } - } - }, - - getSpanValue : function(pan) { - if ( pan instanceof Proton.Span) - return pan.getValue(); - else - return pan; - }, - - inherits : function(subClass, superClass) { - subClass._super_ = superClass; - if (Object['create']) { - //console.log(subClass,superClass); - subClass.prototype = Object.create(superClass.prototype, { - constructor : { - value : subClass - } - }); - } else { - var F = function() { - }; - F.prototype = superClass.prototype; - subClass.prototype = new F(); - subClass.prototype.constructor = subClass; - } - }, - - getImageData : function(context, image, rect) { - context.drawImage(image, rect.x, rect.y); - var imagedata = context.getImageData(rect.x, rect.y, rect.width, rect.height); - context.clearRect(rect.x, rect.y, rect.width, rect.height); - return imagedata; - }, - - getImage : function(img, particle, drawCanvas, fun) { - if ( typeof (img) == 'string') { - this.loadAndSetImage(img, particle, drawCanvas, fun); - } else if ( typeof (img) == 'object') { - this.loadAndSetImage(img.src, particle, drawCanvas, fun); - } else if ( img instanceof Image) { - this.loadedImage(img.src, particle, drawCanvas, fun, img); - } - }, - - loadedImage : function(src, particle, drawCanvas, fun, target) { - particle.target = target; - particle.transform.src = src; - if (!Proton.TextureBuffer[src]) - Proton.TextureBuffer[src] = particle.target; - if (drawCanvas) { - if (Proton.TextureCanvasBuffer[src]) { - particle.transform.canvas = Proton.TextureCanvasBuffer[src]; - } else { - var _width = Proton.WebGLUtil.nhpot(particle.target.width); - var _height = Proton.WebGLUtil.nhpot(particle.target.height); - particle.transform.canvas = Proton.DomUtil.createCanvas('canvas' + src, _width, _height); - var context = particle.transform.canvas.getContext('2d'); - context.drawImage(particle.target, 0, 0, particle.target.width, particle.target.height); - Proton.TextureCanvasBuffer[src] = particle.transform.canvas; - } - } - if (fun) - fun(particle); - }, - - loadAndSetImage : function(src, particle, drawCanvas, fun) { - if (Proton.TextureBuffer[src]) { - this.loadedImage(src, particle, drawCanvas, fun, Proton.TextureBuffer[src]); - } else { - var self = this; - var myImage = new Image(); - myImage.onload = function(e) { - self.loadedImage(src, particle, drawCanvas, fun, e.target); - } - myImage.src = src; - } - }, - - hexToRGB : function(h) { - var hex16 = (h.charAt(0) == "#") ? h.substring(1, 7) : h; - var r = parseInt(hex16.substring(0, 2), 16); - var g = parseInt(hex16.substring(2, 4), 16); - var b = parseInt(hex16.substring(4, 6), 16); - - return { - r : r, - g : g, - b : b - } - }, - - rgbToHex : function(rbg) { - return 'rgb(' + rbg.r + ', ' + rbg.g + ', ' + rbg.b + ')'; - } - }; - - Proton.Util = Util; + function EventDispatcher() { + this.initialize(); + }; + + EventDispatcher.initialize = function(target) { + target.addEventListener = p.addEventListener; + target.removeEventListener = p.removeEventListener; + target.removeAllEventListeners = p.removeAllEventListeners; + target.hasEventListener = p.hasEventListener; + target.dispatchEvent = p.dispatchEvent; + }; + + var p = EventDispatcher.prototype; + + p._listeners = null; + + p.initialize = function() {}; + p.addEventListener = function(type, listener) { + if (!this._listeners) { + this._listeners = {}; + } else { + this.removeEventListener(type, listener); + } + + if (!this._listeners[type]) this._listeners[type] = [] + this._listeners[type].push(listener); + + return listener; + }; + + p.removeEventListener = function(type, listener) { + if (!this._listeners) return; + if (!this._listeners[type]) return; + + var arr = this._listeners[type]; + for (var i = 0, l = arr.length; i < l; i++) { + if (arr[i] == listener) { + if (l == 1) { + delete(this._listeners[type]); + } + // allows for faster checks. + else { + arr.splice(i, 1); + } + break; + } + } + }; + + p.removeAllEventListeners = function(type) { + if (!type) + this._listeners = null; + else if (this._listeners) + delete(this._listeners[type]); + }; + + p.dispatchEvent = function(eventName, eventTarget) { + var ret = false, + listeners = this._listeners; + + if (eventName && listeners) { + var arr = listeners[eventName]; + if (!arr) return ret; + + arr = arr.slice(); + // to avoid issues with items being removed or added during the dispatch + + var handler, i = arr.length; + while (i--) { + var handler = arr[i]; + ret = ret || handler(eventTarget); + } + + } + + return !!ret; + }; + + p.hasEventListener = function(type) { + var listeners = this._listeners; + return !!(listeners && listeners[type]); + }; + + EventDispatcher.initialize(Proton.prototype); + Proton.EventDispatcher = EventDispatcher; + + + + var Util = Util || { + initValue: function(value, defaults) { + var value = (value != null && value != undefined) ? value : defaults; + return value; + }, + + isArray: function(value) { + return typeof value === 'object' && value.hasOwnProperty('length'); + }, + + destroyArray: function(array) { + array.length = 0; + }, + + destroyObject: function(obj) { + for (var o in obj) + delete obj[o]; + }, + + getVector2D: function(postionOrX, y) { + if (typeof(postionOrX) == 'object') { + return postionOrX; + } else { + var vector2d = new Proton.Vector2D(postionOrX, y); + return vector2d; + } + }, + + classApply: function(constructor, argArray) { + if (!argArray) return new constructor; + + var args = [null].concat(argArray); + var factoryFunction = constructor.bind.apply(constructor, args); + return new factoryFunction(); + }, + + judgeVector2D: function(pOBJ) { + var result = ''; + if (pOBJ.hasOwnProperty('x') || pOBJ.hasOwnProperty('y') || pOBJ.hasOwnProperty('p') || pOBJ.hasOwnProperty('position')) + result += 'p'; + if (pOBJ.hasOwnProperty('vx') || pOBJ.hasOwnProperty('vx') || pOBJ.hasOwnProperty('v') || pOBJ.hasOwnProperty('velocity')) + result += 'v'; + if (pOBJ.hasOwnProperty('ax') || pOBJ.hasOwnProperty('ax') || pOBJ.hasOwnProperty('a') || pOBJ.hasOwnProperty('accelerate')) + result += 'a'; + + return result; + }, + + setVector2DByObject: function(target, pOBJ) { + if (pOBJ.hasOwnProperty('x')) + target.p.x = pOBJ['x']; + + if (pOBJ.hasOwnProperty('y')) + target.p.y = pOBJ['y']; + + if (pOBJ.hasOwnProperty('vx')) + target.v.x = pOBJ['vx']; + + if (pOBJ.hasOwnProperty('vy')) + target.v.y = pOBJ['vy']; + + if (pOBJ.hasOwnProperty('ax')) + target.a.x = pOBJ['ax']; + + if (pOBJ.hasOwnProperty('ay')) + target.a.y = pOBJ['ay']; + + if (pOBJ.hasOwnProperty('p')) + particle.p.copy(pOBJ['p']); + + if (pOBJ.hasOwnProperty('v')) + particle.v.copy(pOBJ['v']); + + if (pOBJ.hasOwnProperty('a')) + particle.a.copy(pOBJ['a']); + + if (pOBJ.hasOwnProperty('position')) + particle.p.copy(pOBJ['position']); + + if (pOBJ.hasOwnProperty('velocity')) + particle.v.copy(pOBJ['velocity']); + + if (pOBJ.hasOwnProperty('accelerate')) + particle.a.copy(pOBJ['accelerate']); + }, + //强行添加属性 + addPrototypeByObject: function(target, prototypeObject, filters) { + for (var singlePrototype in prototypeObject) { + if (filters) { + if (filters.indexOf(singlePrototype) < 0) + target[singlePrototype] = Proton.Util.getSpanValue(prototypeObject[singlePrototype]); + } else { + target[singlePrototype] = Proton.Util.getSpanValue(prototypeObject[singlePrototype]); + } + } + + return target; + }, + //set prototype + setPrototypeByObject: function(target, prototypeObject, filters) { + for (var singlePrototype in prototypeObject) { + if (target.hasOwnProperty(singlePrototype)) { + if (filters) { + if (filters.indexOf(singlePrototype) < 0) + target[singlePrototype] = Proton.Util.getSpanValue(prototypeObject[singlePrototype]); + } else { + target[singlePrototype] = Proton.Util.getSpanValue(prototypeObject[singlePrototype]); + } + } + } + + return target; + }, + + setSpanValue: function(a, b, c) { + if (a instanceof Proton.Span) { + return a; + } else { + if (!b) { + return new Proton.Span(a); + } else { + if (!c) + return new Proton.Span(a, b); + else + return new Proton.Span(a, b, c); + } + } + }, + + getSpanValue: function(pan) { + if (pan instanceof Proton.Span) + return pan.getValue(); + else + return pan; + }, + + inherits: function(subClass, superClass) { + subClass._super_ = superClass; + if (Object['create']) { + //console.log(subClass,superClass); + subClass.prototype = Object.create(superClass.prototype, { + constructor: { + value: subClass + } + }); + } else { + var F = function() {}; + F.prototype = superClass.prototype; + subClass.prototype = new F(); + subClass.prototype.constructor = subClass; + } + }, + + getImageData: function(context, image, rect) { + context.drawImage(image, rect.x, rect.y); + var imagedata = context.getImageData(rect.x, rect.y, rect.width, rect.height); + context.clearRect(rect.x, rect.y, rect.width, rect.height); + return imagedata; + }, + + getImage: function(img, particle, drawCanvas, fun) { + if (typeof(img) == 'string') { + this.loadAndSetImage(img, particle, drawCanvas, fun); + } else if (typeof(img) == 'object') { + this.loadAndSetImage(img.src, particle, drawCanvas, fun); + } else if (img instanceof Image) { + this.loadedImage(img.src, particle, drawCanvas, fun, img); + } + }, + + loadedImage: function(src, particle, drawCanvas, fun, target) { + particle.target = target; + particle.transform.src = src; + if (!Proton.TextureBuffer[src]) + Proton.TextureBuffer[src] = particle.target; + if (drawCanvas) { + if (Proton.TextureCanvasBuffer[src]) { + particle.transform.canvas = Proton.TextureCanvasBuffer[src]; + } else { + var _width = Proton.WebGLUtil.nhpot(particle.target.width); + var _height = Proton.WebGLUtil.nhpot(particle.target.height); + particle.transform.canvas = Proton.DomUtil.createCanvas('canvas' + src, _width, _height); + var context = particle.transform.canvas.getContext('2d'); + context.drawImage(particle.target, 0, 0, particle.target.width, particle.target.height); + Proton.TextureCanvasBuffer[src] = particle.transform.canvas; + } + } + if (fun) + fun(particle); + }, + + loadAndSetImage: function(src, particle, drawCanvas, fun) { + if (Proton.TextureBuffer[src]) { + this.loadedImage(src, particle, drawCanvas, fun, Proton.TextureBuffer[src]); + } else { + var self = this; + var myImage = new Image(); + myImage.onload = function(e) { + self.loadedImage(src, particle, drawCanvas, fun, e.target); + } + myImage.src = src; + } + }, + + hexToRGB: function(h) { + var hex16 = (h.charAt(0) == "#") ? h.substring(1, 7) : h; + var r = parseInt(hex16.substring(0, 2), 16); + var g = parseInt(hex16.substring(2, 4), 16); + var b = parseInt(hex16.substring(4, 6), 16); + + return { + r: r, + g: g, + b: b + } + }, + + rgbToHex: function(rbg) { + return 'rgb(' + rbg.r + ', ' + rbg.g + ', ' + rbg.b + ')'; + } + }; + + Proton.Util = Util; + + + +///bind +if (!Function.prototype.bind) { + Function.prototype.bind = function(oThis) { + if (typeof this !== "function") { + // closest thing possible to the ECMAScript 5 + // internal IsCallable function + throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function() {}, + fBound = function() { + return fToBind.apply(this instanceof fNOP ? this : oThis || this, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + fNOP.prototype = this.prototype; + fBound.prototype = new fNOP(); + + return fBound; + }; +} var WebGLUtil = WebGLUtil || { @@ -731,56 +740,77 @@ - function ParticlePool(num, releaseTime) { - this.proParticleCount = Proton.Util.initValue(num, 0); - this.releaseTime = Proton.Util.initValue(releaseTime, -1); - this.poolList = []; - this.timeoutID = 0; - for (var i = 0; i < this.proParticleCount; i++) { - this.add(); - } - ////////////////////////////// - if (this.releaseTime > 0) - this.timeoutID = setTimeout(this.release, this.releaseTime / 1000); - } + function Pool() { + this.cID = 0; + this.list = {}; + } + + Pool.prototype = { + create: function(obj, params) { + this.cID++; + + if (typeof obj == "function") + return Proton.Util.classApply(obj, params); + else + return obj.clone(); + }, + getCount: function() { + var count = 0; + for (var id in this.list) + count += this.list[id].length; - ParticlePool.prototype = { - create : function(newTypeParticleClass) { - if (newTypeParticleClass) - return new newTypeParticle; - else - return new Proton.Particle; - }, - getCount : function() { - return this.poolList.length; - }, - add : function() { - return this.poolList.push(this.create()); - }, - get : function() { - if (this.poolList.length === 0) { - return this.create(); - } else { - return this.poolList.pop().reset(); - } + return count++;; + }, - }, - set : function(particle) { - if (this.poolList.length < Proton.POOL_MAX) - return this.poolList.push(particle); - }, - release : function() { - for (var i = 0; i < this.poolList.length; i++) { - if (this.poolList[i]['destroy']) - this.poolList[i].destroy(); - delete this.poolList[i]; - } - this.poolList = []; - } - } + get: function(obj, params) { + var p, puid = obj.__puid || PUID.id(obj); + if (this.list[puid] && this.list[puid].length > 0) + p = this.list[puid].pop(); + else + p = this.create(obj, params); - Proton.ParticlePool = ParticlePool; + p.__puid = obj.__puid || puid; + return p; + }, + + set: function(obj) { + return this._getList(obj.__puid).push(obj); + }, + + destroy: function() { + for (var id in this.list) { + this.list[id].length = 0; + delete this.list[id]; + } + }, + + _getList: function(uid) { + uid = uid || "default"; + if (!this.list[uid]) this.list[uid] = []; + return this.list[uid]; + } + } + + Proton.Pool = Pool; + + var PUID = { + _id: 0, + _uids: {}, + id: function(obj) { + for (var id in this._uids) { + if (this._uids[id] == obj) return id; + } + + var nid = "PUID_" + (this._id++); + this._uids[nid] = obj; + return nid; + }, + + hash: function(str) { + return; + } + } @@ -2297,12 +2327,10 @@ * @method removeAllParticles */ Emitter.prototype.createParticle = function(initialize, behaviour) { - var particle = Proton.pool.get(); + var particle = Proton.pool.get(Proton.Particle); this.setupParticle(particle, initialize, behaviour); - this.dispatchEvent(new Proton.Event({ - type : Proton.PARTICLE_CREATED, - particle : particle - })); + this.dispatchEvent(Proton.PARTICLE_CREATED, particle); + return particle; }; /** @@ -2390,10 +2418,7 @@ particle.update(time, i); Proton.integrator.integrate(particle, time, damping); - this.dispatchEvent(new Proton.Event({ - type : Proton.PARTICLE_UPDATE, - particle : particle - })); + this.dispatchEvent(Proton.PARTICLE_UPDATE, particle); } }; @@ -2429,12 +2454,10 @@ for ( k = length - 1; k >= 0; k--) { particle = this.particles[k]; if (particle.dead) { + this.dispatchEvent(Proton.PARTICLE_DEAD , particle); + Proton.pool.set(particle); this.particles.splice(k, 1); - this.dispatchEvent(new Proton.Event({ - type : Proton.PARTICLE_DEAD, - particle : particle - })); } } }; @@ -2457,6 +2480,7 @@ behaviours = [behaviour]; } + particle.reset(); Proton.InitializeUtil.initialize(this, particle, initializes); particle.addBehaviours(behaviours); particle.parent = this; @@ -2958,26 +2982,27 @@ this.proton = proton; this.element = element; this.stroke = stroke; + this.pool = new Proton.Pool(); } BaseRender.prototype = { start : function() { var self = this; - this.proton.addEventListener(Proton.PROTON_UPDATE, function(e) { + this.proton.addEventListener(Proton.PROTON_UPDATE, function() { self.onProtonUpdate.call(self); }); - this.proton.addEventListener(Proton.PROTON_UPDATE_AFTER, function(e) { + this.proton.addEventListener(Proton.PROTON_UPDATE_AFTER, function() { self.onProtonUpdateAfter.call(self); }); - this.proton.addEventListener(Proton.EMITTER_ADDED, function(e) { - self.onEmitterAdded.call(self, e.emitter); + this.proton.addEventListener(Proton.EMITTER_ADDED, function(emitter) { + self.onEmitterAdded.call(self, emitter); }); - this.proton.addEventListener(Proton.EMITTER_REMOVED, function(e) { - self.onEmitterRemoved.call(self, e.emitter); + this.proton.addEventListener(Proton.EMITTER_REMOVED, function(emitter) { + self.onEmitterRemoved.call(self, emitter); }); var length = this.proton.emitters.length, i; @@ -2992,14 +3017,14 @@ addEmitterListener : function(emitter) { var self = this; - emitter.addEventListener(Proton.PARTICLE_CREATED, function(e) { - self.onParticleCreated.call(self, e.particle); + emitter.addEventListener(Proton.PARTICLE_CREATED, function(particle) { + self.onParticleCreated.call(self, particle); }); - emitter.addEventListener(Proton.PARTICLE_UPDATE, function(e) { - self.onParticleUpdate.call(self, e.particle); + emitter.addEventListener(Proton.PARTICLE_UPDATE, function(particle) { + self.onParticleUpdate.call(self, particle); }); - emitter.addEventListener(Proton.PARTICLE_DEAD, function(e) { - self.onParticleDead.call(self, e.particle); + emitter.addEventListener(Proton.PARTICLE_DEAD, function(particle) { + self.onParticleDead.call(self, particle); }); }, @@ -3136,69 +3161,70 @@ - function EaselRender(proton, element, stroke) { - EaselRender._super_.call(this, proton, element); - this.stroke = stroke; - } - - - Proton.Util.inherits(EaselRender, Proton.BaseRender); - EaselRender.prototype.resize = function(width, height) { - - } - EaselRender.prototype.start = function() { - EaselRender._super_.prototype.start.call(this); - }; - - EaselRender.prototype.onProtonUpdate = function() { - - } - - EaselRender.prototype.onParticleCreated = function(particle) { - if (particle.target) { - particle.target = particle.target.clone(); - if (!particle.target.parent) { - if (!!particle.target['image']) { - particle.target.regX = particle.target.image.width / 2; - particle.target.regY = particle.target.image.height / 2; - } - this.element.addChild(particle.target); - } - } else { - var graphics = new createjs.Graphics(); - if (this.stroke) { - if (this.stroke == true) { - graphics.beginStroke('#000000'); - } else if (this.stroke instanceof String) { - graphics.beginStroke(this.stroke); - } - } - - graphics.beginFill(particle.color).drawCircle(0, 0, particle.radius); - var shape = new createjs.Shape(graphics); - particle.target = shape; - this.element.addChild(particle.target); - } - } - - EaselRender.prototype.onParticleUpdate = function(particle) { - if (particle.target) { - particle.target.x = particle.p.x; - particle.target.y = particle.p.y; - particle.target.alpha = particle.alpha; - particle.target.scaleX = particle.target.scaleY = particle.scale; - particle.target.rotation = particle.rotation; - } - } - - EaselRender.prototype.onParticleDead = function(particle) { - if (particle.target) { - if (particle.target.parent) - particle.target.parent.removeChild(particle.target); - } - } - - Proton.EaselRender = EaselRender; + function EaselRender(proton, element, stroke) { + EaselRender._super_.call(this, proton, element); + this.stroke = stroke; + } + + + Proton.Util.inherits(EaselRender, Proton.BaseRender); + EaselRender.prototype.resize = function(width, height) { + + } + EaselRender.prototype.start = function() { + EaselRender._super_.prototype.start.call(this); + }; + + EaselRender.prototype.onProtonUpdate = function() { + + } + + EaselRender.prototype.onParticleCreated = function(particle) { + if (particle.target) { + particle.target = this.pool.get(particle.target); + if (!particle.target.parent) { + if (!!particle.target['image']) { + particle.target.regX = particle.target.image.width / 2; + particle.target.regY = particle.target.image.height / 2; + } + this.element.addChild(particle.target); + } + } else { + var graphics = this.pool.get(createjs.Graphics); + if (this.stroke) { + if (this.stroke == true) { + graphics.beginStroke('#000000'); + } else if (this.stroke instanceof String) { + graphics.beginStroke(this.stroke); + } + } + + graphics.beginFill(particle.color).drawCircle(0, 0, particle.radius); + var shape = new createjs.Shape(graphics); + particle.target = shape; + this.element.addChild(particle.target); + } + } + + EaselRender.prototype.onParticleUpdate = function(particle) { + if (particle.target) { + particle.target.x = particle.p.x; + particle.target.y = particle.p.y; + particle.target.alpha = particle.alpha; + particle.target.scaleX = particle.target.scaleY = particle.scale; + particle.target.rotation = particle.rotation; + } + } + + EaselRender.prototype.onParticleDead = function(particle) { + if (particle.target) { + particle.target.parent && particle.target.parent.removeChild(particle.target); + this.pool.set(particle.target); + particle.target = null; + } + } + + Proton.EaselRender = EaselRender; @@ -4028,7 +4054,7 @@ var Debug = Debug || { addEventListener : function(proton, fun) { - proton.addEventListener(Proton.PROTON_UPDATE, function(e) { + proton.addEventListener(Proton.PROTON_UPDATE, function() { fun(); }); }, diff --git a/build/proton-2.1.0.min.js b/build/proton-2.1.0.min.js new file mode 100644 index 0000000..e545898 --- /dev/null +++ b/build/proton-2.1.0.min.js @@ -0,0 +1,10 @@ +/*! + * Proton v2.1.0 + * https://github.com/a-jie/Proton + * + * Copyright 2011-2016, A-JIE + * Licensed under the MIT license + * http://www.opensource.org/licenses/mit-license + * + */ +(function(a,b){function bf(a,b,c,d){bf._super_.call(this),this.reset(a,b,c,d)}function be(a,b,c,d){be._super_.call(this),this.x=a,this.y=b,this.width=c,this.height=d}function bd(a,b){bd._super_.call(this),this.x=a,this.y=b}function bc(a,b,c){bc._super_.call(this),this.x=a,this.y=b,this.radius=c,this.angle=0,this.center={x:this.x,y:this.y}}function bb(a,b,d,e,f){bb._super_.call(this),d-a>=0?(this.x1=a,this.y1=b,this.x2=d,this.y2=e):(this.x1=d,this.y1=e,this.x2=a,this.y2=b),this.dx=this.x2-this.x1,this.dy=this.y2-this.y1,this.minx=Math.min(this.x1,this.x2),this.miny=Math.min(this.y1,this.y2),this.maxx=Math.max(this.x1,this.x2),this.maxy=Math.max(this.y1,this.y2),this.dot=this.x2*this.y1-this.x1*this.y2,this.xxyy=this.dx*this.dx+this.dy*this.dy,this.gradient=this.getGradient(),this.length=this.getLength(),this.direction=c.Util.initValue(f,">")}function ba(){this.vector=new c.Vector2D(0,0),this.random=0,this.crossType="dead",this.alert=!0}function _(a,b){_._super_.call(this,a,b),this.gl=this.element.getContext("experimental-webgl",{antialias:!0,stencil:!1,depth:!1}),this.gl||alert("Sorry your browser do not suppest WebGL!"),this.initVar(),this.setMaxRadius(),this.initShaders(),this.initBuffers(),this.gl.blendEquation(this.gl.FUNC_ADD),this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA),this.gl.enable(this.gl.BLEND)}function $(a,b,c){$._super_.call(this,a,b),this.context=this.element.getContext("2d"),this.imageData=null,this.rectangle=null,this.rectangle=c,this.createImageData(c)}function Z(a,b){Z._super_.call(this,a,b),this.stroke=null,this.context=this.element.getContext("2d"),this.bufferCache={}}function Y(a,b,c){Y._super_.call(this,a,b),this.stroke=c}function X(a,b){X._super_.call(this,a,b),this.stroke=null}function W(a,b,d){this.proton=a,this.element=b,this.stroke=d,this.pool=new c.Pool}function V(a,b,d){this.element=d,this.type=c.Util.initValue(a,"canvas"),this.proton=b,this.renderer=this.getRenderer()}function T(b,d,e){this.mouseTarget=c.Util.initValue(b,a),this.ease=c.Util.initValue(d,.7),this._allowEmitting=!1,this.initEventHandler(),T._super_.call(this,e)}function S(a){this.selfBehaviours=[],S._super_.call(this,a)}function R(a){this.initializes=[],this.particles=[],this.behaviours=[],this.emitTime=0,this.emitTotalTimes=-1,this.damping=.006,this.bindEmitter=!0,this.rate=new c.Rate(1,.1),R._super_.call(this,a),this.id="emitter_"+R.ID++}function Q(a,b,d,e){Q._super_.call(this,d,e),this.distanceVec=new c.Vector2D,this.centerPoint=c.Util.initValue(a,new c.Vector2D),this.force=c.Util.initValue(this.normalizeValue(b),100),this.name="GravityWell"}function P(a,b,c,d){P._super_.call(this,c,d),this.reset(a,b),this.name="Color"}function O(a,b,c,d,e){O._super_.call(this,d,e),this.reset(a,b,c),this.name="Rotate"}function N(a,b,c,d){N._super_.call(this,c,d),this.reset(a,b),this.name="Scale"}function M(a,b,c,d){M._super_.call(this,c,d),this.reset(a,b),this.name="Alpha"}function L(a,b,c,d){L._super_.call(this,c,d),this.reset(a,b),this.name="CrossZone"}function K(a,b,c,d,e){K._super_.call(this,d,e),this.reset(a,b,c),this.name="Collision"}function J(a,b,c){J._super_.call(this,0,a,b,c),this.name="Gravity"}function I(a,b,c,d,e){I._super_.call(this,a,b,c,d,e),this.force*=-1,this.name="Repulsion"}function H(a,b,c,d,e){H._super_.call(this,d,e),this.reset(a,b,c),this.time=0,this.name="RandomDrift"}function G(a,b,d,e,f){G._super_.call(this,e,f),this.targetPosition=c.Util.initValue(a,new c.Vector2D),this.radius=c.Util.initValue(d,1e3),this.force=c.Util.initValue(this.normalizeValue(b),100),this.radiusSq=this.radius*this.radius,this.attractionForce=new c.Vector2D,this.lengthSq=0,this.name="Attraction"}function F(a,b,d,e){F._super_.call(this,d,e),this.force=this.normalizeForce(new c.Vector2D(a,b)),this.name="Force"}function E(a,b,d){E._super_.call(this),this.image=this.setSpanValue(a),this.w=c.Util.initValue(b,20),this.h=c.Util.initValue(d,this.w)}function D(a,b,d){D._super_.call(this),this.radius=c.Util.setSpanValue(a,b,d)}function C(a,b,d){C._super_.call(this),this.massPan=c.Util.setSpanValue(a,b,d)}function B(a,b,d){B._super_.call(this),this.rPan=c.Util.setSpanValue(a),this.thaPan=c.Util.setSpanValue(b),this.type=c.Util.initValue(d,"vector")}function A(a){A._super_.call(this),this.zone=c.Util.initValue(a,new c.PointZone)}function z(a,b,d){z._super_.call(this),this.lifePan=c.Util.setSpanValue(a,b,d)}function x(){}function w(a,b){this.numPan=c.Util.initValue(a,1),this.timePan=c.Util.initValue(b,1),this.numPan=c.Util.setSpanValue(this.numPan),this.timePan=c.Util.setSpanValue(this.timePan),this.startTime=0,this.nextTime=0,this.init()}function v(a,b){this.id="Behaviour_"+v.id++,this.life=c.Util.initValue(a,Infinity),this.easing=c.ease.setEasingByName(b),this.age=0,this.energy=1,this.dead=!1,this.parents=[],this.name="Behaviour"}function t(a,b,c,d){this.x=a,this.y=b,this.width=c,this.height=d,this.bottom=this.y+this.height,this.right=this.x+this.width}function s(a){c.Util.isArray(a)?this.colorArr=a:this.colorArr=[a]}function r(a,b,d){this.isArray=!1,c.Util.isArray(a)?(this.isArray=!0,this.a=a):(this.a=c.Util.initValue(a,1),this.b=c.Util.initValue(b,this.a),this.center=c.Util.initValue(d,!1))}function k(){this.cID=0,this.list={}}function j(a){this.id="particle_"+j.ID++,this.reset(!0),c.Util.setPrototypeByObject(this,a)}function i(){this.mats=[],this.size=0;for(var a=0;a<20;a++)this.mats.push(c.Mat3.create([0,0,0,0,0,0,0,0,0]))}function d(){this.initialize()}function c(a,b){this.integrationType=c.Util.initValue(b,c.EULER),this.emitters=[],this.renderers=[],this.time=0,this.oldTime=0,c.pool=new c.Pool(100),c.integrator=new c.NumericalIntegration(this.integrationType)}c.POOL_MAX=1e3,c.TIME_STEP=60,c.USE_CLOCK=!1,c.MEASURE=100,c.EULER="euler",c.RK2="runge-kutta2",c.RK4="runge-kutta4",c.VERLET="verlet",c.PARTICLE_CREATED="partilcleCreated",c.PARTICLE_UPDATE="partilcleUpdate",c.PARTICLE_SLEEP="particleSleep",c.PARTICLE_DEAD="partilcleDead",c.PROTON_UPDATE="protonUpdate",c.PROTON_UPDATE_AFTER="protonUpdateAfter",c.EMITTER_ADDED="emitterAdded",c.EMITTER_REMOVED="emitterRemoved",c.amendChangeTabsBug=!0,c.TextureBuffer={},c.TextureCanvasBuffer={},c.prototype={addRender:function(a){a.proton=this,this.renderers.push(a.proton)},addEmitter:function(a){this.emitters.push(a),a.parent=this,this.dispatchEvent(c.EMITTER_ADDED,a)},removeEmitter:function(a){var b=this.emitters.indexOf(a);this.emitters.splice(b,1),a.parent=null,this.dispatchEvent(c.EMITTER_REMOVED,a)},update:function(){this.dispatchEvent(c.PROTON_UPDATE);if(c.USE_CLOCK){this.oldTime||(this.oldTime=(new Date).getTime());var a=(new Date).getTime();this.elapsed=(a-this.oldTime)/1e3,c.amendChangeTabsBug&&this.amendChangeTabsBug(),this.oldTime=a}else this.elapsed=.0167;if(this.elapsed>0)for(var b=0;b.5&&(this.oldTime=(new Date).getTime(),this.elapsed=0)},getCount:function(){var a=0,b=this.emitters.length;for(var c=0;c>b;return a+1},makeTranslation:function(a,b){return[1,0,0,0,1,0,a,b,1]},makeRotation:function(a){var b=Math.cos(a),c=Math.sin(a);return[b,-c,0,c,b,0,0,0,1]},makeScale:function(a,b){return[a,0,0,0,b,0,0,0,1]},matrixMultiply:function(a,b){var c=a[0],d=a[1],e=a[2],f=a[3],g=a[4],h=a[5],i=a[6],j=a[7],k=a[8],l=b[0],m=b[1],n=b[2],o=b[3],p=b[4],q=b[5],r=b[6],s=b[7],t=b[8];return[c*l+d*o+e*r,c*m+d*p+e*s,c*n+d*q+e*t,f*l+g*o+h*r,f*m+g*p+h*s,f*n+g*q+h*t,i*l+j*o+k*r,i*m+j*p+k*s,i*n+j*q+k*t]}};c.WebGLUtil=g;var h=h||{createCanvas:function(a,b,c,d){var e=document.createElement("canvas"),f=d?d:"absolute";e.id=a,e.width=b,e.height=c,e.style.position=f,e.style.opacity=0,this.transformDom(e,-500,-500,0,0);return e},transformDom:function(a,b,c,d,e){a.style.WebkitTransform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)",a.style.MozTransform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)",a.style.OTransform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)",a.style.msTransform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)",a.style.transform="translate("+b+"px, "+c+"px) "+"scale("+d+") "+"rotate("+e+"deg)"}};c.DomUtil=h,i.prototype.set=function(a,b){b==0?c.Mat3.set(a,this.mats[0]):c.Mat3.multiply(this.mats[b-1],a,this.mats[b]),this.size=Math.max(this.size,b+1)},i.prototype.push=function(a){this.size==0?c.Mat3.set(a,this.mats[0]):c.Mat3.multiply(this.mats[this.size-1],a,this.mats[this.size]),this.size++},i.prototype.pop=function(){this.size>0&&this.size--},i.prototype.top=function(){return this.mats[this.size-1]},c.MStack=i,j.ID=0,j.prototype={getDirection:function(){return Math.atan2(this.v.x,-this.v.y)*(180/Math.PI)},reset:function(a){this.life=Infinity,this.age=0,this.energy=1,this.dead=!1,this.sleep=!1,this.target=null,this.sprite=null,this.parent=null,this.mass=1,this.radius=10,this.alpha=1,this.scale=1,this.rotation=0,this.color=null,this.easing=c.ease.setEasingByName(c.easeLinear),a?(this.transform={},this.p=new c.Vector2D,this.v=new c.Vector2D,this.a=new c.Vector2D,this.old={p:new c.Vector2D,v:new c.Vector2D,a:new c.Vector2D},this.behaviours=[]):(c.Util.destroyObject(this.transform),this.p.set(0,0),this.v.set(0,0),this.a.set(0,0),this.old.p.set(0,0),this.old.v.set(0,0),this.old.a.set(0,0),this.removeAllBehaviours()),this.transform.rgb={r:255,g:255,b:255};return this},update:function(a,b){if(!this.sleep){this.age+=a;var c=this.behaviours.length,d;for(d=0;d=this.life)this.destroy();else{var e=this.easing(this.age/this.life);this.energy=Math.max(1-e,0)}},addBehaviour:function(a){this.behaviours.push(a),a.hasOwnProperty("parents")&&a.parents.push(this),a.initialize(this)},addBehaviours:function(a){var b=a.length,c;for(c=0;c-1){var a=this.behaviours.splice(b,1);a.parents=null}},removeAllBehaviours:function(){c.Util.destroyArray(this.behaviours)},destroy:function(){this.removeAllBehaviours(),this.energy=0,this.dead=!0,this.parent=null}},c.Particle=j,k.prototype={create:function(a,b){this.cID++;return typeof a=="function"?c.Util.classApply(a,b):a.clone()},getCount:function(){var a=0;for(var b in this.list)a+=this.list[b].length;return a++},get:function(a,b){var c,d=a.__puid||l.id(a);this.list[d]&&this.list[d].length>0?c=this.list[d].pop():c=this.create(a,b),c.__puid=a.__puid||d;return c},set:function(a){return this._getList(a.__puid).push(a)},destroy:function(){for(var a in this.list)this.list[a].length=0,delete this.list[a]},_getList:function(a){a=a||"default",this.list[a]||(this.list[a]=[]);return this.list[a]}},c.Pool=k;var l={_id:0,_uids:{},id:function(a){for(var b in this._uids)if(this._uids[b]==a)return b;var c="PUID_"+this._id++;this._uids[c]=a;return c},hash:function(a){return}},m={randomAToB:function(a,b,c){return c?Math.floor(Math.random()*(b-a))+a:a+Math.random()*(b-a)},randomFloating:function(a,b,c){return m.randomAToB(a-b,a+b,c)},randomZone:function(a){},degreeTransform:function(a){return a*Math.PI/180},toColor16:function(a){return"#"+a.toString(16)},randomColor:function(){return"#"+("00000"+(Math.random()*16777216<<0).toString(16)).slice(-6)}};c.MathUtils=m;var o=function(a){this.type=c.Util.initValue(a,c.EULER)};o.prototype={integrate:function(a,b,c){this.eulerIntegrate(a,b,c)},eulerIntegrate:function(a,b,c){a.sleep||(a.old.p.copy(a.p),a.old.v.copy(a.v),a.a.multiplyScalar(1/a.mass),a.v.add(a.a.multiplyScalar(b)),a.p.add(a.old.v.multiplyScalar(b)),c&&a.v.multiplyScalar(c),a.a.clear())}},c.NumericalIntegration=o;var p=function(a,b){this.x=a||0,this.y=b||0};p.prototype={set:function(a,b){this.x=a,this.y=b;return this},setX:function(a){this.x=a;return this},setY:function(a){this.y=a;return this},setComponent:function(a,b){switch(a){case 0:this.x=b;break;case 1:this.y=b;break;default:throw new Error("index is out of range: "+a)}},getGradient:function(){if(this.x!=0)return Math.atan2(this.y,this.x);if(this.y>0)return Math.PI/2;if(this.y<0)return-Math.PI/2},getComponent:function(a){switch(a){case 0:return this.x;case 1:return this.y;default:throw new Error("index is out of range: "+a)}},copy:function(a){this.x=a.x,this.y=a.y;return this},add:function(a,c){if(c!==b)return this.addVectors(a,c);this.x+=a.x,this.y+=a.y;return this},addXY:function(a,b){this.x+=a,this.y+=b;return this},addVectors:function(a,b){this.x=a.x+b.x,this.y=a.y+b.y;return this},addScalar:function(a){this.x+=a,this.y+=a;return this},sub:function(a,c){if(c!==b)return this.subVectors(a,c);this.x-=a.x,this.y-=a.y;return this},subVectors:function(a,b){this.x=a.x-b.x,this.y=a.y-b.y;return this},multiplyScalar:function(a){this.x*=a,this.y*=a;return this},divideScalar:function(a){a!==0?(this.x/=a,this.y/=a):this.set(0,0);return this},min:function(a){this.x>a.x&&(this.x=a.x),this.y>a.y&&(this.y=a.y);return this},max:function(a){this.xb.x&&(this.x=b.x),this.yb.y&&(this.y=b.y);return this},negate:function(){return this.multiplyScalar(-1)},dot:function(a){return this.x*a.x+this.y*a.y},lengthSq:function(){return this.x*this.x+this.y*this.y},length:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},normalize:function(){return this.divideScalar(this.length())},distanceTo:function(a){return Math.sqrt(this.distanceToSquared(a))},rotate:function(a){var b=this.x,c=this.y;this.x=b*Math.cos(a)+c*Math.sin(a),this.y=-b*Math.sin(a)+c*Math.cos(a);return this},distanceToSquared:function(a){var b=this.x-a.x,c=this.y-a.y;return b*b+c*c},setLength:function(a){var b=this.length();b!==0&&a!==b&&this.multiplyScalar(a/b);return this},lerp:function(a,b){this.x+=(a.x-this.x)*b,this.y+=(a.y-this.y)*b;return this},equals:function(a){return a.x===this.x&&a.y===this.y},toArray:function(){return[this.x,this.y]},clear:function(){this.x=0,this.y=0;return this},clone:function(){return new c.Vector2D(this.x,this.y)}},c.Vector2D=p;var q=function(a,b){this.r=Math.abs(a)||0,this.tha=b||0};q.prototype={set:function(a,b){this.r=a,this.tha=b;return this},setR:function(a){this.r=a;return this},setTha:function(a){this.tha=a;return this},copy:function(a){this.r=a.r,this.tha=a.tha;return this},toVector:function(){return new c.Vector2D(this.getX(),this.getY())},getX:function(){return this.r*Math.sin(this.tha)},getY:function(){return-this.r*Math.cos(this.tha)},normalize:function(){this.r=1;return this},equals:function(a){return a.r===this.r&&a.tha===this.tha},toArray:function(){return[this.r,this.tha]},clear:function(){this.r=0,this.tha=0;return this},clone:function(){return new c.Polar2D(this.r,this.tha)}},c.Polar2D=q,r.prototype={getValue:function(a){return this.isArray?this.a[Math.floor(this.a.length*Math.random())]:this.center?c.MathUtils.randomFloating(this.a,this.b,a):c.MathUtils.randomAToB(this.a,this.b,a)}},c.Span=r,c.getSpan=function(a,b,d){return new c.Span(a,b,d)},c.Util.inherits(s,c.Span),s.prototype.getValue=function(){var a=this.colorArr[Math.floor(this.colorArr.length*Math.random())];return a=="random"||a=="Random"?c.MathUtils.randomColor():a},c.ColorSpan=s,t.prototype={contains:function(a,b){return a<=this.right&&a>=this.x&&b<=this.bottom&&b>=this.y?!0:!1}},c.Rectangle=t;var u=u||{create:function(a){var b=new Float32Array(9);a&&this.set(a,b);return b},set:function(a,b){for(var c=0;c<9;c++)b[c]=a[c];return b},multiply:function(a,b,c){var d=a[0],e=a[1],f=a[2],g=a[3],h=a[4],i=a[6],j=a[7],k=b[0],l=b[1],m=b[2],n=b[3],o=b[4],p=b[6],q=b[7];c[0]=k*d+l*g,c[1]=k*e+l*h,c[2]=f*m,c[3]=n*d+o*g,c[4]=n*e+o*h,c[6]=p*d+q*g+i,c[7]=p*e+q*h+j;return c},inverse:function(a,b){var c=a[0],d=a[1],e=a[3],f=a[4],g=a[6],h=a[7],i=f,j=-e,k=h*e-f*g,l=c*i+d*j,m;m=1/l,b[0]=i*m,b[1]=-d*m,b[3]=j*m,b[4]=c*m,b[6]=k*m,b[7]=(-h*c+d*g)*m;return b},multiplyVec2:function(a,b,c){var d=b[0],e=b[1];c[0]=d*a[0]+e*a[3]+a[6],c[1]=d*a[1]+e*a[4]+a[7];return c}};c.Mat3=u,v.id=0,v.prototype={reset:function(a,b){this.life=c.Util.initValue(a,Infinity),this.easing=c.Util.initValue(b,c.ease.setEasingByName(c.easeLinear))},normalizeForce:function(a){return a.multiplyScalar(c.MEASURE)},normalizeValue:function(a){return a*c.MEASURE},initialize:function(a){},applyBehaviour:function(a,b,c){this.age+=b;if(this.age>=this.life||this.dead)this.energy=0,this.dead=!0,this.destroy();else{var d=this.easing(a.age/a.life);this.energy=Math.max(1-d,0)}},destroy:function(){var a,b=this.parents.length,c;for(c=0;c=this.nextTime){this.startTime=0,this.nextTime=this.timePan.getValue();return this.numPan.b==1?this.numPan.getValue(!1)>.5?1:0:this.numPan.getValue(!0)}return 0}},c.Rate=w,x.prototype.reset=function(){},x.prototype.init=function(a,b){b?this.initialize(b):this.initialize(a)},x.prototype.initialize=function(a){},c.Initialize=x;var y={initialize:function(a,b,d){var e=d.length,f;for(f=0;f4e-6&&this.lengthSq=this.delay&&(a.a.addXY(c.MathUtils.randomAToB(-this.panFoce.x,this.panFoce.x),c.MathUtils.randomAToB(-this.panFoce.y,this.panFoce.y)),this.time=0)},c.RandomDrift=H,c.Util.inherits(I,c.Attraction),I.prototype.reset=function(a,b,c,d,e){I._super_.prototype.reset.call(this,a,b,c,d,e),this.force*=-1},c.Repulsion=I,c.Util.inherits(J,c.Force),J.prototype.reset=function(a,b,c){J._super_.prototype.reset.call(this,0,a,b,c)},c.Gravity=J,c.G=J,c.Util.inherits(K,c.Behaviour),K.prototype.reset=function(a,b,d,e,f){this.emitter=c.Util.initValue(a,null),this.mass=c.Util.initValue(b,!0),this.callback=c.Util.initValue(d,null),this.collisionPool=[],this.delta=new c.Vector2D,e&&K._super_.prototype.reset.call(this,e,f)},K.prototype.applyBehaviour=function(a,b,c){var d=this.emitter?this.emitter.particles.slice(c):this.pool.slice(c),e,f,g,h,i,j=d.length;for(var k=0;k-1&&this.initializes.splice(b,1)},R.prototype.removeInitializers=function(){c.Util.destroyArray(this.initializes)},R.prototype.addBehaviour=function(){var a=arguments.length,b;for(b=0;b-1&&this.behaviours.splice(b,1)},R.prototype.removeAllBehaviours=function(){c.Util.destroyArray(this.behaviours)},R.prototype.integrate=function(a){var b=1-this.damping;c.integrator.integrate(this,a,b);var d=this.particles.length,e;for(e=0;e=this.life||this.dead)&&this.destroy(),this.emitting(a),this.integrate(a);var b,d=this.particles.length,e;for(e=d-1;e>=0;e--)b=this.particles[e],b.dead&&(this.dispatchEvent(c.PARTICLE_DEAD,b),c.pool.set(b),this.particles.splice(e,1))},R.prototype.setupParticle=function(a,b,d){var e=this.initializes,f=this.behaviours;b&&(b instanceof Array?e=b:e=[b]),d&&(d instanceof Array?f=d:f=[d]),a.reset(),c.InitializeUtil.initialize(this,a,e),a.addBehaviours(f),a.parent=this,this.particles.push(a)},R.prototype.destroy=function(){this.dead=!0,this.emitTotalTimes=-1,this.particles.length==0&&(this.removeInitializers(),this.removeAllBehaviours(),this.parent&&this.parent.removeEmitter(this))},c.Emitter=R,c.Util.inherits(S,c.Emitter),S.prototype.addSelfBehaviour=function(){var a=arguments.length,b;for(b=0;b-1&&this.selfBehaviours.splice(b,1)},S.prototype.update=function(a){S._super_.prototype.update.call(this,a);if(!this.sleep){var b=this.selfBehaviours.length,c;for(c=0;cthis.element.width||c<0||c>this.elementwidth)){var f=((c>>0)*a.width+(b>>0))*4;a.data[f]=e.r,a.data[f+1]=e.g,a.data[f+2]=e.b,a.data[f+3]=d.alpha*255}},$.prototype.onParticleDead=function(a){},c.PixelRender=$,c.Util.inherits(_,c.BaseRender),_.prototype.resize=function(a,b){this.umat[4]=-2,this.umat[7]=1,this.smat[0]=1/a,this.smat[4]=1/b,this.mstack.set(this.umat,0),this.mstack.set(this.smat,1),this.gl.viewport(0,0,a,b),this.element.width=a,this.element.height=b},_.prototype.setMaxRadius=function(a){this.circleCanvasURL=this.createCircle(a)},_.prototype.getVertexShader=function(){var a=["uniform vec2 viewport;","attribute vec2 aVertexPosition;","attribute vec2 aTextureCoord;","uniform mat3 tMat;","varying vec2 vTextureCoord;","varying float alpha;","void main() {","vec3 v = tMat * vec3(aVertexPosition, 1.0);","gl_Position = vec4(v.x, v.y, 0, 1);","vTextureCoord = aTextureCoord;","alpha = tMat[0][2];","}"].join("\n");return a},_.prototype.getFragmentShader=function(){var a=["precision mediump float;","varying vec2 vTextureCoord;","varying float alpha;","uniform sampler2D uSampler;","uniform vec4 color;","uniform bool useTexture;","uniform vec3 uColor;","void main() {","vec4 textureColor = texture2D(uSampler, vTextureCoord);","gl_FragColor = textureColor * vec4(uColor, 1.0);","gl_FragColor.w *= alpha;","}"].join("\n");return a},_.prototype.initVar=function(){this.mstack=new c.MStack,this.umat=c.Mat3.create([2,0,1,0,-2,0,-1,1,1]),this.smat=c.Mat3.create([.01,0,1,0,.01,0,0,0,1]),this.texturebuffers={}},_.prototype.start=function(){_._super_.prototype.start.call(this),this.resize(this.element.width,this.element.height)},_.prototype.blendEquation=function(a){this.gl.blendEquation(this.gl[a])},_.prototype.blendFunc=function(a,b){this.gl.blendFunc(this.gl[a],this.gl[b])},_.prototype.getShader=function(a,b,c){var d;c?d=a.createShader(a.FRAGMENT_SHADER):d=a.createShader(a.VERTEX_SHADER),a.shaderSource(d,b),a.compileShader(d);if(!a.getShaderParameter(d,a.COMPILE_STATUS)){alert(a.getShaderInfoLog(d));return null}return d},_.prototype.initShaders=function(){var a=this.getShader(this.gl,this.getFragmentShader(),!0),b=this.getShader(this.gl,this.getVertexShader(),!1);this.sprogram=this.gl.createProgram(),this.gl.attachShader(this.sprogram,b),this.gl.attachShader(this.sprogram,a),this.gl.linkProgram(this.sprogram),this.gl.getProgramParameter(this.sprogram,this.gl.LINK_STATUS)||alert("Could not initialise shaders"),this.gl.useProgram(this.sprogram),this.sprogram.vpa=this.gl.getAttribLocation(this.sprogram,"aVertexPosition"),this.sprogram.tca=this.gl.getAttribLocation(this.sprogram,"aTextureCoord"),this.gl.enableVertexAttribArray(this.sprogram.tca),this.gl.enableVertexAttribArray(this.sprogram.vpa),this.sprogram.tMatUniform=this.gl.getUniformLocation(this.sprogram,"tMat"),this.sprogram.samplerUniform=this.gl.getUniformLocation(this.sprogram,"uSampler"),this.sprogram.useTex=this.gl.getUniformLocation(this.sprogram,"useTexture"),this.sprogram.color=this.gl.getUniformLocation(this.sprogram,"uColor"),this.gl.uniform1i(this.sprogram.useTex,1)},_.prototype.initBuffers=function(){this.unitIBuffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.unitIBuffer);var a=[0,3,1,0,2,3];this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(a),this.gl.STATIC_DRAW);var b=[];for(var c=0;c<100;c++)b.push(c);idx=new Uint16Array(b),this.unitI33=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.unitI33),this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER,idx,this.gl.STATIC_DRAW),b=[];for(c=0;c<100;c++)b.push(c,c+1,c+2);idx=new Uint16Array(b),this.stripBuffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.stripBuffer),this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER,idx,this.gl.STATIC_DRAW)},_.prototype.createCircle=function(a){this.circleCanvasRadius=c.WebGLUtil.nhpot(c.Util.initValue(a,32));var b=c.DomUtil.createCanvas("circle_canvas",this.circleCanvasRadius*2,this.circleCanvasRadius*2),d=b.getContext("2d");d.beginPath(),d.arc(this.circleCanvasRadius,this.circleCanvasRadius,this.circleCanvasRadius,0,Math.PI*2,!0),d.closePath(),d.fillStyle="#FFF",d.fill();return b.toDataURL()},_.prototype.setImgInCanvas=function(a){var b=a.target.width,d=a.target.height,e=c.WebGLUtil.nhpot(a.target.width),f=c.WebGLUtil.nhpot(a.target.height),g=a.target.width/e,h=a.target.height/f;this.texturebuffers[a.transform.src]||(this.texturebuffers[a.transform.src]=[this.gl.createTexture(),this.gl.createBuffer(),this.gl.createBuffer()]),a.transform.texture=this.texturebuffers[a.transform.src][0],a.transform.vcBuffer=this.texturebuffers[a.transform.src][1],a.transform.tcBuffer=this.texturebuffers[a.transform.src][2],this.gl.bindBuffer(this.gl.ARRAY_BUFFER,a.transform.tcBuffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array([0,0,g,0,0,h,h,h]),this.gl.STATIC_DRAW),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,a.transform.vcBuffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array([0,0,b,0,0,d,b,d]),this.gl.STATIC_DRAW);var i=a.transform.canvas.getContext("2d"),j=i.getImageData(0,0,e,f);this.gl.bindTexture(this.gl.TEXTURE_2D,a.transform.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,j),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR_MIPMAP_NEAREST),this.gl.generateMipmap(this.gl.TEXTURE_2D),a.transform.textureLoaded=!0,a.transform.textureWidth=b,a.transform.textureHeight=d},_.prototype.setStroke=function(a,b){},_.prototype.onProtonUpdate=function(){},_.prototype.onParticleCreated=function(a){var b=this;a.transform.textureLoaded=!1,a.transform.tmat=c.Mat3.create(),a.transform.tmat[8]=1,a.transform.imat=c.Mat3.create(),a.transform.imat[8]=1,a.target?c.Util.getImage(a.target,a,!0,function(a){b.setImgInCanvas.call(b,a),a.transform.oldScale=1}):c.Util.getImage(this.circleCanvasURL,a,!0,function(a){b.setImgInCanvas.call(b,a),a.transform.oldScale=a.radius/b.circleCanvasRadius})},_.prototype.onParticleUpdate=function(a){a.transform.textureLoaded&&(this.updateMatrix(a),this.gl.uniform3f(this.sprogram.color,a.transform.rgb.r/255,a.transform.rgb.g/255,a.transform.rgb.b/255),this.gl.uniformMatrix3fv(this.sprogram.tMatUniform,!1,this.mstack.top()),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,a.transform.vcBuffer),this.gl.vertexAttribPointer(this.sprogram.vpa,2,this.gl.FLOAT,!1,0,0),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,a.transform.tcBuffer),this.gl.vertexAttribPointer(this.sprogram.tca,2,this.gl.FLOAT,!1,0,0),this.gl.bindTexture(this.gl.TEXTURE_2D,a.transform.texture),this.gl.uniform1i(this.sprogram.samplerUniform,0),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,this.unitIBuffer),this.gl.drawElements(this.gl.TRIANGLES,6,this.gl.UNSIGNED_SHORT,0),this.mstack.pop())},_.prototype.onParticleDead=function(a){},_.prototype.updateMatrix=function(a){var b=c.WebGLUtil.makeTranslation(-a.transform.textureWidth/2,-a.transform.textureHeight/2),d=c.WebGLUtil.makeTranslation(a.p.x,a.p.y),e=a.rotation*(Math.PI/180),f=c.WebGLUtil.makeRotation(e),g=a.scale*a.transform.oldScale,h=c.WebGLUtil.makeScale(g,g),i=c.WebGLUtil.matrixMultiply(b,h);i=c.WebGLUtil.matrixMultiply(i,f),i=c.WebGLUtil.matrixMultiply(i,d),c.Mat3.inverse(i,a.transform.imat),i[2]=a.alpha,this.mstack.push(i)},c.WebGLRender=_,ba.prototype={getPosition:function(){},crossing:function(a){}},c.Zone=ba,c.Util.inherits(bb,c.Zone),bb.prototype.getPosition=function(){this.random=Math.random(),this.vector.x=this.x1+this.random*this.length*Math.cos(this.gradient),this.vector.y=this.y1+this.random*this.length*Math.sin(this.gradient);return this.vector},bb.prototype.getDirection=function(a,b){var c=this.dy,d=-this.dx,e=this.dot,f=d==0?1:d;return(c*a+d*b+e)*f>0?!0:!1},bb.prototype.getDistance=function(a,b){var c=this.dy,d=-this.dx,e=this.dot,f=c*a+d*b+e;return f/Math.sqrt(this.xxyy)},bb.prototype.getSymmetric=function(a){var b=a.getGradient(),c=this.getGradient(),d=2*(c-b),e=a.x,f=a.y;a.x=e*Math.cos(d)-f*Math.sin(d),a.y=e*Math.sin(d)+f*Math.cos(d);return a},bb.prototype.getGradient=function(){return Math.atan2(this.dy,this.dx)},bb.prototype.getRange=function(a,b){var c=Math.abs(this.getGradient());c<=Math.PI/4?a.p.xthis.minx&&b():a.p.ythis.miny&&b()},bb.prototype.getLength=function(){return Math.sqrt(this.dx*this.dx+this.dy*this.dy)},bb.prototype.crossing=function(a){var b=this;this.crossType=="dead"?this.direction==">"||this.direction=="R"||this.direction=="right"||this.direction=="down"?this.getRange(a,function(){b.getDirection(a.p.x,a.p.y)&&(a.dead=!0)}):this.getRange(a,function(){b.getDirection(a.p.x,a.p.y)||(a.dead=!0)}):this.crossType=="bound"?this.getRange(a,function(){b.getDistance(a.p.x,a.p.y)<=a.radius&&(b.dx==0?a.v.x*=-1:b.dy==0?a.v.y*=-1:b.getSymmetric(a.v))}):this.crossType=="cross"&&this.alert&&(alert("Sorry lineZone does not support cross method"),this.alert=!1)},c.LineZone=bb,c.Util.inherits(bc,c.Zone),bc.prototype.getPosition=function(){this.random=Math.random(),this.angle=Math.PI*2*Math.random(),this.vector.x=this.x+this.random*this.radius*Math.cos(this.angle),this.vector.y=this.y+this.random*this.radius*Math.sin(this.angle);return this.vector},bc.prototype.setCenter=function(a,b){this.center.x=a,this.center.y=b},bc.prototype.crossing=function(a){var b=a.p.distanceTo(this.center);this.crossType=="dead"?b-a.radius>this.radius&&(a.dead=!0):this.crossType=="bound"?b+a.radius>=this.radius&&this.getSymmetric(a):this.crossType=="cross"&&this.alert&&(alert("Sorry CircleZone does not support cross method"),this.alert=!1)},bc.prototype.getSymmetric=function(a){var b=a.v.getGradient(),c=this.getGradient(a),d=2*(c-b),e=a.v.x,f=a.v.y;a.v.x=e*Math.cos(d)-f*Math.sin(d),a.v.y=e*Math.sin(d)+f*Math.cos(d)},bc.prototype.getGradient=function(a){return-Math.PI/2+Math.atan2(a.p.y-this.center.y,a.p.x-this.center.x)},c.CircleZone=bc,c.Util.inherits(bd,c.Zone),bd.prototype.getPosition=function(){this.vector.x=this.x,this.vector.y=this.y;return this.vector},bd.prototype.crossing=function(a){this.alert&&(alert("Sorry PointZone does not support crossing method"),this.alert=!1)},c.PointZone=bd,c.Util.inherits(be,c.Zone),be.prototype.getPosition=function(){this.vector.x=this.x+Math.random()*this.width,this.vector.y=this.y+Math.random()*this.height;return this.vector},be.prototype.crossing=function(a){this.crossType=="dead"?(a.p.x+a.radiusthis.x+this.width&&(a.dead=!0),a.p.y+a.radiusthis.y+this.height&&(a.dead=!0)):this.crossType=="bound"?(a.p.x-a.radiusthis.x+this.width&&(a.p.x=this.x+this.width-a.radius,a.v.x*=-1),a.p.y-a.radiusthis.y+this.height&&(a.p.y=this.y+this.height-a.radius,a.v.y*=-1)):this.crossType=="cross"&&(a.p.x+a.radiusthis.x+this.width&&a.v.x>=0&&(a.p.x=this.x-a.radius),a.p.y+a.radiusthis.y+this.height&&a.v.y>=0&&(a.p.y=this.y-a.radius))},c.RectZone=be,c.Util.inherits(bf,c.Zone),bf.prototype.reset=function(a,b,d,e){this.imageData=a,this.x=c.Util.initValue(b,0),this.y=c.Util.initValue(d,0),this.d=c.Util.initValue(e,2),this.vectors=[],this.setVectors()},bf.prototype.setVectors=function(){var a,b,c=this.imageData.width,d=this.imageData.height;for(a=0;a>0)*c+(a>>0))*4;this.imageData.data[e+3]>0&&this.vectors.push({x:a+this.x,y:b+this.y})}return this.vector},bf.prototype.getBound=function(a,b){var c=((b>>0)*this.imageData.width+(a>>0))*4;return this.imageData.data[c+3]>0?!0:!1},bf.prototype.getPosition=function(){return this.vector.copy(this.vectors[Math.floor(Math.random()*this.vectors.length)])},bf.prototype.getColor=function(a,b){a-=this.x,b-=this.y;var c=((b>>0)*this.imageData.width+(a>>0))*4;return{r:this.imageData.data[c],g:this.imageData.data[c+1],b:this.imageData.data[c+2],a:this.imageData.data[c+3]}},bf.prototype.crossing=function(a){this.crossType=="dead"?this.getBound(a.p.x-this.x,a.p.y-this.y)?a.dead=!0:a.dead=!1:this.crossType=="bound"&&(this.getBound(a.p.x-this.x,a.p.y-this.y)||a.v.negate())},c.ImageZone=bf;var bg=function(){if(a.console&&a.console.log){var b=arguments;if(typeof arguments[0]=="string")if(arguments[0].indexOf("+")==0){var c=parseInt(arguments[0]);bg.once behaviour-attraction1 + - + diff --git a/example/game/quarkjs/squirrel.html b/example/game/quarkjs/squirrel.html index c01bf6f..f064f58 100644 --- a/example/game/quarkjs/squirrel.html +++ b/example/game/quarkjs/squirrel.html @@ -4,6 +4,7 @@ Squirrel - Quark Framework Tutorials + - + diff --git a/example/render/pixel/google.html b/example/render/pixel/google.html index 75f9263..a472fef 100644 --- a/example/render/pixel/google.html +++ b/example/render/pixel/google.html @@ -2,6 +2,7 @@ proton.js-render-pixelrender +