Skip to content

Commit

Permalink
add : TWEEN.Easing.generatePow function and test.
Browse files Browse the repository at this point in the history
related tweenjs#116
  • Loading branch information
MasatoMakino committed Apr 21, 2021
1 parent 307c0b8 commit 88f2275
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Easing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,30 @@ const Easing = {
return Easing.Bounce.Out(amount * 2 - 1) * 0.5 + 0.5
},
},
generatePow: function (
power = 4,
): {
In(amount: number): number
Out(amount: number): number
InOut(amount: number): number
} {
power = power < 1.0 ? 1.0 : power
power = power > 10000 ? 10000 : power
return {
In: function (amount: number): number {
return amount ** power
},
Out: function (amount: number): number {
return 1 - (1 - amount) ** power
},
InOut: function (amount: number): number {
if (amount < 0.5) {
return (amount * 2) ** power / 2
}
return (1 - (2 - amount * 2) ** power) / 2 + 0.5
},
}
},
}

export default Easing
58 changes: 58 additions & 0 deletions src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,58 @@ export const tests = {

test.done()
},

'Test TWEEN.Easing.generatePow(1) equals Linear'(test: Test): void {
const ease1 = TWEEN.Easing.generatePow(1)
const easeMinus = TWEEN.Easing.generatePow(-1)

const compareWithLinear = (ease: EasingFunctionGroup, amount: number) => {
const linearResult = TWEEN.Easing.Linear.None(amount)
test.equal(linearResult, ease.In(amount))
test.equal(linearResult, ease.Out(amount))
test.equal(linearResult, ease.InOut(amount))
}
compareWithLinear(ease1, 0)
compareWithLinear(easeMinus, 0)
compareWithLinear(ease1, 0.25)
compareWithLinear(easeMinus, 0.25)
compareWithLinear(ease1, 0.5)
compareWithLinear(easeMinus, 0.5)
compareWithLinear(ease1, 0.75)
compareWithLinear(easeMinus, 0.75)
compareWithLinear(ease1, 1)
compareWithLinear(easeMinus, 1)

compareWithLinear(ease1, -1)
compareWithLinear(easeMinus, -1)
compareWithLinear(ease1, Infinity)
compareWithLinear(easeMinus, Infinity)

test.done()
},

'Test TWEEN.Easing.generatePow(n) should pass 0.0, 0.5, 1.0'(test: Test): void {
const checkEdgeValue = (ease: EasingFunctionGroup) => {
test.equal(ease.InOut(0.0), 0.0)
test.equal(ease.In(0.0), 0.0)
test.equal(ease.Out(0.0), 0.0)

test.equal(ease.InOut(0.5), 0.5)

test.equal(ease.InOut(1.0), 1.0)
test.equal(ease.In(1.0), 1.0)
test.equal(ease.Out(1.0), 1.0)
}
checkEdgeValue(TWEEN.Easing.generatePow(Number.NEGATIVE_INFINITY))
checkEdgeValue(TWEEN.Easing.generatePow(1))
checkEdgeValue(TWEEN.Easing.generatePow(Math.LOG2E))
checkEdgeValue(TWEEN.Easing.generatePow(Math.PI))
checkEdgeValue(TWEEN.Easing.generatePow())
checkEdgeValue(TWEEN.Easing.generatePow(6))
checkEdgeValue(TWEEN.Easing.generatePow(Number.POSITIVE_INFINITY))

test.done()
},
}

type Test = {
Expand All @@ -1926,3 +1978,9 @@ type Test = {
expect(n: number): void
done(): void
}

type EasingFunctionGroup = {
In(amount: number): number
Out(amount: number): number
InOut(amount: number): number
}

0 comments on commit 88f2275

Please sign in to comment.