Skip to content

Commit 775536c

Browse files
authored
Merge pull request #608 from MasatoMakino/add-unit-test-for-easing-function
Add unit test for easing function
2 parents d8b4f09 + 273520d commit 775536c

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

src/Easing.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ const Easing = {
7070
},
7171
Sinusoidal: {
7272
In: function (amount: number): number {
73-
return 1 - Math.cos((amount * Math.PI) / 2)
73+
return 1 - Math.sin(((1.0 - amount) * Math.PI) / 2)
7474
},
7575
Out: function (amount: number): number {
7676
return Math.sin((amount * Math.PI) / 2)
7777
},
7878
InOut: function (amount: number): number {
79-
return 0.5 * (1 - Math.cos(Math.PI * amount))
79+
return 0.5 * (1 - Math.sin(Math.PI * (0.5 - amount)))
8080
},
8181
},
8282
Exponential: {
@@ -159,11 +159,11 @@ const Easing = {
159159
Back: {
160160
In: function (amount: number): number {
161161
const s = 1.70158
162-
return amount * amount * ((s + 1) * amount - s)
162+
return amount === 1 ? 1 : amount * amount * ((s + 1) * amount - s)
163163
},
164164
Out: function (amount: number): number {
165165
const s = 1.70158
166-
return --amount * amount * ((s + 1) * amount + s) + 1
166+
return amount === 0 ? 0 : --amount * amount * ((s + 1) * amount + s) + 1
167167
},
168168
InOut: function (amount: number): number {
169169
const s = 1.70158 * 1.525

src/tests.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,106 @@ export const tests = {
630630
test.done()
631631
},
632632

633+
'Test TWEEN.Easing should starts at 0.0, ends at 1.0. TWEEN.Easing.InOut() should be 0.5 at midpoint'(
634+
test: Test,
635+
): void {
636+
const checkEdgeValue = (ease: EasingFunctionGroup) => {
637+
test.equal(ease.In(0.0), 0.0)
638+
test.equal(ease.Out(0.0), 0.0)
639+
test.equal(ease.InOut(0.0), 0.0)
640+
641+
test.equal(ease.In(1.0), 1.0)
642+
test.equal(ease.Out(1.0), 1.0)
643+
test.equal(ease.InOut(1.0), 1.0)
644+
645+
test.equal(ease.InOut(0.5), 0.5)
646+
}
647+
648+
checkEdgeValue(TWEEN.Easing.Quadratic)
649+
checkEdgeValue(TWEEN.Easing.Cubic)
650+
checkEdgeValue(TWEEN.Easing.Quartic)
651+
checkEdgeValue(TWEEN.Easing.Quintic)
652+
checkEdgeValue(TWEEN.Easing.Sinusoidal)
653+
checkEdgeValue(TWEEN.Easing.Exponential)
654+
checkEdgeValue(TWEEN.Easing.Circular)
655+
checkEdgeValue(TWEEN.Easing.Elastic)
656+
checkEdgeValue(TWEEN.Easing.Back)
657+
checkEdgeValue(TWEEN.Easing.Bounce)
658+
test.done()
659+
},
660+
661+
'Test TWEEN.Easing should pass a specific value'(test: Test): void {
662+
const checkEasingGroupPassPoints = (
663+
easingGroup: EasingFunctionGroup,
664+
expects: {In: number; Out: number; InOut: number},
665+
) => {
666+
checkPassPoint(easingGroup.In, expects.In)
667+
checkPassPoint(easingGroup.Out, expects.Out)
668+
checkPassPoint(easingGroup.InOut, expects.InOut)
669+
}
670+
const checkPassPoint = (
671+
easeFunc: (amount: number) => number,
672+
expect: number,
673+
numDigits = 14,
674+
amount = Math.LOG10E,
675+
) => {
676+
toBeCloseTo(test, easeFunc(amount), expect, numDigits)
677+
}
678+
679+
checkEasingGroupPassPoints(TWEEN.Easing.Quadratic, {
680+
In: 0.18861169701161393,
681+
Out: 0.6799772667948897,
682+
InOut: 0.37722339402322785,
683+
})
684+
checkEasingGroupPassPoints(TWEEN.Easing.Cubic, {
685+
In: 0.08191301923455198,
686+
Out: 0.8189613739094657,
687+
InOut: 0.3276520769382079,
688+
})
689+
checkEasingGroupPassPoints(TWEEN.Easing.Quartic, {
690+
In: 0.035574372249600854,
691+
Out: 0.8975854502319308,
692+
InOut: 0.28459497799680683,
693+
})
694+
checkEasingGroupPassPoints(TWEEN.Easing.Quintic, {
695+
In: 0.015449753565173821,
696+
Out: 0.9420635240628092,
697+
InOut: 0.24719605704278114,
698+
})
699+
checkEasingGroupPassPoints(TWEEN.Easing.Sinusoidal, {
700+
In: 0.22380505208857682,
701+
Out: 0.630492983971101,
702+
InOut: 0.397521402836783,
703+
})
704+
checkEasingGroupPassPoints(TWEEN.Easing.Exponential, {
705+
In: 0.01981785759600918,
706+
Out: 0.9507231043886069,
707+
InOut: 0.2010867096041978,
708+
})
709+
checkEasingGroupPassPoints(TWEEN.Easing.Circular, {
710+
In: 0.09922905076352173,
711+
Out: 0.8246073409780499,
712+
InOut: 0.2522333699054974,
713+
})
714+
checkEasingGroupPassPoints(TWEEN.Easing.Elastic, {
715+
In: -0.01701121590548648,
716+
Out: 0.9577017895937282,
717+
InOut: -0.09523991217687242,
718+
})
719+
checkEasingGroupPassPoints(TWEEN.Easing.Back, {
720+
In: -0.09964331689734113,
721+
Out: 1.055453950893486,
722+
InOut: 0.19901899530677744,
723+
})
724+
725+
checkEasingGroupPassPoints(TWEEN.Easing.Bounce, {
726+
In: 0.24689860443452594,
727+
Out: 0.8434464829485027,
728+
InOut: 0.43470212148602316,
729+
})
730+
test.done()
731+
},
732+
633733
// TODO test interpolation()
634734

635735
'Test TWEEN.Tween.chain --with one tween'(test: Test): void {
@@ -1954,3 +2054,20 @@ type Test = {
19542054
expect(n: number): void
19552055
done(): void
19562056
}
2057+
2058+
type EasingFunctionGroup = {
2059+
In(amount: number): number
2060+
Out(amount: number): number
2061+
InOut(amount: number): number
2062+
}
2063+
2064+
function toBeCloseTo(test: Test, numberA: number, numberB: number, numDigits = 2): void {
2065+
const diff = Math.abs(numberA - numberB)
2066+
test.ok(
2067+
diff < 10 ** -numDigits / 2,
2068+
`
2069+
actual : ${numberA}
2070+
expect : ${numberB}
2071+
diff : ${diff}`,
2072+
)
2073+
}

0 commit comments

Comments
 (0)