skia-motion-runtime 0.0.0-stage → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,215 @@
1
+ // motion.ts: the motion engine for Skia drawings (adapted from rossark-motion-skill
2
+ // templates/react-native/motion.ts, MIT; the cubic-bezier solver from huashu-art-motion
3
+ // scripts/engine/lib/motion.js MO.bezier, MIT. See NOTICE.md at the package root).
4
+ //
5
+ // HOW IT RUNS IN BOTH PLACES
6
+ // - This file imports nothing. Every function is a pure function of time `t` in milliseconds
7
+ // and carries a 'worklet' directive, so the same file runs:
8
+ // * in the app, on the UI thread, inside Reanimated `useDerivedValue` (clock from clock.app.ts);
9
+ // * in Remotion, during React render, with `t = frame / fps * 1000` (clock from clock.remotion.ts).
10
+ // - The 'worklet' string is a no-op outside Reanimated's Babel plugin.
11
+ // - Keep every curve constant at the TOP of the module: a worklet captures what it references
12
+ // when it is created, and constants declared further down are undefined inside it (rossark §8).
13
+
14
+ /** A cubic-bezier easing p (0..1) -> 0..1, like CSS cubic-bezier(x1, y1, x2, y2). */
15
+ export function bezier(x1: number, y1: number, x2: number, y2: number): (p: number) => number {
16
+ 'worklet';
17
+ const cx = 3 * x1;
18
+ const bx = 3 * (x2 - x1) - cx;
19
+ const ax = 1 - cx - bx;
20
+ const cy = 3 * y1;
21
+ const by = 3 * (y2 - y1) - cy;
22
+ const ay = 1 - cy - by;
23
+ return (p: number) => {
24
+ 'worklet';
25
+ if (p <= 0) return 0;
26
+ if (p >= 1) return 1;
27
+ let s = p;
28
+ for (let k = 0; k < 8; k++) {
29
+ const e = ((ax * s + bx) * s + cx) * s - p;
30
+ const d = (3 * ax * s + 2 * bx) * s + cx;
31
+ if (Math.abs(e) < 1e-6 || Math.abs(d) < 1e-6) break;
32
+ s -= e / d;
33
+ }
34
+ s = Math.min(1, Math.max(0, s));
35
+ return ((ay * s + by) * s + cy) * s;
36
+ };
37
+ }
38
+
39
+ /** Things that travel: gaze, sway, a head turning, a lid closing (rossark §2). */
40
+ export const EASE_IN_OUT = bezier(0.77, 0, 0.175, 1);
41
+ /** Things that open or arrive: a mouth, a lid reopening, a prop appearing (rossark §2). */
42
+ export const EASE_OUT = bezier(0.23, 1, 0.32, 1);
43
+ /** Only things leaving or falling away. Never something the eye is waiting on (rossark §2). */
44
+ export const EASE_IN = bezier(0.55, 0, 1, 0.45);
45
+
46
+ /**
47
+ * The hand-over curve: inOut cubic, the Welcome seal's (word-quran WelcomeScreen.tsx, CARRY_MS 600,
48
+ * Easing.inOut(Easing.cubic), owner O-4578, 9 Nov 2026) and the reels' morph (reels/_kit/morph.js easeIO). Its speed
49
+ * rises and falls smoothly enough that a carry of a phone's width passes skac/abrupt at 30 fps; EASE_IN_OUT (a steep
50
+ * bezier) halves its speed within one 30 fps frame at its peak, which pathcheck reads as a kink.
51
+ */
52
+ export function CUBIC_IN_OUT(p: number): number {
53
+ 'worklet';
54
+ const q = Math.min(1, Math.max(0, p));
55
+ return q < 0.5 ? 4 * q * q * q : 1 - Math.pow(-2 * q + 2, 3) / 2;
56
+ }
57
+ /** Out cubic, the reels' easeOut (reels/_kit/pixelmotion.js): a point set's arrival. */
58
+ export function CUBIC_OUT(p: number): number {
59
+ 'worklet';
60
+ const q = Math.min(1, Math.max(0, p));
61
+ return 1 - Math.pow(1 - q, 3);
62
+ }
63
+ /** In cubic, the reels' easeIn (reels/_kit/pixelmotion.js): a point set replaying its arrival backwards. */
64
+ export function CUBIC_IN(p: number): number {
65
+ 'worklet';
66
+ const q = Math.min(1, Math.max(0, p));
67
+ return q * q * q;
68
+ }
69
+
70
+ /** A keyframe: time (ms), value, and the curve of the segment ARRIVING here ('out', or ease-in-out). */
71
+ export type Key = readonly [number, number, 'out'?];
72
+ export type Keys = readonly Key[];
73
+ /** A key pose for `flow`: time (ms) and value, sorted by time. */
74
+ export type FlowKey = readonly [number, number];
75
+ export type FlowKeys = readonly FlowKey[];
76
+
77
+ /**
78
+ * One move (a blink, a lid, a door): eases into and out of EVERY key, so it stops at each.
79
+ * The last key should equal the first in a loop, so the seam never jumps (rossark §2).
80
+ */
81
+ export function track(t: number, keys: Keys): number {
82
+ 'worklet';
83
+ if (t <= keys[0]![0]) return keys[0]![1];
84
+ for (let i = 1; i < keys.length; i++) {
85
+ const k1 = keys[i]!;
86
+ if (t <= k1[0]) {
87
+ const k0 = keys[i - 1]!;
88
+ const span = k1[0] - k0[0];
89
+ const p = span > 0 ? (t - k0[0]) / span : 1;
90
+ const e = k1[2] === 'out' ? EASE_OUT(p) : EASE_IN_OUT(p);
91
+ return k0[1] + (k1[1] - k0[1]) * e;
92
+ }
93
+ }
94
+ return keys[keys.length - 1]![1];
95
+ }
96
+
97
+ /**
98
+ * A chain of poses (a body, wings, a head turn): passes through every key with a continuous
99
+ * speed and never overshoots one (a monotone cubic). Repeat a value to hold; the idle layer
100
+ * keeps the hold moving (rossark §5.1).
101
+ */
102
+ export function flow(t: number, k: FlowKeys): number {
103
+ 'worklet';
104
+ const n = k.length;
105
+ if (t <= k[0]![0]) return k[0]![1];
106
+ if (t >= k[n - 1]![0]) return k[n - 1]![1];
107
+ let i = 0;
108
+ while (k[i + 1]![0] < t) i++;
109
+ const slope = (j: number) => {
110
+ 'worklet';
111
+ return (k[j + 1]![1] - k[j]![1]) / (k[j + 1]![0] - k[j]![0]);
112
+ };
113
+ const tangent = (j: number) => {
114
+ 'worklet';
115
+ if (j === 0 || j === n - 1) return 0;
116
+ const a = slope(j - 1);
117
+ const b = slope(j);
118
+ if (a * b <= 0) return 0;
119
+ const h0 = k[j]![0] - k[j - 1]![0];
120
+ const h1 = k[j + 1]![0] - k[j]![0];
121
+ const w0 = 2 * h1 + h0;
122
+ const w1 = h1 + 2 * h0;
123
+ return (w0 + w1) / (w0 / a + w1 / b);
124
+ };
125
+ const h = k[i + 1]![0] - k[i]![0];
126
+ const s = (t - k[i]![0]) / h;
127
+ const s2 = s * s;
128
+ const s3 = s2 * s;
129
+ return (2 * s3 - 3 * s2 + 1) * k[i]![1] + (s3 - 2 * s2 + s) * h * tangent(i) + (-2 * s3 + 3 * s2) * k[i + 1]![1] + (s3 - s2) * h * tangent(i + 1);
130
+ }
131
+
132
+ /** 0 -> 1 inside the window [a, b], eased; 0 before, 1 after. One beat of a scene (rossark §2). */
133
+ export function seg(t: number, a: number, b: number, curve: 'out' | 'inOut' = 'out'): number {
134
+ 'worklet';
135
+ if (t <= a) return 0;
136
+ if (t >= b) return 1;
137
+ const p = (t - a) / (b - a);
138
+ return curve === 'out' ? EASE_OUT(p) : EASE_IN_OUT(p);
139
+ }
140
+
141
+ /** An accent's envelope: rises over `ramp` after a, falls over `ramp` before b. Gate a wave with it. */
142
+ export function env(t: number, a: number, b: number, ramp: number): number {
143
+ 'worklet';
144
+ return seg(t, a, a + ramp, 'inOut') * (1 - seg(t, b - ramp, b, 'inOut'));
145
+ }
146
+
147
+ /** A smooth back-and-forth, -1..1. `phase` (0..1) offsets one item from the next (rossark §2). */
148
+ export function wave(t: number, period: number, phase = 0): number {
149
+ 'worklet';
150
+ return Math.sin((t / period + phase) * Math.PI * 2);
151
+ }
152
+
153
+ /** Progress through a loop, 0..1, with a phase: drifting or falling items (rossark §2). */
154
+ export function loop(t: number, period: number, phase = 0): number {
155
+ 'worklet';
156
+ const p = (t / period + phase) % 1;
157
+ return p < 0 ? p + 1 : p;
158
+ }
159
+
160
+ /** 0 at both ends and 1 in the middle: fade in and out (steam, sparkles, ripples) (rossark §2). */
161
+ export function bell(p: number): number {
162
+ 'worklet';
163
+ return Math.sin(Math.PI * Math.min(1, Math.max(0, p)));
164
+ }
165
+
166
+ /** A stable pseudo-random number in [0, 1) for item i. Never Math.random() per frame (rossark §1.6). */
167
+ export function seeded(i: number, salt = 0): number {
168
+ 'worklet';
169
+ const x = Math.sin(i * 12.9898 + salt * 78.233) * 43758.5453;
170
+ return x - Math.floor(x);
171
+ }
172
+
173
+ /**
174
+ * Gravity hop over the window [a, b] (ms) to height `top`: lift = top * (1 - k^2), k from -1 to 1.
175
+ * Peak speed is at take-off and contact: 4 * top / (b - a) units per ms (rossark §5.8).
176
+ */
177
+ export function hop(t: number, a: number, b: number, top: number): number {
178
+ 'worklet';
179
+ if (t <= a || t >= b) return 0;
180
+ const k = ((t - a) / (b - a)) * 2 - 1;
181
+ return top * (1 - k * k);
182
+ }
183
+
184
+ /** 0 at the top of a hop, 1 at take-off and contact: drives the air stretch (rossark §5.8). */
185
+ export function hopStretch(t: number, a: number, b: number): number {
186
+ 'worklet';
187
+ if (t <= a || t >= b) return 0;
188
+ return Math.abs(((t - a) / (b - a)) * 2 - 1);
189
+ }
190
+
191
+ /** Keep the volume: when the height scales by sy, the width scales by 1 / sy^0.6 (rossark §5.9). */
192
+ export function keepVolume(sy: number): number {
193
+ 'worklet';
194
+ return 1 / Math.pow(sy, 0.6);
195
+ }
196
+
197
+ /**
198
+ * A damped spring's step response, 0 -> 1 (may pass 1 = overshoot), t in ms.
199
+ * duration = one natural period; bounce 0 = critically damped (no overshoot), 0.15 ~ 2.5 %,
200
+ * 0.3 ~ 10 % (huashu motion.js MO.spring, SwiftUI parameters). Use only for a contact rebound
201
+ * (one overshoot, then settle) or interface moves; parts follow by delay, not springs (rossark §5.4).
202
+ */
203
+ export function spring(t: number, duration = 500, bounce = 0): number {
204
+ 'worklet';
205
+ if (t <= 0) return 0;
206
+ const s = t / 1000;
207
+ const w = (2 * Math.PI) / (duration / 1000);
208
+ const z = 1 - bounce;
209
+ if (Math.abs(z - 1) < 1e-4) return 1 - Math.exp(-w * s) * (1 + w * s);
210
+ const wd = w * Math.sqrt(1 - z * z);
211
+ return 1 - Math.exp(-z * w * s) * (Math.cos(wd * s) + ((z * w) / wd) * Math.sin(wd * s));
212
+ }
213
+
214
+ /** Degrees to radians: Skia's `rotate` takes radians. */
215
+ export const DEG = Math.PI / 180;
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.