From 840d31bf10da465d9c1ddb64912202fd0d3ccd86 Mon Sep 17 00:00:00 2001 From: "Guillermo Q. Chavez" Date: Mon, 9 Sep 2019 19:29:09 -0600 Subject: [PATCH] Add functions to pause and resume a tween. --- examples/14.pause_tween.html | 82 ++++++++++++++++++++++++++++++++++++ src/Tween.js | 45 ++++++++++++++++++++ test/unit/tests.js | 60 ++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 examples/14.pause_tween.html diff --git a/examples/14.pause_tween.html b/examples/14.pause_tween.html new file mode 100644 index 0000000..a0f9c72 --- /dev/null +++ b/examples/14.pause_tween.html @@ -0,0 +1,82 @@ + + + + Tween.js / pause tween + + + + +
+

tween.js

+

14 _ pause tween

+
+

+ + , then + . +

+
+
+
+ 0% +
+
+ + + + + + diff --git a/src/Tween.js b/src/Tween.js index e5539fe..5e10793 100644 --- a/src/Tween.js +++ b/src/Tween.js @@ -121,6 +121,8 @@ else { TWEEN.Tween = function (object, group) { + this._isPaused = false; + this._pauseStart = null; this._object = object; this._valuesStart = {}; this._valuesEnd = {}; @@ -156,6 +158,10 @@ TWEEN.Tween.prototype = { return this._isPlaying; }, + isPaused: function () { + return this._isPaused; + }, + to: function (properties, duration) { this._valuesEnd = Object.create(properties); @@ -179,6 +185,8 @@ TWEEN.Tween.prototype = { this._isPlaying = true; + this._isPaused = false; + this._onStartCallbackFired = false; this._startTime = time !== undefined ? typeof time === 'string' ? TWEEN.now() + parseFloat(time) : time : TWEEN.now(); @@ -228,8 +236,11 @@ TWEEN.Tween.prototype = { } this._group.remove(this); + this._isPlaying = false; + this._isPaused = false; + if (this._onStopCallback !== null) { this._onStopCallback(this._object); } @@ -246,6 +257,40 @@ TWEEN.Tween.prototype = { }, + pause: function() { + + if (this._isPaused || !this._isPlaying) { + return this; + } + + this._isPaused = true; + + this._pauseStart = TWEEN.now(); + + this._group.remove(this); + + return this; + + }, + + resume: function() { + + if (!this._isPaused || !this._isPlaying) { + return this; + } + + this._isPaused = false; + + this._startTime += TWEEN.now() - this._pauseStart; + + this._pauseStart = 0; + + this._group.add(this); + + return this; + + }, + stopChainedTweens: function () { for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) { diff --git a/test/unit/tests.js b/test/unit/tests.js index 04a27b7..ec8aa1c 100644 --- a/test/unit/tests.js +++ b/test/unit/tests.js @@ -1424,6 +1424,66 @@ }, + // TODO: not really sure how to test this. Advice appreciated! + 'Test TWEEN.Tween.pause()': function(test) { + + var obj = { }, + t = new TWEEN.Tween( obj ); + + t.to( { }, 1000 ); + + TWEEN.removeAll(); + test.equal( TWEEN.getAll().length, 0 ); // TODO move to TWEEN test + + t.start( 0 ); + + test.equal( TWEEN.getAll().length, 1 ); // TODO ditto + + test.equal( t.isPaused(), false ); + + t.pause(); + + test.equal( t.isPaused(), true ); + + test.equal( TWEEN.getAll().length, 0 ); // TODO move to TWEEN test + + test.done(); + + }, + + // TODO: not really sure how to test this. Advice appreciated! + 'Test TWEEN.Tween.resume()': function(test) { + + var obj = { }, + t = new TWEEN.Tween( obj ); + + t.to( { }, 1000 ); + + TWEEN.removeAll(); + test.equal( TWEEN.getAll().length, 0 ); // TODO move to TWEEN test + + t.start( 0 ); + + test.equal( TWEEN.getAll().length, 1 ); // TODO ditto + + test.equal( t.isPaused(), false ); + + t.pause(); + + test.equal( t.isPaused(), true ); + + test.equal( TWEEN.getAll().length, 0 ); + + t.resume(); + + test.equal( TWEEN.getAll().length, 1 ); + + test.equal( t.isPaused(), false ); + + test.done(); + + }, + 'Arrays in the object passed to to() are not modified by start().': function(test) {