Skip to content

Commit 79fd510

Browse files
committed
BREAKING: no longer auto-start tweens by default when calling tween.update(). To restore previous behavior for the whole app, set the static Tween.autoStartOnUpdate to true.
1 parent c92f761 commit 79fd510

File tree

8 files changed

+96
-10
lines changed

8 files changed

+96
-10
lines changed

dist/tween.amd.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ define(['exports'], (function (exports) { 'use strict';
601601
};
602602
Tween.prototype.end = function () {
603603
this._goToEnd = true;
604-
this.update(Infinity);
604+
this.update(this._startTime + this._duration);
605605
return this;
606606
};
607607
Tween.prototype.pause = function (time) {
@@ -712,12 +712,16 @@ define(['exports'], (function (exports) { 'use strict';
712712
* @returns true if the tween is still playing after the update, false
713713
* otherwise (calling update on a paused tween still returns true because
714714
* it is still playing, just paused).
715+
*
716+
* @param autoStart - When true, calling update will implicitly call start()
717+
* as well. Note, if you stop() or end() the tween, but are still calling
718+
* update(), it will start again!
715719
*/
716720
Tween.prototype.update = function (time, autoStart) {
717721
var _this = this;
718722
var _a;
719723
if (time === void 0) { time = now(); }
720-
if (autoStart === void 0) { autoStart = true; }
724+
if (autoStart === void 0) { autoStart = Tween.autoStartOnUpdate; }
721725
if (this._isPaused)
722726
return true;
723727
var property;
@@ -864,6 +868,7 @@ define(['exports'], (function (exports) { 'use strict';
864868
}
865869
this._valuesEnd[property] = tmp;
866870
};
871+
Tween.autoStartOnUpdate = false;
867872
return Tween;
868873
}());
869874

dist/tween.cjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ var Tween = /** @class */ (function () {
603603
};
604604
Tween.prototype.end = function () {
605605
this._goToEnd = true;
606-
this.update(Infinity);
606+
this.update(this._startTime + this._duration);
607607
return this;
608608
};
609609
Tween.prototype.pause = function (time) {
@@ -714,12 +714,16 @@ var Tween = /** @class */ (function () {
714714
* @returns true if the tween is still playing after the update, false
715715
* otherwise (calling update on a paused tween still returns true because
716716
* it is still playing, just paused).
717+
*
718+
* @param autoStart - When true, calling update will implicitly call start()
719+
* as well. Note, if you stop() or end() the tween, but are still calling
720+
* update(), it will start again!
717721
*/
718722
Tween.prototype.update = function (time, autoStart) {
719723
var _this = this;
720724
var _a;
721725
if (time === void 0) { time = now(); }
722-
if (autoStart === void 0) { autoStart = true; }
726+
if (autoStart === void 0) { autoStart = Tween.autoStartOnUpdate; }
723727
if (this._isPaused)
724728
return true;
725729
var property;
@@ -866,6 +870,7 @@ var Tween = /** @class */ (function () {
866870
}
867871
this._valuesEnd[property] = tmp;
868872
};
873+
Tween.autoStartOnUpdate = false;
869874
return Tween;
870875
}());
871876

dist/tween.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ declare const Interpolation: {
5353
*/
5454

5555
declare class Tween<T extends UnknownProps = any> {
56+
static autoStartOnUpdate: boolean;
5657
private _isPaused;
5758
private _pauseStart;
5859
private _valuesStart;
@@ -143,6 +144,10 @@ declare class Tween<T extends UnknownProps = any> {
143144
* @returns true if the tween is still playing after the update, false
144145
* otherwise (calling update on a paused tween still returns true because
145146
* it is still playing, just paused).
147+
*
148+
* @param autoStart - When true, calling update will implicitly call start()
149+
* as well. Note, if you stop() or end() the tween, but are still calling
150+
* update(), it will start again!
146151
*/
147152
update(time?: number, autoStart?: boolean): boolean;
148153
private _updateProperties;

dist/tween.esm.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ var Tween = /** @class */ (function () {
599599
};
600600
Tween.prototype.end = function () {
601601
this._goToEnd = true;
602-
this.update(Infinity);
602+
this.update(this._startTime + this._duration);
603603
return this;
604604
};
605605
Tween.prototype.pause = function (time) {
@@ -710,12 +710,16 @@ var Tween = /** @class */ (function () {
710710
* @returns true if the tween is still playing after the update, false
711711
* otherwise (calling update on a paused tween still returns true because
712712
* it is still playing, just paused).
713+
*
714+
* @param autoStart - When true, calling update will implicitly call start()
715+
* as well. Note, if you stop() or end() the tween, but are still calling
716+
* update(), it will start again!
713717
*/
714718
Tween.prototype.update = function (time, autoStart) {
715719
var _this = this;
716720
var _a;
717721
if (time === void 0) { time = now(); }
718-
if (autoStart === void 0) { autoStart = true; }
722+
if (autoStart === void 0) { autoStart = Tween.autoStartOnUpdate; }
719723
if (this._isPaused)
720724
return true;
721725
var property;
@@ -862,6 +866,7 @@ var Tween = /** @class */ (function () {
862866
}
863867
this._valuesEnd[property] = tmp;
864868
};
869+
Tween.autoStartOnUpdate = false;
865870
return Tween;
866871
}());
867872

dist/tween.umd.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@
605605
};
606606
Tween.prototype.end = function () {
607607
this._goToEnd = true;
608-
this.update(Infinity);
608+
this.update(this._startTime + this._duration);
609609
return this;
610610
};
611611
Tween.prototype.pause = function (time) {
@@ -716,12 +716,16 @@
716716
* @returns true if the tween is still playing after the update, false
717717
* otherwise (calling update on a paused tween still returns true because
718718
* it is still playing, just paused).
719+
*
720+
* @param autoStart - When true, calling update will implicitly call start()
721+
* as well. Note, if you stop() or end() the tween, but are still calling
722+
* update(), it will start again!
719723
*/
720724
Tween.prototype.update = function (time, autoStart) {
721725
var _this = this;
722726
var _a;
723727
if (time === void 0) { time = now(); }
724-
if (autoStart === void 0) { autoStart = true; }
728+
if (autoStart === void 0) { autoStart = Tween.autoStartOnUpdate; }
725729
if (this._isPaused)
726730
return true;
727731
var property;
@@ -868,6 +872,7 @@
868872
}
869873
this._valuesEnd[property] = tmp;
870874
};
875+
Tween.autoStartOnUpdate = false;
871876
return Tween;
872877
}());
873878

examples/19_end_tween.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!-- shows what happens when we call tween.end() -->
2+
3+
<style>
4+
#box {
5+
background-color: deeppink;
6+
width: 100px;
7+
height: 100px;
8+
}
9+
</style>
10+
11+
<div id="box"></div>
12+
13+
<button id="end">tween.end()</button>
14+
15+
<script type="module">
16+
import {Tween, Easing} from '../dist/tween.esm.js'
17+
18+
const box = document.getElementById('box') // Get the element we want to animate.
19+
20+
const coords = {x: 0, y: 0} // Start at (0, 0)
21+
22+
const tween = new Tween(coords, false) // Create a new tween that modifies 'coords'.
23+
.to({x: 300, y: 200}, 5000) // Move to (300, 200) in 1 second.
24+
.easing(Easing.Quadratic.InOut) // Use an easing function to make the animation smooth.
25+
.onUpdate(() => {
26+
// Called after tween.js updates 'coords'.
27+
// Move 'box' to the position described by 'coords' with a CSS translation.
28+
box.style.setProperty('transform', 'translate(' + coords.x + 'px, ' + coords.y + 'px)')
29+
30+
//
31+
console.log(coords, tween.isPlaying())
32+
})
33+
.onComplete(() => {
34+
console.log('onComplete')
35+
})
36+
.start() // Start the tween immediately.
37+
38+
// Setup the animation loop.
39+
function animate(time) {
40+
tween.update(time)
41+
requestAnimationFrame(animate)
42+
}
43+
44+
requestAnimationFrame(animate)
45+
46+
document.querySelector('#end').addEventListener('click', () => {
47+
console.log('tween.end')
48+
tween.end()
49+
console.log(tween)
50+
})
51+
</script>

src/Tween.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import type {InterpolationFunction} from './Interpolation'
1818
import type Group from './Group'
1919

2020
export class Tween<T extends UnknownProps = any> {
21+
static autoStartOnUpdate = false
22+
2123
private _isPaused = false
2224
private _pauseStart = 0
2325
private _valuesStart: UnknownProps = {}
@@ -292,7 +294,7 @@ export class Tween<T extends UnknownProps = any> {
292294

293295
end(): this {
294296
this._goToEnd = true
295-
this.update(Infinity)
297+
this.update(this._startTime + this._duration)
296298
return this
297299
}
298300

@@ -430,8 +432,12 @@ export class Tween<T extends UnknownProps = any> {
430432
* @returns true if the tween is still playing after the update, false
431433
* otherwise (calling update on a paused tween still returns true because
432434
* it is still playing, just paused).
435+
*
436+
* @param autoStart - When true, calling update will implicitly call start()
437+
* as well. Note, if you stop() or end() the tween, but are still calling
438+
* update(), it will start again!
433439
*/
434-
update(time = now(), autoStart = true): boolean {
440+
update(time = now(), autoStart = Tween.autoStartOnUpdate): boolean {
435441
if (this._isPaused) return true
436442

437443
let property

src/tests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ export const tests = {
520520
},
521521

522522
'Ensure tweens start without calling start() method.'(test: Test): void {
523+
TWEEN.Tween.autoStartOnUpdate = true
524+
523525
const obj = {x: 0},
524526
t = new TWEEN.Tween(obj)
525527

@@ -538,6 +540,8 @@ export const tests = {
538540
test.deepEqual(obj.x, 1000)
539541
test.deepEqual(started, false)
540542

543+
TWEEN.Tween.autoStartOnUpdate = false
544+
541545
test.done()
542546
},
543547

0 commit comments

Comments
 (0)