@jsb188/gfx 1.1.2

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.
Files changed (65) hide show
  1. package/README.md +522 -0
  2. package/elements/Shapes.helpers.ts +204 -0
  3. package/elements/Shapes.hooks.ts +171 -0
  4. package/elements/Shapes.tsx +505 -0
  5. package/elements/Shapes.types.ts +81 -0
  6. package/elements/Stack.helpers.ts +42 -0
  7. package/elements/Stack.tsx +143 -0
  8. package/elements/Text.helpers.ts +45 -0
  9. package/elements/Text.hooks.ts +197 -0
  10. package/elements/Text.tsx +98 -0
  11. package/libs/chain.ts +174 -0
  12. package/libs/dotGrid.helpers.ts +53 -0
  13. package/libs/dotGrid.ts +954 -0
  14. package/libs/effects/dotEffects.ts +136 -0
  15. package/libs/effects/effectQuad.ts +146 -0
  16. package/libs/effects/effectRegion.ts +299 -0
  17. package/libs/effects/imageEffects.ts +599 -0
  18. package/libs/effects/index.ts +8 -0
  19. package/libs/effects/ripple.ts +160 -0
  20. package/libs/glsl.ts +134 -0
  21. package/libs/passes.ts +374 -0
  22. package/libs/pointer.ts +151 -0
  23. package/libs/runtime.ts +329 -0
  24. package/libs/texture.ts +160 -0
  25. package/libs/types.ts +94 -0
  26. package/libs/uniforms.ts +32 -0
  27. package/main.ts +4 -0
  28. package/modules/Carousel.helpers.ts +316 -0
  29. package/modules/Carousel.hooks.ts +520 -0
  30. package/modules/Carousel.tsx +631 -0
  31. package/modules/Dots.tsx +269 -0
  32. package/modules/DotsBoard.helpers.ts +388 -0
  33. package/modules/DotsBoard.hooks.ts +144 -0
  34. package/modules/DotsBoard.strings.ts +649 -0
  35. package/modules/DotsBoard.types.ts +240 -0
  36. package/modules/DotsBoardCanvas.hooks.ts +49 -0
  37. package/modules/DotsBoardCanvas.tsx +190 -0
  38. package/modules/DotsBoardElements.tsx +330 -0
  39. package/modules/Gallery.helpers.ts +32 -0
  40. package/modules/Gallery.hooks.ts +149 -0
  41. package/modules/Gallery.tsx +194 -0
  42. package/modules/GfxCanvas.hooks.ts +12 -0
  43. package/modules/GfxCanvas.tsx +189 -0
  44. package/modules/Hero.tsx +90 -0
  45. package/modules/KineticHeader.helpers.ts +44 -0
  46. package/modules/KineticHeader.hooks.ts +206 -0
  47. package/modules/KineticHeader.tsx +305 -0
  48. package/modules/KineticHeader.types.ts +56 -0
  49. package/modules/ScrollSteps.hooks.ts +138 -0
  50. package/modules/ScrollSteps.tsx +98 -0
  51. package/package.json +52 -0
  52. package/scenes/Carousel.ts +277 -0
  53. package/scenes/DotsBoard.helpers.ts +12 -0
  54. package/scenes/DotsBoard.ts +245 -0
  55. package/scenes/Hero.ts +280 -0
  56. package/tsconfig.json +13 -0
  57. package/utils/align.ts +18 -0
  58. package/utils/color.ts +112 -0
  59. package/utils/dotEffectSpecs.ts +79 -0
  60. package/utils/dotGridSpecs.ts +469 -0
  61. package/utils/easing.ts +18 -0
  62. package/utils/imageEffectSpecs.ts +89 -0
  63. package/utils/motion.ts +88 -0
  64. package/utils/random.ts +22 -0
  65. package/utils/strings.ts +116 -0
@@ -0,0 +1,277 @@
1
+ import { createEffectQuad } from '../libs/effects/effectQuad.ts';
2
+ import type { EffectQuad } from '../libs/effects/effectQuad.ts';
3
+ import { createImageEffects, disposeImageEffects } from '../libs/effects/imageEffects.ts';
4
+ import type { ImageEffectInstance, ImageEffectSpec } from '../libs/effects/imageEffects.ts';
5
+ import { createVideoTexture } from '../libs/texture.ts';
6
+ import type { VideoTexture } from '../libs/texture.ts';
7
+ import type { SceneContext, Scene, SceneSize, RGB } from '../libs/types.ts';
8
+ import { cssColorToRgb, hexToRgb } from '../utils/color.ts';
9
+ import { easeInOutCubic } from '../utils/easing.ts';
10
+
11
+ /**
12
+ * Types
13
+ */
14
+
15
+ export interface CarouselSceneSlide {
16
+ /** Stable identity of the slide; also the key its <video> is registered under. */
17
+ key: string;
18
+ /** The focused slide is drawn with the effect fully off; every other slide has it fully on. */
19
+ focused: boolean;
20
+ /** Overall thumbnail opacity, animated independently of focus; defaults to 1. */
21
+ opacity?: number;
22
+ }
23
+
24
+ export interface CarouselSceneParams {
25
+ slides: CarouselSceneSlide[];
26
+ /** Live lookup of each slide's <video> by key. The component fills it from ref callbacks, so the scene reads it every frame. */
27
+ videos: ReadonlyMap<string, HTMLVideoElement>;
28
+ /** Default true; disable for galleries whose thumbnails load independently as they enter view. */
29
+ waitForAllVideos?: boolean;
30
+ /** Draw paused thumbnail frames at their wrappers only when they change; native overlays own active playback. */
31
+ pausedThumbnails?: boolean;
32
+ /** Effects applied to the unfocused slides, in order. */
33
+ effects: ImageEffectSpec[];
34
+ /** How long the effect takes to fade in or out when focus moves (ms); match the slide transition. */
35
+ transitionMs: number;
36
+ /** Corner radius of every video, in CSS pixels. */
37
+ borderRadius: number;
38
+ /** Opacity of a fully unfocused video; the focused one is opaque and the fade follows the effect. */
39
+ unfocusedOpacity: number;
40
+ /** Fallback hex color behind a video when its element's own background cannot be read. */
41
+ backdropColor: string;
42
+ }
43
+
44
+ interface CarouselSlideState {
45
+ video: HTMLVideoElement | null;
46
+ texture: VideoTexture | null;
47
+ unsubscribeVideo: (() => void) | null;
48
+ /** Background of the video's wrapper, seen through the dimmed video in place of the page. */
49
+ backdrop: RGB;
50
+ /** Current effect strength, eased between `from` and `to` on the wall clock. */
51
+ amount: number;
52
+ from: number;
53
+ to: number;
54
+ opacity: number;
55
+ opacityFrom: number;
56
+ opacityTo: number;
57
+ startedAt: number;
58
+ }
59
+
60
+ /*
61
+ * Build the carousel scene: one transparent canvas laid over the whole carousel, on which every
62
+ * slide's video is redrawn each frame at the exact place its <video> element occupies (read from
63
+ * the DOM, so CSS slide transitions carry the drawing along), through the effect list at a
64
+ * per-slide strength. The focused slide eases to 0 (an exact no-op, so it looks like the plain
65
+ * video) and the others ease to 1, fading to `unfocusedOpacity` on the same curve. Each slide's
66
+ * overall opacity can also animate independently without changing its effect. Videos that lie
67
+ * entirely outside the canvas are skipped. By default the scene waits for every registered video
68
+ * to have a frame; galleries can opt out so thumbnails appear independently as their videos load.
69
+ * Paused thumbnails render on demand, waking only for frame readiness, opacity transitions, or layout changes.
70
+ */
71
+ export default function createCarouselScene(
72
+ { gl, invalidate, onDispose }: SceneContext,
73
+ params: CarouselSceneParams,
74
+ ): Scene<CarouselSceneParams> {
75
+ const canvas = gl.canvas as HTMLCanvasElement;
76
+ const states = new Map<string, CarouselSlideState>();
77
+ let quad: EffectQuad | null = null;
78
+ let effects: ImageEffectInstance[] = [];
79
+ let effectsKey = '';
80
+ let size: SceneSize = { width: 1, height: 1, pixelRatio: 1 };
81
+ let current = params;
82
+ let fallbackBackdrop = hexToRgb(params.backdropColor, [1, 1, 1]);
83
+ // Reused per draw so a frame allocates nothing.
84
+ const rect = { x: 0, y: 0, width: 0, height: 0 };
85
+ const pointerUv: [number, number] = [0.5, 0.5];
86
+ onDispose(() => {
87
+ for (const state of states.values()) disposeVideo(state);
88
+ states.clear();
89
+ quad?.dispose();
90
+ disposeImageEffects(effects);
91
+ });
92
+
93
+ /*
94
+ * (Re)compile the quad for a new effect list; effects own GPU state, so the previous set goes with the previous program.
95
+ */
96
+ function buildQuad(specs: ImageEffectSpec[]) {
97
+ const nextKey = JSON.stringify(specs);
98
+ if (quad && nextKey === effectsKey) return;
99
+ effectsKey = nextKey;
100
+ quad?.dispose();
101
+ disposeImageEffects(effects);
102
+ effects = createImageEffects(gl, specs, 'uFx');
103
+ quad = createEffectQuad(gl, effects);
104
+ }
105
+
106
+ /*
107
+ * Retarget every slide's effect strength for the new focus and drop slides that went away.
108
+ */
109
+ function applySlides(next: CarouselSceneParams) {
110
+ const now = performance.now();
111
+ const keep = new Set<string>();
112
+ for (const slide of next.slides) {
113
+ keep.add(slide.key);
114
+ const target = slide.focused ? 0 : 1;
115
+ const opacity = Math.max(0, Math.min(1, slide.opacity ?? 1));
116
+ let state = states.get(slide.key);
117
+ if (!state) {
118
+ // A slide that appears already in its resting state does not animate in.
119
+ state = {
120
+ video: null,
121
+ texture: null,
122
+ unsubscribeVideo: null,
123
+ backdrop: fallbackBackdrop,
124
+ amount: target,
125
+ from: target,
126
+ to: target,
127
+ opacity,
128
+ opacityFrom: opacity,
129
+ opacityTo: opacity,
130
+ startedAt: now,
131
+ };
132
+ states.set(slide.key, state);
133
+ } else if (state.to !== target || state.opacityTo !== opacity) {
134
+ state.from = state.amount;
135
+ state.to = target;
136
+ state.opacityFrom = state.opacity;
137
+ state.opacityTo = opacity;
138
+ state.startedAt = now;
139
+ }
140
+ }
141
+ for (const [key, state] of states) {
142
+ if (keep.has(key)) continue;
143
+ disposeVideo(state);
144
+ states.delete(key);
145
+ }
146
+ }
147
+
148
+ /*
149
+ * Release a slide's texture and readiness listeners when its video changes or the slide leaves.
150
+ */
151
+ function disposeVideo(state: CarouselSlideState) {
152
+ state.unsubscribeVideo?.();
153
+ state.unsubscribeVideo = null;
154
+ state.texture?.dispose();
155
+ state.texture = null;
156
+ state.video = null;
157
+ }
158
+
159
+ /*
160
+ * Bind the slide to whatever <video> is registered for it right now, swapping textures if the element changed.
161
+ */
162
+ function syncVideo(state: CarouselSlideState, video: HTMLVideoElement | null) {
163
+ if (state.video === video) return;
164
+ disposeVideo(state);
165
+ state.video = video;
166
+ state.texture = video ? createVideoTexture(gl, video) : null;
167
+ if (video) {
168
+ const events = ['loadeddata', 'canplay', 'seeked', 'pause', 'error'] as const;
169
+ for (const event of events) video.addEventListener(event, invalidate);
170
+ state.unsubscribeVideo = () => {
171
+ for (const event of events) video.removeEventListener(event, invalidate);
172
+ };
173
+ }
174
+ // The wrapper's CSS background is the source of truth for what a dimmed video fades toward.
175
+ const wrapper = video?.parentElement;
176
+ state.backdrop = wrapper ? cssColorToRgb(getComputedStyle(wrapper).backgroundColor, fallbackBackdrop) : fallbackBackdrop;
177
+ }
178
+
179
+ buildQuad(params.effects);
180
+ applySlides(params);
181
+
182
+ return {
183
+ /*
184
+ * Keep full-rate playback for carousels and stop between changes for paused galleries.
185
+ */
186
+ get frameloop() {
187
+ return current.pausedThumbnails ? 'demand' : 'always';
188
+ },
189
+ /*
190
+ * Paused thumbnails have no pointer effect; their native previews handle interaction.
191
+ */
192
+ get followsPointer() {
193
+ return !current.pausedThumbnails;
194
+ },
195
+ // The frames are the videos themselves (the plain ones hide under the canvas), so they keep
196
+ // playing at full rate: never at the idle rate, nor paused for reduced motion.
197
+ idleFrameRate: 0,
198
+ pauseForReducedMotion: false,
199
+ isReady: () =>
200
+ current.waitForAllVideos === false ||
201
+ current.slides.every(
202
+ (slide) => !current.videos.has(slide.key) || current.videos.get(slide.key)?.error || states.get(slide.key)?.texture?.loaded,
203
+ ),
204
+ resize(nextSize) {
205
+ size = nextSize;
206
+ },
207
+ update() {
208
+ const now = performance.now();
209
+ for (const state of states.values()) {
210
+ if (state.amount === state.to && state.opacity === state.opacityTo) continue;
211
+ const t = Math.min(1, (now - state.startedAt) / Math.max(1, current.transitionMs));
212
+ const eased = easeInOutCubic(t);
213
+ state.amount = state.from + (state.to - state.from) * eased;
214
+ state.opacity = state.opacityFrom + (state.opacityTo - state.opacityFrom) * eased;
215
+ if (t < 1) invalidate();
216
+ }
217
+ },
218
+ render({ elapsed, pointer }) {
219
+ if (!quad) return;
220
+ gl.clearColor(0, 0, 0, 0);
221
+ gl.clear(gl.COLOR_BUFFER_BIT);
222
+ const canvasRect = canvas.getBoundingClientRect();
223
+ // Eased pointer, canvas UV (y up) to CSS pixels (y down) from the canvas's corner.
224
+ const pointerX = pointer.x * size.width;
225
+ const pointerY = (1 - pointer.y) * size.height;
226
+ for (const slide of current.slides) {
227
+ const state = states.get(slide.key);
228
+ if (!state) continue;
229
+ syncVideo(state, current.videos.get(slide.key) ?? null);
230
+ const { video, texture } = state;
231
+ if (!video || !texture) continue;
232
+ // A carousel's initial reveal still waits for registered videos outside the visible canvas.
233
+ if (current.waitForAllVideos !== false) texture.update();
234
+ if (state.opacity <= 0) continue;
235
+ // The enlarged native preview must not stretch the gallery's fixed thumbnail underneath it.
236
+ const videoRect = (current.pausedThumbnails ? (video.parentElement ?? video) : video).getBoundingClientRect();
237
+ rect.x = videoRect.left - canvasRect.left;
238
+ rect.y = videoRect.top - canvasRect.top;
239
+ rect.width = videoRect.width;
240
+ rect.height = videoRect.height;
241
+ // Skip what the carousel has clipped away entirely.
242
+ if (
243
+ rect.width <= 0 ||
244
+ rect.height <= 0 ||
245
+ rect.x + rect.width <= 0 ||
246
+ rect.x >= size.width ||
247
+ rect.y + rect.height <= 0 ||
248
+ rect.y >= size.height
249
+ )
250
+ continue;
251
+ if (current.waitForAllVideos === false) texture.update();
252
+ if (!texture.loaded) continue;
253
+ pointerUv[0] = (pointerX - rect.x) / rect.width;
254
+ pointerUv[1] = 1 - (pointerY - rect.y) / rect.height;
255
+ quad.draw({
256
+ image: texture.texture,
257
+ imageSize: texture.size,
258
+ rect,
259
+ canvas: size,
260
+ amount: state.amount,
261
+ pointer: pointerUv,
262
+ time: elapsed,
263
+ radius: current.borderRadius,
264
+ opacity: 1 - state.amount * (1 - current.unfocusedOpacity),
265
+ alpha: state.opacity,
266
+ backdrop: state.backdrop,
267
+ });
268
+ }
269
+ },
270
+ setParams(next) {
271
+ current = next;
272
+ fallbackBackdrop = hexToRgb(next.backdropColor, [1, 1, 1]);
273
+ buildQuad(next.effects);
274
+ applySlides(next);
275
+ },
276
+ };
277
+ }
@@ -0,0 +1,12 @@
1
+ import { easeInOutCubic, easeOutCubic } from '../utils/easing.ts';
2
+
3
+ /*
4
+ * Animate from the initial size to the resting size, or reach a larger peak halfway through the reveal
5
+ * and settle back. Both phases meet at rest so the change of direction stays smooth.
6
+ */
7
+ export function getDotAppearSize(progress: number, size: number, appearSize: number, initialSize = 0) {
8
+ const amount = Math.min(1, Math.max(0, progress));
9
+ if (appearSize <= size) return initialSize + (size - initialSize) * easeOutCubic(amount);
10
+ if (amount < 0.5) return initialSize + (appearSize - initialSize) * easeOutCubic(amount * 2);
11
+ return appearSize + (size - appearSize) * easeInOutCubic((amount - 0.5) * 2);
12
+ }
@@ -0,0 +1,245 @@
1
+ import type { RefObject } from 'react';
2
+ import { createDotGrid } from '../libs/dotGrid.ts';
3
+ import { createImageTexture } from '../libs/texture.ts';
4
+ import type { ImageTexture } from '../libs/texture.ts';
5
+ import type { SceneContext, Scene, SceneSize } from '../libs/types.ts';
6
+ import type { DotsBoardBottomFade, DotsBoardHover, DotsBoardImage, DotsBoardIntro, Offset } from '../modules/DotsBoard.types.ts';
7
+ import type { DotsBoardView } from '../modules/DotsBoardCanvas.hooks.ts';
8
+ import type { ResolvedDotGridOptions } from '../utils/dotGridSpecs.ts';
9
+ import { cssColorToRgb, cssColorToRgba } from '../utils/color.ts';
10
+ import { prefersReducedMotion } from '../utils/motion.ts';
11
+ import { getDotsBoardBlendColor, getDotsBoardBlendMode } from '../modules/DotsBoard.helpers.ts';
12
+ import { getDotAppearSize } from './DotsBoard.helpers.ts';
13
+
14
+ /** A logical board layer rendered through a bounded, scrolling canvas window. */
15
+ export interface DotsBoardSceneParams extends ResolvedDotGridOptions {
16
+ boardRef: RefObject<HTMLDivElement | null>;
17
+ hostRef: RefObject<HTMLDivElement | null>;
18
+ view: DotsBoardView;
19
+ width: number;
20
+ height: number;
21
+ image?: DotsBoardImage;
22
+ imageOffset: Offset;
23
+ imageWidth: number;
24
+ intro?: DotsBoardIntro;
25
+ hover?: DotsBoardHover;
26
+ fadeDotsBottom?: DotsBoardBottomFade;
27
+ /** Draw only blend-source dots in the bottom band for CSS backdrop compositing. */
28
+ blend?: boolean;
29
+ begin: number;
30
+ }
31
+
32
+ /*
33
+ * Render a scrolling or sticky layer with the board's shared clock. The full logical grid
34
+ * keeps its spring state while only a viewport-sized framebuffer is drawn. Board clicks
35
+ * reach every visible layer in local coordinates, including effects crossing its edges.
36
+ */
37
+ export default function createDotsBoardScene(
38
+ { gl, signal, invalidate, onDispose }: SceneContext,
39
+ initial: DotsBoardSceneParams,
40
+ ): Scene<DotsBoardSceneParams> {
41
+ let params = initial;
42
+ const grid = createDotGrid(gl, params);
43
+ let surface: SceneSize = { width: 1, height: 1, pixelRatio: 1 };
44
+ let overlay: ImageTexture | null = null;
45
+ let overlayUrl: string | undefined;
46
+ let overlayFailed = false;
47
+ const previousClient: [number, number] = [0, 0];
48
+ let hadPointer = false;
49
+ let hoverAmount = 0;
50
+ const pointerPx: [number, number] = [0, 0];
51
+ const velocity: [number, number] = [0, 0];
52
+ const board = params.boardRef.current;
53
+ params.view.invalidate = invalidate;
54
+ board?.addEventListener('click', handleClick, { capture: true, passive: true });
55
+ onDispose(() => {
56
+ board?.removeEventListener('click', handleClick, true);
57
+ if (params.view.invalidate === invalidate) params.view.invalidate = undefined;
58
+ grid.dispose();
59
+ overlay?.dispose();
60
+ });
61
+
62
+ /*
63
+ * All layers use wall time, so lazy initialization and offscreen pauses never restart effects.
64
+ */
65
+ function clock() {
66
+ return Math.max(0, (performance.now() - params.begin) / 1000);
67
+ }
68
+
69
+ /*
70
+ * The end of the optional appear intro, in seconds on the shared board clock.
71
+ */
72
+ function introEnd() {
73
+ return params.intro ? (Math.max(0, params.intro.delay ?? 0) + Math.max(1, params.intro.duration ?? 1000)) / 1000 : 0;
74
+ }
75
+
76
+ /*
77
+ * Observe the board's click once per layer without consuming content clicks. Include nearby
78
+ * points outside this layer so a ripple crossing a sticky boundary is drawn on both sides.
79
+ */
80
+ function handleClick(event: MouseEvent) {
81
+ if (!params.click || event.button !== 0 || event.detail === 0 || clock() < introEnd() || prefersReducedMotion()) return;
82
+ const rect = params.hostRef.current?.getBoundingClientRect();
83
+ if (!rect || rect.width <= 0 || rect.height <= 0) return;
84
+ const x = ((event.clientX - rect.left) * params.width) / rect.width;
85
+ const y = ((event.clientY - rect.top) * params.height) / rect.height;
86
+ const radius = Math.max(params.click.radius, params.click.kickStrength ? params.click.kickRadius : 0);
87
+ if (x < -radius || y < -radius || x > params.width + radius || y > params.height + radius) return;
88
+ if (grid.clickAt(x, y, (event.timeStamp - params.begin) / 1000)) invalidate();
89
+ }
90
+
91
+ /*
92
+ * Swap only the image texture when its URL changes; a failed image leaves base-colored dots.
93
+ */
94
+ function loadImage() {
95
+ const url = params.image?.src;
96
+ if (url === overlayUrl) return;
97
+ overlayUrl = url;
98
+ overlayFailed = false;
99
+ overlay?.dispose();
100
+ overlay = null;
101
+ grid.setOverlay(null);
102
+ if (!url) return;
103
+ const texture = createImageTexture(gl, {
104
+ url,
105
+ signal,
106
+ onLoad: () => {
107
+ if (texture !== overlay) return;
108
+ grid.setOverlay(texture);
109
+ invalidate();
110
+ },
111
+ onError: () => {
112
+ if (texture !== overlay) return;
113
+ overlayFailed = true;
114
+ invalidate();
115
+ },
116
+ });
117
+ overlay = texture;
118
+ grid.setOverlay(texture);
119
+ }
120
+
121
+ /*
122
+ * Let the browser resolve theme variables in the board's CSS context before uploading RGB.
123
+ */
124
+ function configureColors() {
125
+ const fade = params.fadeDotsBottom;
126
+ const blend = getDotsBoardBlendMode(fade);
127
+ gl.canvas.style.color = fade?.color ?? '';
128
+ const color: [number, number, number, number] | null =
129
+ fade && blend ? getDotsBoardBlendColor(fade) : fade ? cssColorToRgba(getComputedStyle(gl.canvas).color) : null;
130
+ grid.setBottomTint(fade?.start ?? 0, fade?.end ?? 0, color, blend ? (params.blend ? 'blend' : 'normal') : 'color');
131
+ gl.canvas.style.color = params.hover?.color ?? '';
132
+ const hoverColor = params.hover ? cssColorToRgb(getComputedStyle(gl.canvas).color) : null;
133
+ grid.setHover(hoverColor, params.hover?.radius, params.hover?.fade);
134
+ }
135
+
136
+ /*
137
+ * Update logical geometry independently of the canvas window and its device pixel ratio.
138
+ */
139
+ function resizeGrid() {
140
+ grid.resize({ width: Math.max(1, params.width), height: Math.max(1, params.height), pixelRatio: surface.pixelRatio });
141
+ grid.setViewport(surface.width, surface.height, params.view.top);
142
+ grid.setOverlayRegion(
143
+ params.imageWidth,
144
+ params.image?.height ?? 0,
145
+ params.imageOffset.left,
146
+ params.imageOffset.top,
147
+ params.image?.fade ?? 0,
148
+ );
149
+ }
150
+
151
+ const scene: Scene<DotsBoardSceneParams> = {
152
+ frameloop: 'always',
153
+ followsPointer: grid.isInteractive() || !!params.hover,
154
+ pointerSmoothing: Number.POSITIVE_INFINITY,
155
+ hoverSmoothing: Number.POSITIVE_INFINITY,
156
+ /*
157
+ * Keep the static image visible until its GPU texture is available or has failed.
158
+ */
159
+ isReady: () => !overlay || overlay.loaded || overlayFailed,
160
+ /*
161
+ * Resize the canvas projection without discarding logical state on ordinary scrolls.
162
+ */
163
+ resize(next) {
164
+ surface = next;
165
+ resizeGrid();
166
+ },
167
+ /*
168
+ * Advance the intro, spring simulation and shared effect clock for this layer.
169
+ */
170
+ update({ pointer, delta, reducedMotion }) {
171
+ const now = clock();
172
+ const intro = params.intro;
173
+ const end = introEnd();
174
+ const revealing = !reducedMotion && now < end;
175
+ grid.setTime(now, reducedMotion);
176
+ grid.setPulseStart(end);
177
+ grid.setViewport(surface.width, surface.height, params.view.top);
178
+ if (intro && revealing) {
179
+ const progress = (now * 1000 - Math.max(0, intro.delay ?? 0)) / Math.max(1, intro.duration ?? 1000);
180
+ grid.setReveal(
181
+ 1,
182
+ getDotAppearSize(
183
+ progress,
184
+ params.dots.size,
185
+ Math.max(params.dots.size, intro.peakSize ?? params.dots.size),
186
+ Math.max(0, intro.initialSize ?? 0),
187
+ ),
188
+ );
189
+ } else {
190
+ grid.setReveal(1);
191
+ }
192
+ // Read the host rather than cached canvas UVs: scrolling moves a layer beneath a still cursor.
193
+ const rect = params.hostRef.current?.getBoundingClientRect();
194
+ const radius = Math.max(params.kick?.radius ?? 0, params.hover ? (params.hover.radius ?? 175) : 0);
195
+ const x = rect && rect.width > 0 ? ((pointer.clientX - rect.left) * params.width) / rect.width : -1e6;
196
+ const y = rect && rect.height > 0 ? ((pointer.clientY - rect.top) * params.height) / rect.height : -1e6;
197
+ const active =
198
+ pointer.active &&
199
+ pointer.present &&
200
+ !revealing &&
201
+ !reducedMotion &&
202
+ x >= -radius &&
203
+ x <= params.width + radius &&
204
+ y >= -radius &&
205
+ y <= params.height + radius;
206
+ const hoverTarget = active && params.hover ? 1 : 0;
207
+ hoverAmount = reducedMotion ? 0 : hoverAmount + (hoverTarget - hoverAmount) * (delta > 0 ? 1 - Math.exp(-delta / 0.06) : 1);
208
+ if (Math.abs(hoverTarget - hoverAmount) < 0.001) hoverAmount = hoverTarget;
209
+ grid.setHoverPointer(x, y, hoverAmount);
210
+ const moved = active && hadPointer && delta > 0;
211
+ pointerPx[0] = x;
212
+ pointerPx[1] = y;
213
+ velocity[0] = moved ? (pointer.clientX - previousClient[0]) / delta : 0;
214
+ velocity[1] = moved ? (pointer.clientY - previousClient[1]) / delta : 0;
215
+ previousClient[0] = pointer.clientX;
216
+ previousClient[1] = pointer.clientY;
217
+ hadPointer = active;
218
+ const settling = grid.step(pointerPx, velocity, active, delta, now);
219
+ const clicking = grid.stepClicks(now, reducedMotion);
220
+ scene.frameloop = revealing || grid.isAnimating() ? 'always' : 'demand';
221
+ scene.followsPointer = grid.isInteractive() || !!params.hover;
222
+ if (settling || clicking || hoverAmount !== hoverTarget) invalidate();
223
+ },
224
+ /*
225
+ * Clear the viewport window and draw its visible dots.
226
+ */
227
+ render() {
228
+ grid.draw(true);
229
+ },
230
+ /*
231
+ * Apply board changes without recreating the renderer or restarting its timeline.
232
+ */
233
+ setParams(next) {
234
+ params = next;
235
+ grid.setOptions(params);
236
+ resizeGrid();
237
+ loadImage();
238
+ configureColors();
239
+ invalidate();
240
+ },
241
+ };
242
+ loadImage();
243
+ configureColors();
244
+ return scene;
245
+ }