kodeui 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +30 -2
- package/bin/kodeui.mjs +89 -0
- package/package.json +37 -4
- package/skill/kodeui/SKILL.md +105 -0
- package/skill/kodeui/assets/tokens.css +40 -0
- package/skill/kodeui/components/aurora-input/AuroraInput.tsx +153 -0
- package/skill/kodeui/components/aurora-input/aurora-input.css +218 -0
- package/skill/kodeui/components/aurora-input/index.ts +2 -0
- package/skill/kodeui/components/bloom-wall/BloomWall.tsx +90 -0
- package/skill/kodeui/components/bloom-wall/bloom-wall.css +10 -0
- package/skill/kodeui/components/bloom-wall/engine.ts +1022 -0
- package/skill/kodeui/components/bloom-wall/icons/icons-a.ts +818 -0
- package/skill/kodeui/components/bloom-wall/icons/icons-b.ts +827 -0
- package/skill/kodeui/components/bloom-wall/icons/index.ts +25 -0
- package/skill/kodeui/components/bloom-wall/icons/types.ts +8 -0
- package/skill/kodeui/components/bloom-wall/index.ts +12 -0
- package/skill/kodeui/components/bloom-wall/paint.ts +113 -0
- package/skill/kodeui/components/bloom-wall/palettes.ts +32 -0
- package/skill/kodeui/components/blur-in-text/BlurInText.tsx +53 -0
- package/skill/kodeui/components/blur-in-text/blur-in-text.css +14 -0
- package/skill/kodeui/components/blur-in-text/index.ts +2 -0
- package/skill/kodeui/components/glass-reveal/GlassReveal.tsx +108 -0
- package/skill/kodeui/components/glass-reveal/glass-reveal.css +126 -0
- package/skill/kodeui/components/glass-reveal/index.ts +2 -0
- package/skill/kodeui/components/glow-card/GlowCard.tsx +67 -0
- package/skill/kodeui/components/glow-card/glow-card.css +134 -0
- package/skill/kodeui/components/glow-card/index.ts +2 -0
- package/skill/kodeui/components/launch-chip/LaunchChip.tsx +180 -0
- package/skill/kodeui/components/launch-chip/icons.tsx +119 -0
- package/skill/kodeui/components/launch-chip/index.ts +3 -0
- package/skill/kodeui/components/launch-chip/launch-chip.css +345 -0
- package/skill/kodeui/components/ring-reveal/RingReveal.tsx +84 -0
- package/skill/kodeui/components/ring-reveal/index.ts +4 -0
- package/skill/kodeui/components/ring-reveal/ring-reveal.css +10 -0
- package/skill/kodeui/components/ring-reveal/ring.ts +298 -0
- package/skill/kodeui/components/rolling-text/RollingText.tsx +121 -0
- package/skill/kodeui/components/rolling-text/index.ts +4 -0
- package/skill/kodeui/components/rolling-text/roll.ts +94 -0
- package/skill/kodeui/components/rolling-text/rolling-text.css +43 -0
- package/skill/kodeui/components/use-live-transcript/index.ts +2 -0
- package/skill/kodeui/components/use-live-transcript/useLiveTranscript.ts +215 -0
- package/skill/kodeui/components/voice-input/VoiceInput.tsx +273 -0
- package/skill/kodeui/components/voice-input/icons.tsx +27 -0
- package/skill/kodeui/components/voice-input/index.ts +4 -0
- package/skill/kodeui/components/voice-input/useVoiceRecorder.ts +289 -0
- package/skill/kodeui/components/voice-input/voice-input.css +34 -0
- package/skill/kodeui/components/voice-note/VoiceNote.tsx +170 -0
- package/skill/kodeui/components/voice-note/index.ts +3 -0
- package/skill/kodeui/components/voice-note/noteFile.ts +25 -0
- package/skill/kodeui/components/voice-note/voice-note.css +45 -0
- package/skill/kodeui/references/aurora-input.md +44 -0
- package/skill/kodeui/references/bloom-wall.md +59 -0
- package/skill/kodeui/references/blur-in-text.md +17 -0
- package/skill/kodeui/references/frameworks.md +46 -0
- package/skill/kodeui/references/glass-reveal.md +40 -0
- package/skill/kodeui/references/glow-card.md +48 -0
- package/skill/kodeui/references/launch-chip.md +37 -0
- package/skill/kodeui/references/recipes.md +77 -0
- package/skill/kodeui/references/ring-reveal.md +35 -0
- package/skill/kodeui/references/rolling-text.md +19 -0
- package/skill/kodeui/references/theming.md +45 -0
- package/skill/kodeui/references/use-live-transcript.md +28 -0
- package/skill/kodeui/references/voice-input.md +60 -0
- package/skill/kodeui/references/voice-note.md +22 -0
- package/skill/kodeui/scripts/add.mjs +192 -0
- package/skill/kodeui/scripts/manifest.json +92 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Ring Reveal engine: a soft ring opens from a point and darkens everything behind it,
|
|
3
|
+
* then folds back into that point. Canvas 2D, no React, no other files needed:
|
|
4
|
+
* the few lines of maths it uses are kept here so this folder can be copied on its own.
|
|
5
|
+
*
|
|
6
|
+
* Open: the ring reaches the far corner in 0.85 s (0.588 s when you come back mid-fold).
|
|
7
|
+
* Fold: the far edge goes first, the area by the origin last, up to 2.45 s.
|
|
8
|
+
* Reduced motion: no ring, just a 0.25 s fade in and a 0.45 s fade out.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// ------------------------------------------------------------------ maths
|
|
12
|
+
|
|
13
|
+
const clamp = (v: number, lo: number, hi: number) => (v < lo ? lo : v > hi ? hi : v);
|
|
14
|
+
const clamp01 = (v: number) => clamp(v, 0, 1);
|
|
15
|
+
const smootherstep = (x: number) => { const t = clamp01(x); return t * t * t * (t * (t * 6 - 15) + 10); };
|
|
16
|
+
const easeOutCubic = (x: number) => 1 - Math.pow(1 - clamp01(x), 3);
|
|
17
|
+
|
|
18
|
+
export const RING_OPEN_SECONDS = 0.85;
|
|
19
|
+
export const RING_REOPEN_SECONDS = 0.588;
|
|
20
|
+
export const RING_FOLD_SECONDS = 2.45;
|
|
21
|
+
|
|
22
|
+
/** How uncovered a point at normalised distance d is, `elapsed` s into the opening. */
|
|
23
|
+
export function openCover(d: number, elapsed: number, dur: number = RING_OPEN_SECONDS): number {
|
|
24
|
+
if (elapsed <= 0 || dur <= 0) return 0;
|
|
25
|
+
if (elapsed >= dur) return 1;
|
|
26
|
+
return smootherstep((easeOutCubic(elapsed / dur) * 1.42 - clamp01(d)) / 0.42);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** How uncovered a point at normalised distance d is, `elapsed` s into the fold. */
|
|
30
|
+
export function foldCover(d: number, elapsed: number): number {
|
|
31
|
+
if (elapsed <= 0) return 1;
|
|
32
|
+
if (elapsed >= RING_FOLD_SECONDS) return 0;
|
|
33
|
+
const lead = (1 - d) * 0.16;
|
|
34
|
+
return 1 - smootherstep((clamp01(elapsed / RING_FOLD_SECONDS) - lead) / Math.max(0.05, 0.9 - d * 0.32 - lead));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Reduced motion fold: a plain 0.45 s fade. */
|
|
38
|
+
export const plainFade = (from: number, t: number) => (from <= 0 ? 0 : Math.max(0, from * (1 - smootherstep(t / 0.45))));
|
|
39
|
+
|
|
40
|
+
function readSnapshot(table: ArrayLike<number>, d: number): number {
|
|
41
|
+
const x = clamp(d, 0, 1) * 32;
|
|
42
|
+
const i = Math.floor(x);
|
|
43
|
+
const f = x - i;
|
|
44
|
+
return table[i] * (1 - f) + table[Math.min(32, i + 1)] * f;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Three close stops for the dark diagonal: the colour, a touch lighter in the middle, almost the colour at the end. */
|
|
48
|
+
function backdropStops(color: string): [string, string, string] {
|
|
49
|
+
const m = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim());
|
|
50
|
+
if (!m) return [color, color, color];
|
|
51
|
+
let hex = m[1];
|
|
52
|
+
if (hex.length === 3) hex = hex.split('').map((c) => c + c).join('');
|
|
53
|
+
const n = parseInt(hex, 16);
|
|
54
|
+
const rgb = [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
55
|
+
const lift = (by: number) => '#' + rgb.map((v) => Math.min(255, v + by).toString(16).padStart(2, '0')).join('');
|
|
56
|
+
return [lift(0), lift(6), lift(1)];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ------------------------------------------------------------------ engine
|
|
60
|
+
export type RingState = 'idle' | 'active' | 'fading';
|
|
61
|
+
|
|
62
|
+
export interface RingEngineOptions {
|
|
63
|
+
/** Where the ring starts, in the canvas's CSS px. Read when it opens and on resize. Default: (50 %, 24 %). */
|
|
64
|
+
origin?: () => { x: number; y: number };
|
|
65
|
+
/** The colour behind the ring. Default: the KodeUI night, #060c09. */
|
|
66
|
+
color?: string;
|
|
67
|
+
/** Called when a fold has finished and the canvas is clear. */
|
|
68
|
+
onIdle?: () => void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class RingEngine {
|
|
72
|
+
readonly canvas: HTMLCanvasElement;
|
|
73
|
+
readonly ctx: CanvasRenderingContext2D | null;
|
|
74
|
+
state: RingState = 'idle';
|
|
75
|
+
timeScale = 1;
|
|
76
|
+
fastFade = false;
|
|
77
|
+
speed = 1;
|
|
78
|
+
w = 0; h = 0; dpr = 1;
|
|
79
|
+
ox = 512; oy = 180; reach = 900;
|
|
80
|
+
/** came back while folding */
|
|
81
|
+
again = false;
|
|
82
|
+
inSnap: Float32Array | null = null;
|
|
83
|
+
outSnap: Float32Array | null = null;
|
|
84
|
+
/** the backdrop's alpha and its in / out clocks */
|
|
85
|
+
alpha = 0; inT = 0; inFrom = 0; outT = 0; outFrom = 0;
|
|
86
|
+
quality = 0; frameAvg = 0; frames = 0;
|
|
87
|
+
|
|
88
|
+
private opts: RingEngineOptions;
|
|
89
|
+
private stops: [string, string, string];
|
|
90
|
+
private raf: number | null = null;
|
|
91
|
+
private lastTs: number | null = null;
|
|
92
|
+
private mq: MediaQueryList | null;
|
|
93
|
+
private ro: ResizeObserver | null = null;
|
|
94
|
+
private readonly onResize = () => { this.measure(); if (this.state !== 'idle') this.paint(); };
|
|
95
|
+
private readonly onVisibility = () => {
|
|
96
|
+
if (document.visibilityState === 'hidden') this.stop();
|
|
97
|
+
else if (this.state !== 'idle') this.start();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
constructor(canvas: HTMLCanvasElement, opts: RingEngineOptions = {}) {
|
|
101
|
+
this.canvas = canvas;
|
|
102
|
+
this.ctx = canvas.getContext('2d');
|
|
103
|
+
this.opts = opts;
|
|
104
|
+
this.stops = backdropStops(opts.color ?? '#060c09');
|
|
105
|
+
this.mq = typeof window !== 'undefined' && window.matchMedia ? window.matchMedia('(prefers-reduced-motion: reduce)') : null;
|
|
106
|
+
if (typeof window !== 'undefined') window.addEventListener('resize', this.onResize);
|
|
107
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
108
|
+
this.ro = new ResizeObserver(this.onResize);
|
|
109
|
+
this.ro.observe(canvas);
|
|
110
|
+
}
|
|
111
|
+
if (typeof document !== 'undefined') document.addEventListener('visibilitychange', this.onVisibility);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get reduced(): boolean { return !!(this.mq && this.mq.matches); }
|
|
115
|
+
|
|
116
|
+
setActive(on: boolean): void {
|
|
117
|
+
if (on) {
|
|
118
|
+
this.measure();
|
|
119
|
+
if (this.state === 'idle') {
|
|
120
|
+
this.again = false; this.inSnap = this.outSnap = null; this.speed = 1;
|
|
121
|
+
this.alpha = 0; this.inT = 0; this.inFrom = 0; this.outT = 0; this.outFrom = 0;
|
|
122
|
+
this.state = 'active';
|
|
123
|
+
} else if (this.state === 'fading') {
|
|
124
|
+
this.again = true; this.speed = 1;
|
|
125
|
+
this.inSnap = this.coverTable(); this.outSnap = null;
|
|
126
|
+
this.inFrom = this.alpha; this.inT = 0;
|
|
127
|
+
this.state = 'active';
|
|
128
|
+
}
|
|
129
|
+
this.start();
|
|
130
|
+
} else if (this.state === 'active') {
|
|
131
|
+
this.outSnap = this.coverTable(); this.inSnap = null;
|
|
132
|
+
this.outFrom = this.alpha; this.outT = 0;
|
|
133
|
+
this.state = 'fading';
|
|
134
|
+
this.start();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
setFastFade(on: boolean): void { this.fastFade = !!on; }
|
|
139
|
+
setOrigin(origin: (() => { x: number; y: number }) | undefined): void { this.opts.origin = origin; }
|
|
140
|
+
setColor(color: string | undefined): void {
|
|
141
|
+
this.stops = backdropStops(color ?? '#060c09');
|
|
142
|
+
if (this.state !== 'idle') this.paint();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
destroy(): void {
|
|
146
|
+
this.stop();
|
|
147
|
+
this.state = 'idle';
|
|
148
|
+
if (typeof window !== 'undefined') window.removeEventListener('resize', this.onResize);
|
|
149
|
+
if (typeof document !== 'undefined') document.removeEventListener('visibilitychange', this.onVisibility);
|
|
150
|
+
if (this.ro) this.ro.disconnect();
|
|
151
|
+
this.ro = null;
|
|
152
|
+
if (this.ctx) {
|
|
153
|
+
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
154
|
+
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
measure(): void {
|
|
159
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
160
|
+
const win = typeof window !== 'undefined' ? window : null;
|
|
161
|
+
let w = rect.width || (win && win.innerWidth) || 1024;
|
|
162
|
+
let h = rect.height || (win && win.innerHeight) || 768;
|
|
163
|
+
const dpr = this.quality >= 1 ? 1 : clamp((win && win.devicePixelRatio) || 1, 1, 2);
|
|
164
|
+
w = Math.max(1, Math.round(w)); h = Math.max(1, Math.round(h));
|
|
165
|
+
this.w = w; this.h = h; this.dpr = dpr;
|
|
166
|
+
const pw = Math.round(w * dpr), ph = Math.round(h * dpr);
|
|
167
|
+
if (this.canvas.width !== pw) this.canvas.width = pw;
|
|
168
|
+
if (this.canvas.height !== ph) this.canvas.height = ph;
|
|
169
|
+
const o = this.opts.origin ? this.opts.origin() : null;
|
|
170
|
+
this.ox = o ? clamp(o.x, 0, w) : w * 0.5;
|
|
171
|
+
this.oy = o ? clamp(o.y, 0, h) : h * 0.24;
|
|
172
|
+
const { ox, oy } = this;
|
|
173
|
+
this.reach = Math.max(1, Math.hypot(ox, oy), Math.hypot(w - ox, oy), Math.hypot(ox, h - oy), Math.hypot(w - ox, h - oy));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** How uncovered the backdrop is at normalised distance d (0 at the origin, 1 at the far corner). */
|
|
177
|
+
cover(d: number): number {
|
|
178
|
+
d = clamp01(d);
|
|
179
|
+
if (this.state === 'idle') return 0;
|
|
180
|
+
if (this.reduced) return clamp01(this.alpha);
|
|
181
|
+
if (this.state === 'active') {
|
|
182
|
+
const r = openCover(d, this.inT, this.again ? RING_REOPEN_SECONDS : RING_OPEN_SECONDS);
|
|
183
|
+
return this.inSnap ? clamp01(Math.max(readSnapshot(this.inSnap, d), r)) : r;
|
|
184
|
+
}
|
|
185
|
+
return (this.outSnap ? readSnapshot(this.outSnap, d) : 1) * foldCover(d, this.outT);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
coverTable(): Float32Array {
|
|
189
|
+
const t = new Float32Array(33);
|
|
190
|
+
for (let i = 0; i <= 32; i++) t[i] = this.cover(i / 32);
|
|
191
|
+
return t;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
start(): void {
|
|
195
|
+
if (this.raf !== null || this.state === 'idle' || typeof document === 'undefined' || document.visibilityState === 'hidden') return;
|
|
196
|
+
this.lastTs = null;
|
|
197
|
+
const tick = (ts: number) => {
|
|
198
|
+
this.raf = null;
|
|
199
|
+
if (this.state === 'idle') return;
|
|
200
|
+
const dt = Math.min(0.1, this.lastTs === null ? 1 / 60 : Math.max(0, (ts - this.lastTs) / 1000));
|
|
201
|
+
this.lastTs = ts;
|
|
202
|
+
if (this.step(dt) && (this.state as RingState) !== 'idle' && document.visibilityState !== 'hidden') this.raf = requestAnimationFrame(tick);
|
|
203
|
+
};
|
|
204
|
+
this.raf = requestAnimationFrame(tick);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
stop(): void {
|
|
208
|
+
if (this.raf !== null) cancelAnimationFrame(this.raf);
|
|
209
|
+
this.raf = null;
|
|
210
|
+
this.lastTs = null;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Advance by dt seconds of real time and paint. Returns false when the loop may sleep. */
|
|
214
|
+
step(dt: number): boolean {
|
|
215
|
+
dt *= this.timeScale;
|
|
216
|
+
if (this.state === 'idle') return false;
|
|
217
|
+
const reduced = this.reduced;
|
|
218
|
+
if (this.state === 'active') {
|
|
219
|
+
const dur = reduced ? 0.25 : this.again ? 0.42 : RING_OPEN_SECONDS;
|
|
220
|
+
this.inT += dt;
|
|
221
|
+
this.alpha = reduced ? this.inFrom + (1 - this.inFrom) * easeOutCubic(this.inT / dur) : this.cover(0);
|
|
222
|
+
const done = this.cover(1) >= 1;
|
|
223
|
+
this.paint();
|
|
224
|
+
return !done; // fully open is a still picture: the loop can sleep
|
|
225
|
+
}
|
|
226
|
+
const target = this.fastFade ? (reduced ? 2.25 : 4) : 1;
|
|
227
|
+
this.speed += (target - this.speed) * Math.min(1, dt * 14);
|
|
228
|
+
dt *= this.speed;
|
|
229
|
+
this.outT += dt;
|
|
230
|
+
this.alpha = reduced ? plainFade(this.outFrom, this.outT) : this.cover(0);
|
|
231
|
+
let left = this.alpha;
|
|
232
|
+
for (let i = 1; i <= 10; i++) left = Math.max(left, this.cover(i / 10));
|
|
233
|
+
if (left <= 0.002) { this.finish(); return false; }
|
|
234
|
+
this.paint();
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private finish(): void {
|
|
239
|
+
this.again = false; this.inSnap = this.outSnap = null; this.alpha = 0;
|
|
240
|
+
if (this.ctx) {
|
|
241
|
+
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
242
|
+
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
243
|
+
}
|
|
244
|
+
this.stop();
|
|
245
|
+
this.state = 'idle';
|
|
246
|
+
if (this.opts.onIdle) this.opts.onIdle();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
paint(): void {
|
|
250
|
+
const ctx = this.ctx;
|
|
251
|
+
if (!ctx || this.w <= 0 || this.h <= 0) return;
|
|
252
|
+
const startedAt = performance.now();
|
|
253
|
+
const { w, h, dpr } = this;
|
|
254
|
+
const reduced = this.reduced;
|
|
255
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
256
|
+
ctx.clearRect(0, 0, w, h);
|
|
257
|
+
const r0 = this.cover(0), r1 = this.cover(1);
|
|
258
|
+
if (r0 > 0.002 || r1 > 0.002) {
|
|
259
|
+
ctx.save();
|
|
260
|
+
const open = reduced || (r0 >= 0.999 && r1 >= 0.999);
|
|
261
|
+
if (reduced) ctx.globalAlpha = clamp01(r0);
|
|
262
|
+
const diag = ctx.createLinearGradient(0, 0, w, h);
|
|
263
|
+
diag.addColorStop(0, this.stops[0]);
|
|
264
|
+
diag.addColorStop(0.5, this.stops[1]);
|
|
265
|
+
diag.addColorStop(1, this.stops[2]);
|
|
266
|
+
ctx.fillStyle = diag; ctx.fillRect(0, 0, w, h);
|
|
267
|
+
const light = ctx.createRadialGradient(w * 0.5, h * 0.44, 0, w * 0.5, h * 0.44, Math.max(w, h) * 0.58);
|
|
268
|
+
light.addColorStop(0, 'rgba(232, 241, 235, 0.025)');
|
|
269
|
+
light.addColorStop(0.6, 'rgba(232, 241, 235, 0.008)');
|
|
270
|
+
light.addColorStop(1, 'rgba(0, 0, 0, 0)');
|
|
271
|
+
ctx.fillStyle = light; ctx.fillRect(0, 0, w, h);
|
|
272
|
+
const vig = ctx.createRadialGradient(w * 0.5, h * 0.46, Math.min(w, h) * 0.24, w * 0.5, h * 0.5, Math.max(w, h) * 0.74);
|
|
273
|
+
vig.addColorStop(0, 'rgba(0, 0, 0, 0)');
|
|
274
|
+
vig.addColorStop(1, 'rgba(0, 0, 0, 0.36)');
|
|
275
|
+
ctx.fillStyle = vig; ctx.fillRect(0, 0, w, h);
|
|
276
|
+
if (!open) {
|
|
277
|
+
// cut the backdrop with the ring: a radial mask from the origin
|
|
278
|
+
ctx.globalCompositeOperation = 'destination-in';
|
|
279
|
+
const mask = ctx.createRadialGradient(this.ox, this.oy, 0, this.ox, this.oy, this.reach);
|
|
280
|
+
for (let i = 0; i <= 10; i++) mask.addColorStop(i / 10, `rgba(0, 0, 0, ${this.cover(i / 10).toFixed(3)})`);
|
|
281
|
+
ctx.fillStyle = mask;
|
|
282
|
+
ctx.fillRect(0, 0, w, h);
|
|
283
|
+
}
|
|
284
|
+
ctx.restore();
|
|
285
|
+
}
|
|
286
|
+
// slow frames (over 12 ms on average) drop to DPR 1
|
|
287
|
+
const ms = performance.now() - startedAt;
|
|
288
|
+
if (this.quality < 1) {
|
|
289
|
+
this.frameAvg = this.frames === 0 ? ms : this.frameAvg * 0.88 + ms * 0.12;
|
|
290
|
+
this.frames++;
|
|
291
|
+
if (this.frames >= 24 && this.frameAvg > 12) {
|
|
292
|
+
this.quality = 1; this.frames = 0; this.frameAvg = 0;
|
|
293
|
+
if (this.dpr > 1) { this.measure(); this.paint(); }
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { forwardRef, useCallback, useEffect, useLayoutEffect, useRef, type CSSProperties, type ForwardedRef } from 'react';
|
|
2
|
+
import { planRoll, ROLL_CYCLE } from './roll';
|
|
3
|
+
import './rolling-text.css';
|
|
4
|
+
|
|
5
|
+
export interface RollingTextProps {
|
|
6
|
+
/** The lines to roll through. The first one shows at rest; the track ends on it again. */
|
|
7
|
+
items: string[];
|
|
8
|
+
/** Seconds before the first roll cycle starts. Default 0. */
|
|
9
|
+
delay?: number;
|
|
10
|
+
/** Seconds for one cycle of two items. Default 8. */
|
|
11
|
+
duration?: number;
|
|
12
|
+
/** Repeat the cycle for ever. Default false (it plays once, like the reference). */
|
|
13
|
+
loop?: boolean;
|
|
14
|
+
className?: string;
|
|
15
|
+
style?: CSSProperties;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const useIsoLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Text that rolls up to the next line and back inside a soft-edged window. The window's
|
|
22
|
+
* width follows the line on show, so a pill around it grows and shrinks to fit.
|
|
23
|
+
* Driven by the Web Animations API; nothing moves when "reduce motion" is on.
|
|
24
|
+
*/
|
|
25
|
+
export const RollingText = forwardRef<HTMLSpanElement, RollingTextProps>(function RollingText(
|
|
26
|
+
{ items, delay = 0, duration = ROLL_CYCLE, loop = false, className, style },
|
|
27
|
+
ref,
|
|
28
|
+
) {
|
|
29
|
+
const windowRef = useRef<HTMLSpanElement | null>(null);
|
|
30
|
+
const trackRef = useRef<HTMLSpanElement | null>(null);
|
|
31
|
+
const signature = items.join('␟');
|
|
32
|
+
const count = items.length;
|
|
33
|
+
|
|
34
|
+
useIsoLayoutEffect(() => {
|
|
35
|
+
const win = windowRef.current;
|
|
36
|
+
const track = trackRef.current;
|
|
37
|
+
if (!win || !track || count < 2 || typeof win.animate !== 'function') return;
|
|
38
|
+
|
|
39
|
+
const reduce = typeof window.matchMedia === 'function' ? window.matchMedia('(prefers-reduced-motion: reduce)') : null;
|
|
40
|
+
const itemEls = () => Array.from(track.children).slice(0, count) as HTMLElement[];
|
|
41
|
+
const measure = () => itemEls().map((el) => el.offsetWidth);
|
|
42
|
+
|
|
43
|
+
let anims: Animation[] = [];
|
|
44
|
+
let measured = '';
|
|
45
|
+
|
|
46
|
+
const stop = () => {
|
|
47
|
+
anims.forEach((a) => a.cancel());
|
|
48
|
+
anims = [];
|
|
49
|
+
};
|
|
50
|
+
const start = () => {
|
|
51
|
+
stop();
|
|
52
|
+
if (reduce?.matches) return;
|
|
53
|
+
const widths = measure();
|
|
54
|
+
measured = widths.join(',');
|
|
55
|
+
const plan = planRoll(widths, duration);
|
|
56
|
+
const timing: KeyframeAnimationOptions = {
|
|
57
|
+
duration: plan.duration,
|
|
58
|
+
delay: delay * 1000,
|
|
59
|
+
iterations: loop ? Infinity : 1,
|
|
60
|
+
fill: 'both',
|
|
61
|
+
easing: 'linear',
|
|
62
|
+
};
|
|
63
|
+
anims = [win.animate(plan.window, timing), track.animate(plan.track, timing)];
|
|
64
|
+
};
|
|
65
|
+
// fonts arriving late change the widths: swap the keyframes in place, the clock keeps running
|
|
66
|
+
const refit = () => {
|
|
67
|
+
if (!anims.length) return;
|
|
68
|
+
const widths = measure();
|
|
69
|
+
const next = widths.join(',');
|
|
70
|
+
if (next === measured) return;
|
|
71
|
+
measured = next;
|
|
72
|
+
const plan = planRoll(widths, duration);
|
|
73
|
+
(anims[0].effect as KeyframeEffect | null)?.setKeyframes(plan.window);
|
|
74
|
+
(anims[1].effect as KeyframeEffect | null)?.setKeyframes(plan.track);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
start();
|
|
78
|
+
|
|
79
|
+
let ro: ResizeObserver | null = null;
|
|
80
|
+
if (typeof ResizeObserver === 'function') {
|
|
81
|
+
ro = new ResizeObserver(refit);
|
|
82
|
+
itemEls().forEach((el) => ro!.observe(el));
|
|
83
|
+
}
|
|
84
|
+
const fonts = typeof document !== 'undefined' ? document.fonts : undefined;
|
|
85
|
+
let live = true;
|
|
86
|
+
fonts?.ready.then(() => { if (live) refit(); }, () => {});
|
|
87
|
+
const onMotionPref = () => start();
|
|
88
|
+
reduce?.addEventListener?.('change', onMotionPref);
|
|
89
|
+
|
|
90
|
+
return () => {
|
|
91
|
+
live = false;
|
|
92
|
+
ro?.disconnect();
|
|
93
|
+
reduce?.removeEventListener?.('change', onMotionPref);
|
|
94
|
+
stop();
|
|
95
|
+
};
|
|
96
|
+
}, [signature, count, delay, duration, loop]);
|
|
97
|
+
|
|
98
|
+
const setRoot = useCallback((el: HTMLSpanElement | null) => {
|
|
99
|
+
windowRef.current = el;
|
|
100
|
+
assignRef(ref, el);
|
|
101
|
+
}, [ref]);
|
|
102
|
+
|
|
103
|
+
if (count === 0) return null;
|
|
104
|
+
const rolls = count > 1;
|
|
105
|
+
return (
|
|
106
|
+
<span ref={setRoot} className={className ? `ku-rolling-text ${className}` : 'ku-rolling-text'} style={style}>
|
|
107
|
+
{rolls && <span className="ku-rolling-text__sr">{items.join('. ')}</span>}
|
|
108
|
+
<span ref={trackRef} className="ku-rolling-text__track" aria-hidden={rolls || undefined}>
|
|
109
|
+
{items.map((item, i) => (
|
|
110
|
+
<span key={i} className="ku-rolling-text__item" style={i > 0 ? { top: `${i * 100}%` } : undefined}>{item}</span>
|
|
111
|
+
))}
|
|
112
|
+
{rolls && <span className="ku-rolling-text__item" style={{ top: `${count * 100}%` }}>{items[0]}</span>}
|
|
113
|
+
</span>
|
|
114
|
+
</span>
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
function assignRef<T>(ref: ForwardedRef<T>, value: T | null) {
|
|
119
|
+
if (typeof ref === 'function') ref(value);
|
|
120
|
+
else if (ref) ref.current = value;
|
|
121
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rolling schedule as Web Animations keyframes. No React, no DOM reads: give it the
|
|
3
|
+
* measured width of each item and the cycle length, get back keyframes for the window
|
|
4
|
+
* (its width follows the line on show) and for the track (moves up one line at a time).
|
|
5
|
+
*
|
|
6
|
+
* Two items is the reference cycle (8 s by default):
|
|
7
|
+
* longer second line: hold 2 s, widen 0.5 s, roll up 1 s, hold 3 s, roll up 1 s, narrow 0.5 s
|
|
8
|
+
* shorter second line: hold 2 s, roll up 1 s, narrow 0.6 s, hold 2.4 s, widen 0.6 s, roll up 1 s, hold 0.4 s
|
|
9
|
+
* The track holds the first line again at the end, so a loop starts again without a jump.
|
|
10
|
+
* Three or more items repeat the same moves, one item every half cycle.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** The ease of every move. */
|
|
14
|
+
export const ROLL_EASE = 'cubic-bezier(.65,0,.35,1)';
|
|
15
|
+
/** Seconds for one full cycle with two items. */
|
|
16
|
+
export const ROLL_CYCLE = 8;
|
|
17
|
+
|
|
18
|
+
export interface RollPlan {
|
|
19
|
+
/** Length of one iteration in ms. */
|
|
20
|
+
duration: number;
|
|
21
|
+
/** Keyframes for the window: padding-right grows it, a negative margin-right shrinks it. */
|
|
22
|
+
window: Keyframe[];
|
|
23
|
+
/** Keyframes for the track: translateY in whole lines. */
|
|
24
|
+
track: Keyframe[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type Stop = [offset: number, value: number];
|
|
28
|
+
|
|
29
|
+
const px = (n: number) => `${n}px`;
|
|
30
|
+
const lines = (n: number) => `translateY(${n === 0 ? 0 : n * 100}%)`;
|
|
31
|
+
|
|
32
|
+
function frames(stops: Stop[], toFrame: (v: number) => Keyframe): Keyframe[] {
|
|
33
|
+
return stops.map(([offset, v]) => ({ offset, easing: ROLL_EASE, ...toFrame(v) }));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param widths offsetWidth of each item, first item first (the window's natural width is widths[0])
|
|
38
|
+
* @param cycle seconds for one cycle of two items
|
|
39
|
+
*/
|
|
40
|
+
export function planRoll(widths: number[], cycle = ROLL_CYCLE): RollPlan {
|
|
41
|
+
const n = widths.length;
|
|
42
|
+
if (n < 2) {
|
|
43
|
+
return { duration: Math.max(1, cycle * 1000), window: [{ paddingRight: '0px' }, { paddingRight: '0px' }], track: [{ transform: lines(0) }, { transform: lines(0) }] };
|
|
44
|
+
}
|
|
45
|
+
if (n === 2) return planPair(widths[1] - widths[0], cycle);
|
|
46
|
+
return planMany(widths, cycle);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The reference cycle: two lines, first / second / first again. */
|
|
50
|
+
function planPair(delta: number, cycle: number): RollPlan {
|
|
51
|
+
const duration = cycle * 1000;
|
|
52
|
+
if (delta >= 0) {
|
|
53
|
+
return {
|
|
54
|
+
duration,
|
|
55
|
+
window: frames([[0, 0], [0.25, 0], [0.3125, delta], [0.9375, delta], [1, 0]], (v) => ({ paddingRight: px(v) })),
|
|
56
|
+
track: frames([[0, 0], [0.3125, 0], [0.4375, -1], [0.8125, -1], [0.9375, -2], [1, -2]], (v) => ({ transform: lines(v) })),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
duration,
|
|
61
|
+
window: frames([[0, 0], [0.375, 0], [0.45, delta], [0.75, delta], [0.825, 0], [1, 0]], (v) => ({ marginRight: px(v) })),
|
|
62
|
+
track: frames([[0, 0], [0.25, 0], [0.375, -1], [0.825, -1], [0.95, -2], [1, -2]], (v) => ({ transform: lines(v) })),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Three or more lines: each step gets half a cycle, with the same moves as the first step of a pair. */
|
|
67
|
+
function planMany(widths: number[], cycle: number): RollPlan {
|
|
68
|
+
const n = widths.length;
|
|
69
|
+
const unit = cycle / 8; // 1 s at the default cycle
|
|
70
|
+
const total = (cycle / 2) * n; // seconds per iteration
|
|
71
|
+
const off = (i: number) => (i % n === 0 ? 0 : widths[i] - widths[0]);
|
|
72
|
+
const win: Array<[number, number]> = [[0, 0]];
|
|
73
|
+
const trk: Array<[number, number]> = [[0, 0]];
|
|
74
|
+
for (let k = 0; k < n; k++) {
|
|
75
|
+
const start = k * (cycle / 2) + 2 * unit;
|
|
76
|
+
const from = off(k);
|
|
77
|
+
const to = off(k + 1);
|
|
78
|
+
if (to >= from) {
|
|
79
|
+
win.push([start, from], [start + 0.5 * unit, to]);
|
|
80
|
+
trk.push([start + 0.5 * unit, -k], [start + 1.5 * unit, -(k + 1)]);
|
|
81
|
+
} else {
|
|
82
|
+
trk.push([start, -k], [start + unit, -(k + 1)]);
|
|
83
|
+
win.push([start + unit, from], [start + 1.6 * unit, to]);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
win.push([total, 0]);
|
|
87
|
+
trk.push([total, -n]);
|
|
88
|
+
const toStops = (list: Array<[number, number]>): Stop[] => list.map(([t, v]) => [Math.min(1, Math.max(0, t / total)), v]);
|
|
89
|
+
return {
|
|
90
|
+
duration: total * 1000,
|
|
91
|
+
window: frames(toStops(win), (v) => ({ paddingRight: px(Math.max(0, v)), marginRight: px(Math.min(0, v)) })),
|
|
92
|
+
track: frames(toStops(trk), (v) => ({ transform: lines(v) })),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/* Rolling Text: a one-line window with soft top and bottom edges over a column of lines. */
|
|
2
|
+
.ku-rolling-text {
|
|
3
|
+
--ku-rolling-line: 1.5em;
|
|
4
|
+
position: relative;
|
|
5
|
+
display: inline-flex;
|
|
6
|
+
align-items: flex-start;
|
|
7
|
+
height: var(--ku-rolling-line);
|
|
8
|
+
overflow: hidden;
|
|
9
|
+
padding-right: 0;
|
|
10
|
+
-webkit-mask-image: linear-gradient(transparent 0, #000 12%, #000 88%, transparent);
|
|
11
|
+
mask-image: linear-gradient(transparent 0, #000 12%, #000 88%, transparent);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.ku-rolling-text__track {
|
|
15
|
+
position: relative;
|
|
16
|
+
display: inline-flex;
|
|
17
|
+
flex-direction: column;
|
|
18
|
+
align-items: flex-start;
|
|
19
|
+
height: var(--ku-rolling-line);
|
|
20
|
+
will-change: transform;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.ku-rolling-text__item {
|
|
24
|
+
display: inline-flex;
|
|
25
|
+
align-items: center;
|
|
26
|
+
flex-shrink: 0;
|
|
27
|
+
height: var(--ku-rolling-line);
|
|
28
|
+
line-height: var(--ku-rolling-line);
|
|
29
|
+
white-space: nowrap;
|
|
30
|
+
}
|
|
31
|
+
.ku-rolling-text__item + .ku-rolling-text__item { position: absolute; left: 0; }
|
|
32
|
+
|
|
33
|
+
.ku-rolling-text__sr {
|
|
34
|
+
position: absolute;
|
|
35
|
+
width: 1px;
|
|
36
|
+
height: 1px;
|
|
37
|
+
margin: -1px;
|
|
38
|
+
padding: 0;
|
|
39
|
+
overflow: hidden;
|
|
40
|
+
clip-path: inset(50%);
|
|
41
|
+
white-space: nowrap;
|
|
42
|
+
border: 0;
|
|
43
|
+
}
|