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

Add #hasEnded() #151

Merged
merged 9 commits into from
Jun 12, 2022
Merged
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
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