tweenjs / tween.js

JavaScript/TypeScript animation engine
https://tweenjs.github.io/tween.js/
Other
9.85k stars 1.41k forks source link

Ability to pause and play #15

Closed AndyOGo closed 8 years ago

AndyOGo commented 13 years ago

Actuallay I needed such a function for my project. So I decided to add such functionality.

Hope this is usefull. Maybe it could get improved, for example with progressbar. Rewind, fast forward functions an so on. Just an idea

//global vars
_paused = false,
_pasueStart = null,

...

    this.pause = function () {
        if(_paused)
            return;

        _paused = true;
        _pauseStart = new Date().getTime();

        TWEEN.remove( this );
    };

    this.play = function () {
        if(!_paused)
            return;

        _paused = false;

        now = new Date().getTime();

        _startTime += now - _pauseStart;

        TWEEN.add( this );
    };
sole commented 13 years ago

Thanks! We're also thinking about how to add support for something on these lines too, so we'll have a look at your solution too.

AndrewRayCode commented 10 years ago

I also need this, and ecstatic to learn that this ticket is 2 years old, @sole

photonstorm commented 10 years ago

There are so many other tweening libraries available now, if this feature is needed so badly why not just use a different one? This for example: http://www.greensock.com/gsap-js/ (which claims, although I've never verified it, to be significantly faster than the competition too)

sole commented 10 years ago

Thanks for your interest! I'm sorry this hasn't been resolved in two years, but I guess no one has needed it so badly as to implement it and sending a PR, and etc etc.

The "problem" we have now is that the library does more things now than two years ago, so adding this might not be trivial. I personally haven't had this need, so I haven't really attempted to implement it either.

In short--sorry, but no one has done it yet. If someone does it nicely I will merge it in. Otherwise, maybe use other libraries as @photonstorm says :-)

photonstorm commented 10 years ago

Also anyone reading is welcome to use our implementation of tween.js too:

https://github.com/photonstorm/phaser/tree/master/src/tween

You'd have to remove a couple lines of Phaser specific code, but otherwise we added full pause / resume support (linked into a central game clock so the times update accurately), pauseAll/resumeAll functions and more importantly we split tween creation / destruction across 2 arrays. This resolves the issue tween.js has where if you nuke a tween during a custom callback (or as part of a chain) it throws reference errors in the main update loop.

sole commented 10 years ago

Wow @photonstorm, if that wasn't a sales pitch I don't know what it was :-P

We recently solved some issues with stopping tweens so maybe that's what you mention...

(this conversation is getting derailed)

AndrewRayCode commented 10 years ago

fyi I basically implemented this the same way @photonstorm except externally to the tween library. I didn't know tweenjs could take a time for update() (not documented). When my code pauses, I just store a pause time, and on resume store how long we were paused for, and update tween with the timestamp as if nothing ever happened. Creates some minor jumps on pausing / resuming probably due to minor timing differences but works for my needs

thednp commented 10 years ago

Why don't you fork and pull a merge for this?