Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pausing a tween from a Group removes that tween from the Group's array. #664

Open
NyaNguyen opened this issue Jul 15, 2023 · 5 comments
Open

Comments

@NyaNguyen
Copy link

I'm not sure if that was intentional, but when we access the array of tweens from a Group using .getAll() and execute .pause() on one, that tweens gets removed from that Group's tween array. I find that weird as pausing a tween should not remove it. I understand if we did a .stop() on it that it would get terminated, but not .pause().

Also, it would be nice to have a .pauseAll() function to be used on a Group. Obviously, we can access the array of tweens, but like the issue I brought up, pausing each tween removes them completely.

@trusktr
Copy link
Member

trusktr commented Jul 15, 2023

This seems unintuitive indeed, so will mark as a bug. Do you have a sample? Here's a starting point: https://codepen.io/trusktr/pen/OJRQjZw

@NyaNguyen
Copy link
Author

Not sure if this small code was required, but here is the demonstration that the tween from a Group's array gets removed after pausing a tween:

const DATA = { 'X': 0 };
const TWEEN_DATA = { 'X': 1000 };
const TWEEN_GROUP = new TWEEN.Group();

new TWEEN.Tween(DATA, TWEEN_GROUP).to(TWEEN_DATA).duration(1000).onUpdate(UpdateCallback).start();

function UpdateCallback() {
    console.log(DATA);
}

function Animate() {
    requestAnimationFrame(Animate);
    TWEEN_GROUP.update();
}

Animate();

setTimeout(function () {
    console.log('Array Length Before Pausing: ' + TWEEN_GROUP.getAll().length);
    TWEEN_GROUP.getAll()[0].pause();
    console.log('Array Length After Pausing: ' + TWEEN_GROUP.getAll().length);
}, 500);

@trusktr
Copy link
Member

trusktr commented Jul 16, 2023

Yeah, totally I see that. I meant, do you have an example of a problem this is causing?

Even long ago before TWEEN was made into a Group, and before there was pause(), Tweens were added or removed on start/stop:

var TWEEN = TWEEN || (function () {

So we need to see an example of the problem.

@MasatoMakino
Copy link
Contributor

MasatoMakino commented Aug 15, 2023

I found a code related to this issue.

tween.js/src/Tween.ts

Lines 284 to 285 in e01bbdb

// eslint-disable-next-line
this._group && this._group.remove(this as any)

tween.js/src/Tween.ts

Lines 301 to 302 in e01bbdb

// eslint-disable-next-line
this._group && this._group.add(this as any)

All tween instances hold a reference to a group. On pause, tween removes itself from a group and re-adds itself on resume.

tween.js/src/Tween.ts

Lines 393 to 394 in e01bbdb

update(time = now(), autoStart = true): boolean {
if (this._isPaused) return true

Tween.update() skips paused tween. Because of this, it is not necessary to remove and re-add tweens to a group.


The downside of this behavior is that it can be confusing when users use Group to manage and manipulate their tween.

The advantage of this behavior is that the update loop is faster if the mainGroup holds a large number of paused tween.

@trusktr
Copy link
Member

trusktr commented Jan 15, 2024

Ah indeed, should be easy to implement!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants