BlueGit

projects-timfalken-com meteors

Files

meteors / test / easing.test.js

easing.test.js

easing.test.js 3.3 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const Easing = require('../src/easing.js');
6const Planet = require('../src/planet.js');
7
8describe('Click-tween defaults (KWin Click Tween)', () =>
9{
10 it('matches Tim\'s default scale, durations and Qt easing parameters', () =>
11 {
12 const c = Easing.CLICK_TWEEN;
13 assert.equal(c.pressScale, 0.8);
14 assert.equal(c.releaseScale, 1.0);
15 assert.equal(c.pressDuration, 0.150);
16 assert.equal(c.releaseDuration, 0.720);
17 assert.equal(c.backOvershoot, 1.70158);
18 assert.equal(c.elasticPeriod, 0.3);
19 assert.equal(c.elasticAmplitude, 1.0);
20 assert.equal(c.pressEase, Easing.easeOutBack);
21 assert.equal(c.releaseEase, Easing.easeOutElastic);
22 });
23
24 it('easeOutBack starts at 0, ends at 1, and overshoots past 1 mid-way', () =>
25 {
26 assert.ok(Math.abs(Easing.easeOutBack(0)) < 1e-12);
27 assert.ok(Math.abs(Easing.easeOutBack(1) - 1) < 1e-12);
28 let max = 0;
29 for (let i = 0; i <= 100; i++)
30 {
31 const v = Easing.easeOutBack(i / 100);
32 if (v > max)
33 { max = v; }
34 }
35 assert.ok(max > 1.01, 'OutBack overshoots, max=' + max);
36 // Qt / Penner default 1.70158 peaks around 1.1.
37 assert.ok(max < 1.2, 'overshoot is the Qt default, max=' + max);
38 });
39
40 it('easeOutElastic ends at 1 and oscillates around the end value', () =>
41 {
42 assert.equal(Easing.easeOutElastic(0), 0);
43 assert.equal(Easing.easeOutElastic(1), 1);
44 const mid = Easing.easeOutElastic(0.3);
45 // Decaying sine: not monotonic; some samples exceed 1.
46 let above = false;
47 let below = false;
48 for (let i = 1; i < 40; i++)
49 {
50 const v = Easing.easeOutElastic(i / 40);
51 if (v > 1.01)
52 { above = true; }
53 if (v < 0.99 && i > 10)
54 { below = true; }
55 }
56 assert.ok(above, 'elastic overshoots above 1');
57 assert.ok(below, 'elastic undershoots as it decays');
58 assert.ok(typeof mid === 'number');
59 });
60
61 it('tween from 1.0 to 0.8 with easeOutBack overshoots below 0.8 then settles', () =>
62 {
63 const c = Easing.CLICK_TWEEN;
64 assert.ok(Math.abs(Easing.tween(1, 0.8, 0, c.pressEase) - 1) < 1e-12);
65 assert.ok(Math.abs(Easing.tween(1, 0.8, 1, c.pressEase) - 0.8) < 1e-12);
66 let min = 1;
67 for (let i = 0; i <= 100; i++)
68 {
69 const v = Easing.tween(1, 0.8, i / 100, c.pressEase);
70 if (v < min)
71 { min = v; }
72 }
73 assert.ok(min < 0.8 - 0.005, 'press overshoots below 0.8, min=' + min);
74 });
75});
76
77describe('Planet apex-ease and bounce parameters', () =>
78{
79 it('derives faster ease and bounce from lower fallTime (low air resistance)', () =>
80 {
81 const terra = Planet.defaultPlanet();
82 assert.ok(Math.abs(terra.apexEaseTime - 0.15 * 3) < 1e-12);
83 assert.ok(Math.abs(terra.bounceDuration - 0.12 * 3) < 1e-12);
84 assert.equal(terra.bounceSquash, 0.16);
85 assert.ok(Math.abs(terra.fallAccel - terra.fallSpeed / terra.apexEaseTime) < 1e-9);
86 });
87});
88
89describe('Swap tween (t1121)', () =>
90{
91 it('easeOutCubic is 0 at 0, 1 at 1, and 0.875 at 0.5', () =>
92 {
93 assert.equal(Easing.easeOutCubic(0), 0);
94 assert.equal(Easing.easeOutCubic(1), 1);
95 assert.ok(Math.abs(Easing.easeOutCubic(0.5) - 0.875) < 1e-12);
96 assert.ok(Easing.easeOutCubic(0.25) > 0.25, 'front-loaded (fast finish)');
97 });
98
99 it('SWAP_TWEEN lasts 80–120 ms and uses easeOutCubic', () =>
100 {
101 const s = Easing.SWAP_TWEEN;
102 assert.ok(s.duration >= 0.08 && s.duration <= 0.12, 'duration=' + s.duration);
103 assert.equal(s.ease, Easing.easeOutCubic);
104 });
105});