BlueGit

projects-timfalken-com meteors

Files

meteors / src / easing.js

easing.js

easing.js 2.9 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
54function 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 */
67var 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 */
84function 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
96var 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
108if (typeof module !== 'undefined' && module.exports)
109{ module.exports = Easing; }
110else if (typeof globalThis !== 'undefined')
111{ globalThis.Easing = Easing; }