Skip to content

Commit 88f2275

Browse files
committed
add : TWEEN.Easing.generatePow function and test.
related tweenjs#116
1 parent 307c0b8 commit 88f2275

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/Easing.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ const Easing = {
195195
return Easing.Bounce.Out(amount * 2 - 1) * 0.5 + 0.5
196196
},
197197
},
198+
generatePow: function (
199+
power = 4,
200+
): {
201+
In(amount: number): number
202+
Out(amount: number): number
203+
InOut(amount: number): number
204+
} {
205+
power = power < 1.0 ? 1.0 : power
206+
power = power > 10000 ? 10000 : power
207+
return {
208+
In: function (amount: number): number {
209+
return amount ** power
210+
},
211+
Out: function (amount: number): number {
212+
return 1 - (1 - amount) ** power
213+
},
214+
InOut: function (amount: number): number {
215+
if (amount < 0.5) {
216+
return (amount * 2) ** power / 2
217+
}
218+
return (1 - (2 - amount * 2) ** power) / 2 + 0.5
219+
},
220+
}
221+
},
198222
}
199223

200224
export default Easing

src/tests.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,58 @@ export const tests = {
19171917

19181918
test.done()
19191919
},
1920+
1921+
'Test TWEEN.Easing.generatePow(1) equals Linear'(test: Test): void {
1922+
const ease1 = TWEEN.Easing.generatePow(1)
1923+
const easeMinus = TWEEN.Easing.generatePow(-1)
1924+
1925+
const compareWithLinear = (ease: EasingFunctionGroup, amount: number) => {
1926+
const linearResult = TWEEN.Easing.Linear.None(amount)
1927+
test.equal(linearResult, ease.In(amount))
1928+
test.equal(linearResult, ease.Out(amount))
1929+
test.equal(linearResult, ease.InOut(amount))
1930+
}
1931+
compareWithLinear(ease1, 0)
1932+
compareWithLinear(easeMinus, 0)
1933+
compareWithLinear(ease1, 0.25)
1934+
compareWithLinear(easeMinus, 0.25)
1935+
compareWithLinear(ease1, 0.5)
1936+
compareWithLinear(easeMinus, 0.5)
1937+
compareWithLinear(ease1, 0.75)
1938+
compareWithLinear(easeMinus, 0.75)
1939+
compareWithLinear(ease1, 1)
1940+
compareWithLinear(easeMinus, 1)
1941+
1942+
compareWithLinear(ease1, -1)
1943+
compareWithLinear(easeMinus, -1)
1944+
compareWithLinear(ease1, Infinity)
1945+
compareWithLinear(easeMinus, Infinity)
1946+
1947+
test.done()
1948+
},
1949+
1950+
'Test TWEEN.Easing.generatePow(n) should pass 0.0, 0.5, 1.0'(test: Test): void {
1951+
const checkEdgeValue = (ease: EasingFunctionGroup) => {
1952+
test.equal(ease.InOut(0.0), 0.0)
1953+
test.equal(ease.In(0.0), 0.0)
1954+
test.equal(ease.Out(0.0), 0.0)
1955+
1956+
test.equal(ease.InOut(0.5), 0.5)
1957+
1958+
test.equal(ease.InOut(1.0), 1.0)
1959+
test.equal(ease.In(1.0), 1.0)
1960+
test.equal(ease.Out(1.0), 1.0)
1961+
}
1962+
checkEdgeValue(TWEEN.Easing.generatePow(Number.NEGATIVE_INFINITY))
1963+
checkEdgeValue(TWEEN.Easing.generatePow(1))
1964+
checkEdgeValue(TWEEN.Easing.generatePow(Math.LOG2E))
1965+
checkEdgeValue(TWEEN.Easing.generatePow(Math.PI))
1966+
checkEdgeValue(TWEEN.Easing.generatePow())
1967+
checkEdgeValue(TWEEN.Easing.generatePow(6))
1968+
checkEdgeValue(TWEEN.Easing.generatePow(Number.POSITIVE_INFINITY))
1969+
1970+
test.done()
1971+
},
19201972
}
19211973

19221974
type Test = {
@@ -1926,3 +1978,9 @@ type Test = {
19261978
expect(n: number): void
19271979
done(): void
19281980
}
1981+
1982+
type EasingFunctionGroup = {
1983+
In(amount: number): number
1984+
Out(amount: number): number
1985+
InOut(amount: number): number
1986+
}

0 commit comments

Comments
 (0)