feat: planet apex-ease and bounce params, Click Tween eases
fallAccel and bounceDuration are faster at low air resistance (fallTime). Easing curves and 150 ms / 720 ms / 0.8 scale copy the defaults of Tim's KWin Click Tween (Qt QEasingCurve overshoot 1.70158).
@@ -13,6 +13,7 @@ rm -f "$BUILD_FILE"
13
13
FILES=(
14
14
"$SRC_DIR/rng.js"
15
15
"$SRC_DIR/planet.js"
16
"$SRC_DIR/easing.js"
16
17
"$SRC_DIR/gameCore.js"
17
18
"$SRC_DIR/main.js"
18
19
)
@@ -13,6 +13,7 @@ rm -f "$BUILD_FILE"13
FILES=(13
FILES=(14
"$SRC_DIR/rng.js"14
"$SRC_DIR/rng.js"15
"$SRC_DIR/planet.js"15
"$SRC_DIR/planet.js"16
"$SRC_DIR/easing.js"16
"$SRC_DIR/gameCore.js"17
"$SRC_DIR/gameCore.js"17
"$SRC_DIR/main.js"18
"$SRC_DIR/main.js"18
)19
)@@ -0,0 +1,111 @@
1
'use strict';
2
3
/**
4
* Easing curves matching Qt QEasingCurve defaults (the same values Tim's
5
* KWin Click Tween effect uses). Formulas and default parameters come from
6
* Qt 6's QEasingCurve and https://easings.net/.
7
*
8
* Source of the click/release defaults:
9
* /workspace/click-tween/settings.h (clickTime, releaseTime, easing names)
10
* /workspace/click-tween/clicktween.cpp (press scale 0.8, release scale 1.0)
11
* /workspace/click-tween/README.md
12
*/
13
14
/** Qt QEasingCurve default overshoot for InBack / OutBack / InOutBack. */
15
var BACK_OVERSHOOT = 1.70158;
16
17
/** Qt QEasingCurve default period and amplitude for Elastic curves. */
18
var ELASTIC_PERIOD = 0.3;
19
var ELASTIC_AMPLITUDE = 1.0;
20
21
function clamp01 (t)
22
{
23
if (t <= 0)
24
{ return 0; }
25
if (t >= 1)
26
{ return 1; }
27
return t;
28
}
29
30
/** easeOutBack: overshoots the end value, then settles. Qt / easings.net. */
31
function easeOutBack (t)
32
{
33
t = clamp01(t);
34
var c1 = BACK_OVERSHOOT;
35
var c3 = c1 + 1;
36
var u = t - 1;
37
return 1 + c3 * u * u * u + c1 * u * u;
38
}
39
40
/**
41
* easeOutElastic: decaying sine overshoot past the end value.
42
* Qt default amplitude 1, period 0.3 (equivalent to easings.net).
43
*/
44
function easeOutElastic (t)
45
{
46
t = clamp01(t);
47
if (t === 0 || t === 1)
48
{ return t; }
49
var p = ELASTIC_PERIOD;
50
var s = p / 4;
51
return Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / p) + 1;
52
}
53
54
function tween (from, to, t, ease)
55
{
56
return from + (to - from) * ease(clamp01(t));
57
}
58
59
/**
60
* DEFAULT settings of Tim's KWin Click Tween
61
* (git.timfalken.com/timfa/click-tween).
62
*
63
* Press: shrink to 80% in 150 ms with easeOutBack, stay at 80% while held.
64
* Release: grow back to 100% in 720 ms with easeOutElastic.
65
* Mid-animation release starts from the current scale, like QVariantAnimation.
66
*/
67
var CLICK_TWEEN = {
68
pressScale: 0.8,
69
releaseScale: 1.0,
70
pressDuration: 0.150,
71
releaseDuration: 0.720,
72
pressEase: easeOutBack,
73
releaseEase: easeOutElastic,
74
backOvershoot: BACK_OVERSHOOT,
75
elasticPeriod: ELASTIC_PERIOD,
76
elasticAmplitude: ELASTIC_AMPLITUDE,
77
source: 'click-tween settings.h + clicktween.cpp (Qt QEasingCurve defaults)'
78
};
79
80
/**
81
* Visual landing bounce, u in 0..1. Subtle squash with a slight stretch
82
* on the other axis and a tiny upward rebound (in tiles, visual only).
83
*/
84
function bouncePose (u, squash)
85
{
86
if (u <= 0 || u >= 1)
87
{ return { sx: 1, sy: 1, dy: 0 }; }
88
var wave = Math.sin(Math.PI * u);
89
return {
90
sx: 1 + squash * 0.6 * wave,
91
sy: 1 - squash * wave,
92
dy: 0.07 * wave * (1 - u)
93
};
94
}
95
96
var Easing = {
97
BACK_OVERSHOOT: BACK_OVERSHOOT,
98
ELASTIC_PERIOD: ELASTIC_PERIOD,
99
ELASTIC_AMPLITUDE: ELASTIC_AMPLITUDE,
100
easeOutBack: easeOutBack,
101
easeOutElastic: easeOutElastic,
102
tween: tween,
103
CLICK_TWEEN: CLICK_TWEEN,
104
clamp01: clamp01,
105
bouncePose: bouncePose
106
};
107
108
if (typeof module !== 'undefined' && module.exports)
109
{ module.exports = Easing; }
110
else if (typeof globalThis !== 'undefined')
111
{ globalThis.Easing = Easing; }
@@ -0,0 +1,111 @@1
'use strict';2
3
/**4
* Easing curves matching Qt QEasingCurve defaults (the same values Tim's5
* KWin Click Tween effect uses). Formulas and default parameters come from6
* Qt 6's QEasingCurve and https://easings.net/.7
*8
* Source of the click/release defaults:9
* /workspace/click-tween/settings.h (clickTime, releaseTime, easing names)10
* /workspace/click-tween/clicktween.cpp (press scale 0.8, release scale 1.0)11
* /workspace/click-tween/README.md12
*/13
14
/** Qt QEasingCurve default overshoot for InBack / OutBack / InOutBack. */15
var BACK_OVERSHOOT = 1.70158;16
17
/** Qt QEasingCurve default period and amplitude for Elastic curves. */18
var ELASTIC_PERIOD = 0.3;19
var ELASTIC_AMPLITUDE = 1.0;20
21
function clamp01 (t)22
{23
if (t <= 0)24
{ return 0; }25
if (t >= 1)26
{ return 1; }27
return t;28
}29
30
/** easeOutBack: overshoots the end value, then settles. Qt / easings.net. */31
function easeOutBack (t)32
{33
t = clamp01(t);34
var c1 = BACK_OVERSHOOT;35
var c3 = c1 + 1;36
var u = t - 1;37
return 1 + c3 * u * u * u + c1 * u * u;38
}39
40
/**41
* easeOutElastic: decaying sine overshoot past the end value.42
* Qt default amplitude 1, period 0.3 (equivalent to easings.net).43
*/44
function easeOutElastic (t)45
{46
t = clamp01(t);47
if (t === 0 || t === 1)48
{ return t; }49
var p = ELASTIC_PERIOD;50
var s = p / 4;51
return Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / p) + 1;52
}53
54
function tween (from, to, t, ease)55
{56
return from + (to - from) * ease(clamp01(t));57
}58
59
/**60
* DEFAULT settings of Tim's KWin Click Tween61
* (git.timfalken.com/timfa/click-tween).62
*63
* Press: shrink to 80% in 150 ms with easeOutBack, stay at 80% while held.64
* Release: grow back to 100% in 720 ms with easeOutElastic.65
* Mid-animation release starts from the current scale, like QVariantAnimation.66
*/67
var CLICK_TWEEN = {68
pressScale: 0.8,69
releaseScale: 1.0,70
pressDuration: 0.150,71
releaseDuration: 0.720,72
pressEase: easeOutBack,73
releaseEase: easeOutElastic,74
backOvershoot: BACK_OVERSHOOT,75
elasticPeriod: ELASTIC_PERIOD,76
elasticAmplitude: ELASTIC_AMPLITUDE,77
source: 'click-tween settings.h + clicktween.cpp (Qt QEasingCurve defaults)'78
};79
80
/**81
* Visual landing bounce, u in 0..1. Subtle squash with a slight stretch82
* on the other axis and a tiny upward rebound (in tiles, visual only).83
*/84
function bouncePose (u, squash)85
{86
if (u <= 0 || u >= 1)87
{ return { sx: 1, sy: 1, dy: 0 }; }88
var wave = Math.sin(Math.PI * u);89
return {90
sx: 1 + squash * 0.6 * wave,91
sy: 1 - squash * wave,92
dy: 0.07 * wave * (1 - u)93
};94
}95
96
var Easing = {97
BACK_OVERSHOOT: BACK_OVERSHOOT,98
ELASTIC_PERIOD: ELASTIC_PERIOD,99
ELASTIC_AMPLITUDE: ELASTIC_AMPLITUDE,100
easeOutBack: easeOutBack,101
easeOutElastic: easeOutElastic,102
tween: tween,103
CLICK_TWEEN: CLICK_TWEEN,104
clamp01: clamp01,105
bouncePose: bouncePose106
};107
108
if (typeof module !== 'undefined' && module.exports)109
{ module.exports = Easing; }110
else if (typeof globalThis !== 'undefined')111
{ globalThis.Easing = Easing; }@@ -58,7 +58,14 @@ function createPlanet (opts)
58
58
riseTime: riseTime,
59
59
colors: colors,
60
60
baseJumpHeight: FIELD_HEIGHT / jumpsToClear,
61
fallSpeed: FIELD_HEIGHT / fallTime
61
fallSpeed: FIELD_HEIGHT / fallTime,
62
// Time to ease from vy=0 at the apex to terminal fallSpeed.
63
// Shorter when air resistance is low (small fallTime).
64
apexEaseTime: 0.15 * fallTime,
65
fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime),
66
// Visual landing bounce: faster at low air resistance. Stage-3 params.
67
bounceDuration: 0.12 * fallTime,
68
bounceSquash: 0.16
62
69
};
63
70
return planet;
64
71
}
@@ -58,7 +58,14 @@ function createPlanet (opts)58
riseTime: riseTime,58
riseTime: riseTime,59
colors: colors,59
colors: colors,60
baseJumpHeight: FIELD_HEIGHT / jumpsToClear,60
baseJumpHeight: FIELD_HEIGHT / jumpsToClear,61
fallSpeed: FIELD_HEIGHT / fallTime61
fallSpeed: FIELD_HEIGHT / fallTime,62
// Time to ease from vy=0 at the apex to terminal fallSpeed.63
// Shorter when air resistance is low (small fallTime).64
apexEaseTime: 0.15 * fallTime,65
fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime),66
// Visual landing bounce: faster at low air resistance. Stage-3 params.67
bounceDuration: 0.12 * fallTime,68
bounceSquash: 0.1662
};69
};63
return planet;70
return planet;64
}71
}@@ -0,0 +1,87 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const Easing = require('../src/easing.js');
6
const Planet = require('../src/planet.js');
7
8
describe('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
77
describe('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
});
@@ -0,0 +1,87 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const Easing = require('../src/easing.js');6
const Planet = require('../src/planet.js');7
8
describe('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
77
describe('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
});