Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
trusktr authored Dec 2, 2021
2 parents cfde568 + 9f8c56c commit d202e0d
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
115 changes: 115 additions & 0 deletions docs/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ It is great for synchronising to other events or triggering actions you want to

The tweened object is passed in as the first parameter.

### onEveryStart

As per `onStart`, except that it _will_ be run on every repeat of the tween.

The tweened object is passed in as the first parameter.

### onStop

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

The tweened object is passed in as the first parameter.

To clarify when `onStart`, `onEveryStart` and `onRepeat` are called, consider:

```javascript
const obj = {x: 0}

const t = new TWEEN.Tween(obj)
.to({x: 5}, 5)
.repeat(Infinity)
.onStart(() => {
console.log('onStart')
})
.onRepeat(() => {
console.log('onRepeat')
})
.onEveryStart(() => {
console.log('onEveryStart')
})
.start(0)

for (let ticks = 0; ticks < 22; ticks += 1) {
console.log('Tick', ticks)
TWEEN.update(ticks)

console.log(obj)
console.log()
}
```

The output would look like this, on the left as above, and on the right with `.delay(5)`:

```
Tick 0 Tick 0
onStart { x: 0 }
onEveryStart
{ x: 0 }
Tick 1 Tick 1
{ x: 1 } { x: 0 }
Tick 2 Tick 2
{ x: 2 } { x: 0 }
Tick 3 Tick 3
{ x: 3 } { x: 0 }
Tick 4 Tick 4
{ x: 4 } { x: 0 }
Tick 5 Tick 5
onRepeat onStart
{ x: 5 } onEveryStart
{ x: 0 }
Tick 6 Tick 6
onEveryStart { x: 1 }
{ x: 1 }
Tick 7 Tick 7
{ x: 2 } { x: 2 }
Tick 8 Tick 8
{ x: 3 } { x: 3 }
Tick 9 Tick 9
{ x: 4 } { x: 4 }
Tick 10 Tick 10
onRepeat onRepeat
{ x: 5 } { x: 5 }
Tick 11 Tick 11
onEveryStart { x: 5 }
{ x: 1 }
Tick 12 Tick 12
{ x: 2 } { x: 5 }
Tick 13 Tick 13
{ x: 3 } { x: 5 }
Tick 14 Tick 14
{ x: 4 } { x: 5 }
Tick 15 Tick 15
onRepeat onEveryStart
{ x: 5 } { x: 0 }
Tick 16 Tick 16
onEveryStart { x: 1 }
{ x: 1 }
Tick 17 Tick 17
{ x: 2 } { x: 2 }
Tick 18 Tick 18
{ x: 3 } { x: 3 }
Tick 19 Tick 19
{ x: 4 } { x: 4 }
Tick 20 Tick 20
onRepeat onRepeat
{ x: 5 } { x: 5 }
Tick 21 Tick 21
onEveryStart { x: 5 }
{ x: 1 }
```

## Advanced tweening

### Relative values
Expand Down
18 changes: 18 additions & 0 deletions src/Tween.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class Tween<T extends UnknownProps> {
private _chainedTweens: Array<Tween<any>> = []
private _onStartCallback?: (object: T) => void
private _onStartCallbackFired = false
private _onEveryStartCallback?: (object: T) => void
private _onEveryStartCallbackFired = false
private _onUpdateCallback?: (object: T, elapsed: number) => void
private _onRepeatCallback?: (object: T) => void
private _onCompleteCallback?: (object: T) => void
Expand Down Expand Up @@ -105,6 +107,7 @@ export class Tween<T extends UnknownProps> {
this._isPaused = false

this._onStartCallbackFired = false
this._onEveryStartCallbackFired = false

this._isChainStopped = false

Expand Down Expand Up @@ -306,6 +309,11 @@ export class Tween<T extends UnknownProps> {
return this
}

onEveryStart(callback?: (object: T) => void): this {
this._onEveryStartCallback = callback
return this
}

onUpdate(callback?: (object: T, elapsed: number) => void): this {
this._onUpdateCallback = callback
return this
Expand Down Expand Up @@ -360,6 +368,14 @@ export class Tween<T extends UnknownProps> {
this._onStartCallbackFired = true
}

if (this._onEveryStartCallbackFired === false) {
if (this._onEveryStartCallback) {
this._onEveryStartCallback(this._object)
}

this._onEveryStartCallbackFired = true
}

elapsed = (time - this._startTime) / this._duration
elapsed = this._duration === 0 || elapsed > 1 ? 1 : elapsed

Expand Down Expand Up @@ -408,6 +424,8 @@ export class Tween<T extends UnknownProps> {
this._onRepeatCallback(this._object)
}

this._onEveryStartCallbackFired = false

return true
} else {
if (this._onCompleteCallback) {
Expand Down
33 changes: 33 additions & 0 deletions src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ export const tests = {
test.ok(t.onStart() instanceof TWEEN.Tween)
test.equal(t.onStart(), t)

test.ok(t.onEveryStart() instanceof TWEEN.Tween)
test.equal(t.onEveryStart(), t)

test.ok(t.onStop() instanceof TWEEN.Tween)
test.equal(t.onStop(), t)

Expand Down Expand Up @@ -976,6 +979,36 @@ export const tests = {
test.done()
},

'Test TWEEN.Tween.onEveryStart'(test: Test): void {
const obj = {},
t = new TWEEN.Tween(obj)
let counter = 0

t.to({x: 2}, 500)
t.delay(500)
t.repeat(Infinity)
t.onEveryStart(function (): void {
counter++
})

test.deepEqual(counter, 0)

t.start(0)
TWEEN.update(0)
test.deepEqual(counter, 0, 'onEveryStart callback not called before delayed start')

TWEEN.update(500)
test.deepEqual(counter, 1, 'onEveryStart callback called at delayed start')

TWEEN.update(1000)
test.deepEqual(counter, 1, 'onEveryStart callback not called before delayed repeat start')

TWEEN.update(1500)
test.deepEqual(counter, 2, 'onEveryStart callback called at delayed repeat start')

test.done()
},

'Test TWEEN.Tween.onStop'(test: Test): void {
const obj = {},
t = new TWEEN.Tween(obj)
Expand Down

0 comments on commit d202e0d

Please sign in to comment.