From cb61da5befba9167e4892a4c1157d5b7d671d69f Mon Sep 17 00:00:00 2001 From: houfio <26674115+houfio@users.noreply.github.com> Date: Fri, 24 Jul 2020 23:41:59 +0200 Subject: [PATCH] Add update callback --- src/Tween.ts | 9 +++++++++ test/unit/Tween-test.js | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Tween.ts b/src/Tween.ts index 8041a913..b9a3a0c0 100644 --- a/src/Tween.ts +++ b/src/Tween.ts @@ -38,6 +38,7 @@ class TweenEngine { onPause: Function; onReset: Function; onFinish: Function; + onUpdate: Function; constructor(prop, propFunc, func, begin, finish, duration, yoyo) { this.prop = prop; @@ -129,6 +130,7 @@ class TweenEngine { } update() { this.setPosition(this.getPosition(this._time)); + this.fire('onUpdate'); } onEnterFrame() { var t = this.getTimer() - this._startTime; @@ -179,6 +181,7 @@ export class Tween { _id: number; onFinish: Function; onReset: Function; + onUpdate: Function; constructor(config) { var that = this, @@ -249,6 +252,7 @@ export class Tween { // callbacks this.onFinish = config.onFinish; this.onReset = config.onReset; + this.onUpdate = config.onUpdate; } _addAttr(key, end) { var node = this.node, @@ -437,6 +441,11 @@ export class Tween { this.onReset(); } }; + this.tween.onUpdate = () => { + if (this.onUpdate) { + this.onUpdate.call(this); + } + } } /** * play diff --git a/test/unit/Tween-test.js b/test/unit/Tween-test.js index 677ea4f1..61f51d97 100644 --- a/test/unit/Tween-test.js +++ b/test/unit/Tween-test.js @@ -252,6 +252,23 @@ suite('Tween', function () { }, 50); }); + test('tween to call update callback', function (done) { + var stage = addStage(); + var updateCount = 0; + + stage.to({ + x: 10, + duration: 0.01, + onUpdate: function () { + updateCount++; + }, + onFinish: function () { + assert(updateCount === 3, 'updateCount should equal 3'); + done(); + }, + }); + }); + suite('tween array with different length', function () { test('prepare array closed', function () { var start = [0, 0, 10, 0, 10, 10];