Skip to content

Commit b898eec

Browse files
authored
Merge branch 'master' into PR-tweenjs#610-Freeze-Easing
2 parents b23bf7e + 720495c commit b898eec

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed

docs/user_guide.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ It is great for synchronising to other events or triggering actions you want to
351351

352352
The tweened object is passed in as the first parameter.
353353

354+
### onEveryStart
355+
356+
As per `onStart`, except that it _will_ be run on every repeat of the tween.
357+
358+
The tweened object is passed in as the first parameter.
359+
354360
### onStop
355361

356362
Executed when a tween is explicitly stopped via `stop()`, but not when it is completed normally, and before stopping any possible chained tween.
@@ -375,6 +381,115 @@ Executed whenever a tween has just finished one repetition and will begin anothe
375381

376382
The tweened object is passed in as the first parameter.
377383

384+
To clarify when `onStart`, `onEveryStart` and `onRepeat` are called, consider:
385+
386+
```javascript
387+
const obj = {x: 0}
388+
389+
const t = new TWEEN.Tween(obj)
390+
.to({x: 5}, 5)
391+
.repeat(Infinity)
392+
.onStart(() => {
393+
console.log('onStart')
394+
})
395+
.onRepeat(() => {
396+
console.log('onRepeat')
397+
})
398+
.onEveryStart(() => {
399+
console.log('onEveryStart')
400+
})
401+
.start(0)
402+
403+
for (let ticks = 0; ticks < 22; ticks += 1) {
404+
console.log('Tick', ticks)
405+
TWEEN.update(ticks)
406+
407+
console.log(obj)
408+
console.log()
409+
}
410+
```
411+
412+
The output would look like this, on the left as above, and on the right with `.delay(5)`:
413+
414+
```
415+
Tick 0 Tick 0
416+
onStart { x: 0 }
417+
onEveryStart
418+
{ x: 0 }
419+
420+
Tick 1 Tick 1
421+
{ x: 1 } { x: 0 }
422+
423+
Tick 2 Tick 2
424+
{ x: 2 } { x: 0 }
425+
426+
Tick 3 Tick 3
427+
{ x: 3 } { x: 0 }
428+
429+
Tick 4 Tick 4
430+
{ x: 4 } { x: 0 }
431+
432+
Tick 5 Tick 5
433+
onRepeat onStart
434+
{ x: 5 } onEveryStart
435+
{ x: 0 }
436+
437+
Tick 6 Tick 6
438+
onEveryStart { x: 1 }
439+
{ x: 1 }
440+
441+
Tick 7 Tick 7
442+
{ x: 2 } { x: 2 }
443+
444+
Tick 8 Tick 8
445+
{ x: 3 } { x: 3 }
446+
447+
Tick 9 Tick 9
448+
{ x: 4 } { x: 4 }
449+
450+
Tick 10 Tick 10
451+
onRepeat onRepeat
452+
{ x: 5 } { x: 5 }
453+
454+
Tick 11 Tick 11
455+
onEveryStart { x: 5 }
456+
{ x: 1 }
457+
458+
Tick 12 Tick 12
459+
{ x: 2 } { x: 5 }
460+
461+
Tick 13 Tick 13
462+
{ x: 3 } { x: 5 }
463+
464+
Tick 14 Tick 14
465+
{ x: 4 } { x: 5 }
466+
467+
Tick 15 Tick 15
468+
onRepeat onEveryStart
469+
{ x: 5 } { x: 0 }
470+
471+
Tick 16 Tick 16
472+
onEveryStart { x: 1 }
473+
{ x: 1 }
474+
475+
Tick 17 Tick 17
476+
{ x: 2 } { x: 2 }
477+
478+
Tick 18 Tick 18
479+
{ x: 3 } { x: 3 }
480+
481+
Tick 19 Tick 19
482+
{ x: 4 } { x: 4 }
483+
484+
Tick 20 Tick 20
485+
onRepeat onRepeat
486+
{ x: 5 } { x: 5 }
487+
488+
Tick 21 Tick 21
489+
onEveryStart { x: 5 }
490+
{ x: 1 }
491+
```
492+
378493
## Advanced tweening
379494

380495
### Relative values

src/Tween.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export class Tween<T extends UnknownProps> {
3838
private _chainedTweens: Array<Tween<any>> = []
3939
private _onStartCallback?: (object: T) => void
4040
private _onStartCallbackFired = false
41+
private _onEveryStartCallback?: (object: T) => void
42+
private _onEveryStartCallbackFired = false
4143
private _onUpdateCallback?: (object: T, elapsed: number) => void
4244
private _onRepeatCallback?: (object: T) => void
4345
private _onCompleteCallback?: (object: T) => void
@@ -105,6 +107,7 @@ export class Tween<T extends UnknownProps> {
105107
this._isPaused = false
106108

107109
this._onStartCallbackFired = false
110+
this._onEveryStartCallbackFired = false
108111

109112
this._isChainStopped = false
110113

@@ -146,7 +149,9 @@ export class Tween<T extends UnknownProps> {
146149
endValues = endValues.map(this._handleRelativeValue.bind(this, startValue as number))
147150

148151
// Create a local copy of the Array with the start value at the front
149-
_valuesEnd[property] = [startValue].concat(endValues)
152+
if (_valuesStart[property] === undefined) {
153+
_valuesEnd[property] = [startValue].concat(endValues)
154+
}
150155
}
151156

152157
// handle the deepness of the values
@@ -304,6 +309,11 @@ export class Tween<T extends UnknownProps> {
304309
return this
305310
}
306311

312+
onEveryStart(callback?: (object: T) => void): this {
313+
this._onEveryStartCallback = callback
314+
return this
315+
}
316+
307317
onUpdate(callback?: (object: T, elapsed: number) => void): this {
308318
this._onUpdateCallback = callback
309319
return this
@@ -358,6 +368,14 @@ export class Tween<T extends UnknownProps> {
358368
this._onStartCallbackFired = true
359369
}
360370

371+
if (this._onEveryStartCallbackFired === false) {
372+
if (this._onEveryStartCallback) {
373+
this._onEveryStartCallback(this._object)
374+
}
375+
376+
this._onEveryStartCallbackFired = true
377+
}
378+
361379
elapsed = (time - this._startTime) / this._duration
362380
elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed
363381

@@ -406,6 +424,8 @@ export class Tween<T extends UnknownProps> {
406424
this._onRepeatCallback(this._object)
407425
}
408426

427+
this._onEveryStartCallbackFired = false
428+
409429
return true
410430
} else {
411431
if (this._onCompleteCallback) {

src/tests.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ export const tests = {
203203
test.ok(t.onStart() instanceof TWEEN.Tween)
204204
test.equal(t.onStart(), t)
205205

206+
test.ok(t.onEveryStart() instanceof TWEEN.Tween)
207+
test.equal(t.onEveryStart(), t)
208+
206209
test.ok(t.onStop() instanceof TWEEN.Tween)
207210
test.equal(t.onStop(), t)
208211

@@ -1016,6 +1019,36 @@ export const tests = {
10161019
test.done()
10171020
},
10181021

1022+
'Test TWEEN.Tween.onEveryStart'(test: Test): void {
1023+
const obj = {},
1024+
t = new TWEEN.Tween(obj)
1025+
let counter = 0
1026+
1027+
t.to({x: 2}, 500)
1028+
t.delay(500)
1029+
t.repeat(Infinity)
1030+
t.onEveryStart(function (): void {
1031+
counter++
1032+
})
1033+
1034+
test.deepEqual(counter, 0)
1035+
1036+
t.start(0)
1037+
TWEEN.update(0)
1038+
test.deepEqual(counter, 0, 'onEveryStart callback not called before delayed start')
1039+
1040+
TWEEN.update(500)
1041+
test.deepEqual(counter, 1, 'onEveryStart callback called at delayed start')
1042+
1043+
TWEEN.update(1000)
1044+
test.deepEqual(counter, 1, 'onEveryStart callback not called before delayed repeat start')
1045+
1046+
TWEEN.update(1500)
1047+
test.deepEqual(counter, 2, 'onEveryStart callback called at delayed repeat start')
1048+
1049+
test.done()
1050+
},
1051+
10191052
'Test TWEEN.Tween.onStop'(test: Test): void {
10201053
const obj = {},
10211054
t = new TWEEN.Tween(obj)
@@ -2153,6 +2186,20 @@ export const tests = {
21532186
test.done()
21542187
},
21552188

2189+
"Test TWEEN.to(ends) shouldn't grow endless on ends value"(test: Test): void {
2190+
const target = {y: 0}
2191+
const ends = {y: [100, 200]}
2192+
const tween = new TWEEN.Tween(target).to(ends, 1000)
2193+
2194+
tween.stop().start(0)
2195+
tween.stop().start(0)
2196+
2197+
TWEEN.update(250)
2198+
test.equal(target.y, 50)
2199+
2200+
test.done()
2201+
},
2202+
21562203
'Test TWEEN.Tween.update() with no arguments'(test: Test): void {
21572204
const clock = FakeTimers.install()
21582205
const targetNow = {x: 0.0}

0 commit comments

Comments
 (0)