mirror of
https://github.com/tweenjs/tween.js.git
synced 2026-02-01 17:27:10 +00:00
parent
307c0b8266
commit
88f22751c9
@ -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
src/tests.ts
58
src/tests.ts
@ -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 = {
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user