Skip to content

Commit

Permalink
Add #hasEnded() (#151)
Browse files Browse the repository at this point in the history
* Add #hasEnded()

Co-authored-by: Jeremy Kahn <jeremyckahn@gmail.com>
  • Loading branch information
arthuro555 and jeremyckahn committed Jun 12, 2022
1 parent 619a722 commit 1c7c972
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/tweenable.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ const processTween = (tween, currentTime) => {

const endTime = timestamp + delay + duration
let timeToCompute = currentTime > endTime ? endTime : currentTime
const hasEnded = timeToCompute >= endTime
tween._hasEnded = timeToCompute >= endTime
const offset = duration - (endTime - timeToCompute)
const hasFilters = tween._filters.length > 0

if (hasEnded) {
if (tween._hasEnded) {
tween._render(targetState, tween._data, offset)
return tween.stop(true)
}
Expand Down Expand Up @@ -321,6 +321,8 @@ export class Tweenable {
/** @private */
this._timestamp = null
/** @private */
this._hasEnded = false
/** @private */
this._resolve = null
/** @private */
this._reject = null
Expand Down Expand Up @@ -717,6 +719,15 @@ export class Tweenable {
return this._isPlaying
}

/**
* Whether or not a tween has finished running.
* @method Tweenable#hasEnded
* @return {boolean}
*/
hasEnded() {
return this._hasEnded
}

/**
* @method Tweenable#setScheduleFunction
* @param {shifty.scheduleFunction} scheduleFunction
Expand Down
31 changes: 31 additions & 0 deletions src/tweenable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,37 @@ describe('#tween', () => {
})
})

describe('#hasEnded', () => {
test('Only trigger after the end of the tween', () => {
tweenable = new Tweenable()

tweenable.tween({
from: { x: 0 },
to: { x: 10 },
duration: 500,
})

expect(tweenable.hasEnded()).toBe(false)
tweenable.seek(500)
expect(tweenable.hasEnded()).toBe(true)
})

test('Triggers again after restarting the tween', () => {
tweenable = new Tweenable()

tweenable.tween({
from: { x: 0 },
to: { x: 10 },
duration: 500,
})

tweenable.seek(500)
expect(tweenable.hasEnded()).toBe(true)
tweenable.seek(0)
expect(tweenable.hasEnded()).toBe(false)
})
})

describe('#resume', () => {
beforeEach(() => {
Tweenable.now = () => 0
Expand Down

0 comments on commit 1c7c972

Please sign in to comment.