BlueGit

projects-timfalken-com meteors

Files

meteors / src / easing.js

easing.js

easing.js 3.3 KiB Download Raw
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. */
15var BACK_OVERSHOOT = 1.70158;
16
17/** Qt QEasingCurve default period and amplitude for Elastic curves. */
18var ELASTIC_PERIOD = 0.3;
19var ELASTIC_AMPLITUDE = 1.0;
20
21function 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. */
31function 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 */
44function 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/** easeOutCubic: 1 − (1−t)³. Smooth, no overshoot (easings.net). */
55function easeOutCubic (t)
56{
57 t = clamp01(t);
58 var u = 1 - t;
59 return 1 - u * u * u;
60}
61
62function tween (from, to, t, ease)
63{
64 return from + (to - from) * ease(clamp01(t));
65}
66
67/**
68 * Visual-only block swap tween (t1121). Game logic and the grid update
69 * immediately; this only moves the displaced sprite. 100 ms sits in the
70 * requested 80–120 ms range.
71 */
72var SWAP_TWEEN = {
73 duration: 0.100,
74 ease: easeOutCubic
75};
76
77/**
78 * DEFAULT settings of Tim's KWin Click Tween
79 * (git.timfalken.com/timfa/click-tween).
80 *
81 * Press: shrink to 80% in 150 ms with easeOutBack, stay at 80% while held.
82 * Release: grow back to 100% in 720 ms with easeOutElastic.
83 * Mid-animation release starts from the current scale, like QVariantAnimation.
84 */
85var CLICK_TWEEN = {
86 pressScale: 0.8,
87 releaseScale: 1.0,
88 pressDuration: 0.150,
89 releaseDuration: 0.720,
90 pressEase: easeOutBack,
91 releaseEase: easeOutElastic,
92 backOvershoot: BACK_OVERSHOOT,
93 elasticPeriod: ELASTIC_PERIOD,
94 elasticAmplitude: ELASTIC_AMPLITUDE,
95 source: 'click-tween settings.h + clicktween.cpp (Qt QEasingCurve defaults)'
96};
97
98/**
99 * Visual landing bounce, u in 0..1. Subtle squash with a slight stretch
100 * on the other axis and a tiny upward rebound (in tiles, visual only).
101 */
102function bouncePose (u, squash)
103{
104 if (u <= 0 || u >= 1)
105 { return { sx: 1, sy: 1, dy: 0 }; }
106 var wave = Math.sin(Math.PI * u);
107 return {
108 sx: 1 + squash * 0.6 * wave,
109 sy: 1 - squash * wave,
110 dy: 0.07 * wave * (1 - u)
111 };
112}
113
114var Easing = {
115 BACK_OVERSHOOT: BACK_OVERSHOOT,
116 ELASTIC_PERIOD: ELASTIC_PERIOD,
117 ELASTIC_AMPLITUDE: ELASTIC_AMPLITUDE,
118 easeOutBack: easeOutBack,
119 easeOutElastic: easeOutElastic,
120 easeOutCubic: easeOutCubic,
121 tween: tween,
122 CLICK_TWEEN: CLICK_TWEEN,
123 SWAP_TWEEN: SWAP_TWEEN,
124 clamp01: clamp01,
125 bouncePose: bouncePose
126};
127
128if (typeof module !== 'undefined' && module.exports)
129{ module.exports = Easing; }
130else if (typeof globalThis !== 'undefined')
131{ globalThis.Easing = Easing; }