Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added autostart #18

Merged
merged 1 commit into from

2 participants

@jocafa

If you call autostart now, tween will automatically create the interval when tweens are added, and destroy it when the tweens array is empty. Normally this isn't an issue, but having that noop interval around can get in the way of debugging other things, particularly if you want to use your js debugger's "break on next" functionality.

@sole sole merged commit 35a4036 into from
@sole
Owner

I've finally "merged" this into the main branch :-)

I changed a few things here and there, mostly because the patch couldn't be automagically merged, but you get the idea. Many thanks for your contribution! ^^

@sole sole referenced this pull request
Closed

Some Changes #24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 35 additions and 6 deletions.
  1. +4 −3 build/Tween.js
  2. +31 −3 src/Tween.js
View
7 build/Tween.js
@@ -1,7 +1,8 @@
// tween.js r2 - http://github.com/sole/tween.js
-var TWEEN=TWEEN||function(){var a,e,c,d,f=[];return{start:function(g){c=setInterval(this.update,1E3/(g||60))},stop:function(){clearInterval(c)},add:function(g){f.push(g)},getAll:function(){return f},removeAll:function(){f=[]},remove:function(g){a=f.indexOf(g);a!==-1&&f.splice(a,1)},update:function(){a=0;e=f.length;for(d=(new Date).getTime();a<e;)if(f[a].update(d))a++;else{f.splice(a,1);e--}}}}();
-TWEEN.Tween=function(a){var e={},c={},d={},f=1E3,g=0,j=null,n=TWEEN.Easing.Linear.EaseNone,k=null,l=null,m=null;this.to=function(b,h){if(h!==null)f=h;for(var i in b)if(a[i]!==null)d[i]=b[i];return this};this.start=function(){TWEEN.add(this);j=(new Date).getTime()+g;for(var b in d)if(a[b]!==null){e[b]=a[b];c[b]=d[b]-a[b]}return this};this.stop=function(){TWEEN.remove(this);return this};this.delay=function(b){g=b;return this};this.easing=function(b){n=b;return this};this.chain=function(b){k=b};this.onUpdate=
-function(b){l=b;return this};this.onComplete=function(b){m=b;return this};this.update=function(b){var h,i;if(b<j)return true;b=(b-j)/f;b=b>1?1:b;i=n(b);for(h in c)a[h]=e[h]+c[h]*i;l!==null&&l.call(a,i);if(b==1){m!==null&&m.call(a);k!==null&&k.start();return false}return true}};TWEEN.Easing={Linear:{},Quadratic:{},Cubic:{},Quartic:{},Quintic:{},Sinusoidal:{},Exponential:{},Circular:{},Elastic:{},Back:{},Bounce:{}};TWEEN.Easing.Linear.EaseNone=function(a){return a};
+var TWEEN=TWEEN||function(){var a,e,c,d,k=60,j=false,f=[];return{setFPS:function(g){k=g||60},start:function(g){this.setFPS(g);c=setInterval(this.update,1E3/k)},stop:function(){clearInterval(c)},autostart:function(g){j=true;this.start(g)},add:function(g){f.push(g);j&&!c&&this.start()},getAll:function(){return f},removeAll:function(){f=[]},remove:function(g){a=f.indexOf(g);a!==-1&&f.splice(a,1)},update:function(){a=0;e=f.length;for(d=(new Date).getTime();a<e;)if(f[a].update(d))a++;else{f.splice(a,1);
+e--}e==0&&j==true&&this.stop()}}}();
+TWEEN.Tween=function(a){var e={},c={},d={},k=1E3,j=0,f=null,g=TWEEN.Easing.Linear.EaseNone,l=null,m=null,n=null;this.to=function(b,h){if(h!==null)k=h;for(var i in b)if(a[i]!==null)d[i]=b[i];return this};this.start=function(){TWEEN.add(this);f=(new Date).getTime()+j;for(var b in d)if(a[b]!==null){e[b]=a[b];c[b]=d[b]-a[b]}return this};this.stop=function(){TWEEN.remove(this);return this};this.delay=function(b){j=b;return this};this.easing=function(b){g=b;return this};this.chain=function(b){l=b};this.onUpdate=
+function(b){m=b;return this};this.onComplete=function(b){n=b;return this};this.update=function(b){var h,i;if(b<f)return true;b=(b-f)/k;b=b>1?1:b;i=g(b);for(h in c)a[h]=e[h]+c[h]*i;m!==null&&m.call(a,i);if(b==1){n!==null&&n.call(a);l!==null&&l.start();return false}return true}};TWEEN.Easing={Linear:{},Quadratic:{},Cubic:{},Quartic:{},Quintic:{},Sinusoidal:{},Exponential:{},Circular:{},Elastic:{},Back:{},Bounce:{}};TWEEN.Easing.Linear.EaseNone=function(a){return a};
TWEEN.Easing.Quadratic.EaseIn=function(a){return a*a};TWEEN.Easing.Quadratic.EaseOut=function(a){return-a*(a-2)};TWEEN.Easing.Quadratic.EaseInOut=function(a){if((a*=2)<1)return 0.5*a*a;return-0.5*(--a*(a-2)-1)};TWEEN.Easing.Cubic.EaseIn=function(a){return a*a*a};TWEEN.Easing.Cubic.EaseOut=function(a){return--a*a*a+1};TWEEN.Easing.Cubic.EaseInOut=function(a){if((a*=2)<1)return 0.5*a*a*a;return 0.5*((a-=2)*a*a+2)};TWEEN.Easing.Quartic.EaseIn=function(a){return a*a*a*a};
TWEEN.Easing.Quartic.EaseOut=function(a){return-(--a*a*a*a-1)};TWEEN.Easing.Quartic.EaseInOut=function(a){if((a*=2)<1)return 0.5*a*a*a*a;return-0.5*((a-=2)*a*a*a-2)};TWEEN.Easing.Quintic.EaseIn=function(a){return a*a*a*a*a};TWEEN.Easing.Quintic.EaseOut=function(a){return(a-=1)*a*a*a*a+1};TWEEN.Easing.Quintic.EaseInOut=function(a){if((a*=2)<1)return 0.5*a*a*a*a*a;return 0.5*((a-=2)*a*a*a*a+2)};TWEEN.Easing.Sinusoidal.EaseIn=function(a){return-Math.cos(a*Math.PI/2)+1};
TWEEN.Easing.Sinusoidal.EaseOut=function(a){return Math.sin(a*Math.PI/2)};TWEEN.Easing.Sinusoidal.EaseInOut=function(a){return-0.5*(Math.cos(Math.PI*a)-1)};TWEEN.Easing.Exponential.EaseIn=function(a){return a==0?0:Math.pow(2,10*(a-1))};TWEEN.Easing.Exponential.EaseOut=function(a){return a==1?1:-Math.pow(2,-10*a)+1};TWEEN.Easing.Exponential.EaseInOut=function(a){if(a==0)return 0;if(a==1)return 1;if((a*=2)<1)return 0.5*Math.pow(2,10*(a-1));return 0.5*(-Math.pow(2,-10*(a-1))+2)};
View
34 src/Tween.js
@@ -8,13 +8,21 @@
var TWEEN = TWEEN || ( function () {
- var i, tl, interval, time, tweens = [];
+ var i, tl, interval, time, fps = 60, auto = false, tweens = [];
return {
- start: function ( fps ) {
+ setFPS: function ( f ) {
- interval = setInterval( this.update, 1000 / ( fps || 60 ) );
+ fps = f || 60;
+
+ },
+
+ start: function ( f ) {
+
+ this.setFPS(f);
+
+ interval = setInterval( this.update, 1000 / fps );
},
@@ -24,10 +32,24 @@ var TWEEN = TWEEN || ( function () {
},
+ autostart: function (fps) {
+
+ auto = true;
+
+ this.start(fps);
+
+ },
+
add: function ( tween ) {
tweens.push( tween );
+ if (auto && !interval) {
+
+ this.start();
+
+ }
+
},
getAll: function() {
@@ -74,6 +96,12 @@ var TWEEN = TWEEN || ( function () {
}
+ if (tl == 0 && auto == true) {
+
+ this.stop();
+
+ }
+
}
};
Something went wrong with that request. Please try again.