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

Multiple .start() and .to(prop:[]) #602

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Tween.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class Tween<T extends UnknownProps> {
private _onStopCallback?: (object: T) => void
private _id = Sequence.nextId()
private _isChainStopped = false
private _hasSetupProperty = false

constructor(private _object: T, private _group: Group | false = mainGroup) {}

Expand All @@ -65,6 +66,7 @@ export class Tween<T extends UnknownProps> {
// currently no opt-out).
// for (const prop in properties) this._valuesEnd[prop] = properties[prop]
this._valuesEnd = Object.create(properties)
this._hasSetupProperty = false

if (duration !== undefined) {
this._duration = duration
Expand Down Expand Up @@ -146,7 +148,9 @@ export class Tween<T extends UnknownProps> {
endValues = endValues.map(this._handleRelativeValue.bind(this, startValue as number))

// Create a local copy of the Array with the start value at the front
_valuesEnd[property] = [startValue].concat(endValues)
if (!this._hasSetupProperty) {
_valuesEnd[property] = [_valuesStart[property] ?? startValue].concat(endValues)
}
}

// handle the deepness of the values
Expand Down Expand Up @@ -186,6 +190,7 @@ export class Tween<T extends UnknownProps> {
}
}
}
this._hasSetupProperty = true
}

stop(): this {
Expand Down
46 changes: 46 additions & 0 deletions src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,52 @@ export const tests = {
test.done()
},

'Tween.to(value:[number]) and Tween.to(value:number) return the same result after multiple .start()'(
test: Test,
): void {
const toNumber = {x: 1.0}
const toArray = {x: [1.0]}
const targetNumber = {x: 0.0}
const targetArray = {x: 0.0}
const tweenNumber = new TWEEN.Tween(targetNumber).to(toNumber, 100)
const tweenArray = new TWEEN.Tween(targetArray).to(toArray, 100)

tweenNumber.start(0)
tweenArray.start(0)
test.equal(targetNumber.x, targetArray.x)
TWEEN.update(37)
test.equal(targetNumber.x, targetArray.x)
TWEEN.update(50)
test.equal(targetNumber.x, targetArray.x)
TWEEN.update(100)
test.equal(targetNumber.x, targetArray.x)

tweenNumber.start(100)
tweenArray.start(100)
test.equal(targetNumber.x, targetArray.x)
TWEEN.update(137)
test.equal(targetNumber.x, targetArray.x)
TWEEN.update(150)
test.equal(targetNumber.x, targetArray.x)
TWEEN.update(200)
test.equal(targetNumber.x, targetArray.x)

//should keep _valuesStart after .to() function
tweenNumber.to({x: 2.0})
tweenArray.to({x: [2.0]})
tweenNumber.start(200)
tweenArray.start(200)
test.equal(targetNumber.x, targetArray.x)
TWEEN.update(237)
test.equal(targetNumber.x, targetArray.x)
TWEEN.update(250)
test.equal(targetNumber.x, targetArray.x)
TWEEN.update(300)
test.equal(targetNumber.x, targetArray.x)

test.done()
},

'Test TWEEN.Tween.update() with no arguments'(test: Test): void {
const clock = FakeTimers.install()
const targetNow = {x: 0.0}
Expand Down