@jsb188/gfx 1.1.2 → 1.1.11
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/README.md +54 -248
- package/libs/dotGrid.ts +77 -480
- package/libs/effects/effectQuad.ts +2 -8
- package/libs/pointer.ts +0 -6
- package/libs/runtime.ts +4 -6
- package/libs/types.ts +1 -8
- package/main.ts +0 -2
- package/modules/Carousel.helpers.ts +0 -234
- package/modules/Carousel.tsx +29 -135
- package/modules/DotsArea.tsx +120 -0
- package/modules/GfxCanvas.tsx +2 -10
- package/modules/ScrollSteps.tsx +52 -7
- package/modules/VisionBoard.helpers.ts +561 -0
- package/modules/VisionBoard.tsx +358 -0
- package/modules/VisionBoard.types.ts +88 -0
- package/package.json +5 -15
- package/scenes/Carousel.ts +18 -99
- package/scenes/DotsArea.ts +208 -0
- package/tsconfig.json +1 -1
- package/utils/color.ts +2 -64
- package/utils/dotGridSpecs.ts +46 -304
- package/elements/Shapes.helpers.ts +0 -204
- package/elements/Shapes.hooks.ts +0 -171
- package/elements/Shapes.tsx +0 -505
- package/elements/Shapes.types.ts +0 -81
- package/elements/Stack.helpers.ts +0 -42
- package/elements/Stack.tsx +0 -143
- package/elements/Text.helpers.ts +0 -45
- package/elements/Text.hooks.ts +0 -197
- package/elements/Text.tsx +0 -98
- package/libs/dotGrid.helpers.ts +0 -53
- package/libs/effects/dotEffects.ts +0 -136
- package/modules/Carousel.hooks.ts +0 -520
- package/modules/Dots.tsx +0 -269
- package/modules/DotsBoard.helpers.ts +0 -388
- package/modules/DotsBoard.hooks.ts +0 -144
- package/modules/DotsBoard.strings.ts +0 -649
- package/modules/DotsBoard.types.ts +0 -240
- package/modules/DotsBoardCanvas.hooks.ts +0 -49
- package/modules/DotsBoardCanvas.tsx +0 -190
- package/modules/DotsBoardElements.tsx +0 -330
- package/modules/Gallery.helpers.ts +0 -32
- package/modules/Gallery.hooks.ts +0 -149
- package/modules/Gallery.tsx +0 -194
- package/modules/KineticHeader.helpers.ts +0 -44
- package/modules/KineticHeader.hooks.ts +0 -206
- package/modules/KineticHeader.tsx +0 -305
- package/modules/KineticHeader.types.ts +0 -56
- package/modules/ScrollSteps.hooks.ts +0 -138
- package/scenes/DotsBoard.helpers.ts +0 -12
- package/scenes/DotsBoard.ts +0 -245
- package/utils/align.ts +0 -18
- package/utils/dotEffectSpecs.ts +0 -79
- package/utils/motion.ts +0 -88
- package/utils/random.ts +0 -22
- package/utils/strings.ts +0 -116
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import type { RefObject } from 'react';
|
|
2
|
-
import { useEffect, useRef, useState } from 'react';
|
|
3
|
-
|
|
4
|
-
/*
|
|
5
|
-
* Whether an element is in the viewport (any pixel of it, or `threshold` of it, within
|
|
6
|
-
* `rootMargin` of the edges). With `once` it stays true after the first sighting, for a
|
|
7
|
-
* reveal that should not undo itself when the visitor scrolls back. False until the first
|
|
8
|
-
* observation, so on the server and before hydration nothing counts as seen; where the
|
|
9
|
-
* browser has no `IntersectionObserver` everything counts as seen at once. `disabled` keeps
|
|
10
|
-
* the element unobserved (and the answer false), for a caller that only sometimes needs it.
|
|
11
|
-
*/
|
|
12
|
-
export function useInView(
|
|
13
|
-
ref: RefObject<Element | null>,
|
|
14
|
-
{
|
|
15
|
-
once = false,
|
|
16
|
-
rootMargin,
|
|
17
|
-
threshold = 0,
|
|
18
|
-
disabled = false,
|
|
19
|
-
}: { once?: boolean; rootMargin?: string; threshold?: number; disabled?: boolean } = {},
|
|
20
|
-
) {
|
|
21
|
-
const [inView, setInView] = useState(false);
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
const element = ref.current;
|
|
25
|
-
if (!element || disabled) return;
|
|
26
|
-
if (typeof IntersectionObserver === 'undefined') {
|
|
27
|
-
setInView(true);
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
const observer = new IntersectionObserver(
|
|
31
|
-
([entry]) => {
|
|
32
|
-
// Initial observations can intersect without meeting the requested visible fraction.
|
|
33
|
-
if (entry.isIntersecting && entry.intersectionRatio >= threshold) {
|
|
34
|
-
setInView(true);
|
|
35
|
-
if (once) observer.disconnect();
|
|
36
|
-
} else if (!once) setInView(false);
|
|
37
|
-
},
|
|
38
|
-
{ rootMargin, threshold },
|
|
39
|
-
);
|
|
40
|
-
observer.observe(element);
|
|
41
|
-
return () => observer.disconnect();
|
|
42
|
-
}, [ref, once, rootMargin, threshold, disabled]);
|
|
43
|
-
|
|
44
|
-
return inView;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Constants
|
|
49
|
-
*/
|
|
50
|
-
|
|
51
|
-
/** Start listening to scroll a little before the track enters the viewport, and keep listening a little after. */
|
|
52
|
-
const SCROLL_ROOT_MARGIN = '25% 0px';
|
|
53
|
-
|
|
54
|
-
/*
|
|
55
|
-
* The step a scrolled element is at: how far its top has scrolled past the `top` line, as a
|
|
56
|
-
* share of `count × stepHeight` pixels, floored into `0..count - 1`; or, with `stepHeights`,
|
|
57
|
-
* each step its own number of pixels long, in order, `count` and `stepHeight` then being
|
|
58
|
-
* ignored. `onProgress` gets the continuous 0..1 position on every scroll frame (without a
|
|
59
|
-
* re-render) for anything that should move with the page. The scroll listener is only
|
|
60
|
-
* attached while the element is near the viewport, and reads one layout rectangle per frame.
|
|
61
|
-
*/
|
|
62
|
-
export function useScrollStep(
|
|
63
|
-
ref: RefObject<HTMLElement | null>,
|
|
64
|
-
{
|
|
65
|
-
count,
|
|
66
|
-
stepHeight = 500,
|
|
67
|
-
stepHeights,
|
|
68
|
-
top = 0,
|
|
69
|
-
onProgress,
|
|
70
|
-
}: { count: number; stepHeight?: number; stepHeights?: number[]; top?: number; onProgress?: (progress: number) => void },
|
|
71
|
-
) {
|
|
72
|
-
const onProgressRef = useRef(onProgress);
|
|
73
|
-
const [step, setStep] = useState(0);
|
|
74
|
-
// As one string, so a fresh array of the same heights does not restart the listener.
|
|
75
|
-
const heightsKey = stepHeights?.join(',');
|
|
76
|
-
|
|
77
|
-
useEffect(() => {
|
|
78
|
-
onProgressRef.current = onProgress;
|
|
79
|
-
}, [onProgress]);
|
|
80
|
-
|
|
81
|
-
// `stepHeights` is read through `heightsKey`, so a fresh array of the same values changes nothing.
|
|
82
|
-
useEffect(() => {
|
|
83
|
-
const element = ref.current;
|
|
84
|
-
if (!element) return;
|
|
85
|
-
// Where each step ends, in pixels from the start of the track.
|
|
86
|
-
const ends = getScrollStepEnds(heightsKey ? heightsKey.split(',').map(Number) : Array.from({ length: count }, () => stepHeight));
|
|
87
|
-
if (ends.length < 1) return;
|
|
88
|
-
let rafId = 0;
|
|
89
|
-
let listening = false;
|
|
90
|
-
const distance = ends[ends.length - 1];
|
|
91
|
-
|
|
92
|
-
const measure = () => {
|
|
93
|
-
rafId = 0;
|
|
94
|
-
const scrolled = top - element.getBoundingClientRect().top;
|
|
95
|
-
// Steps with no length yet (a layer not measured) are done as soon as the line is reached.
|
|
96
|
-
const progress = distance ? Math.min(1, Math.max(0, scrolled / distance)) : Number(scrolled >= 0);
|
|
97
|
-
// The first step not yet scrolled past; the last one once all are.
|
|
98
|
-
const at = ends.findIndex((end) => scrolled < end);
|
|
99
|
-
setStep(at === -1 ? ends.length - 1 : at);
|
|
100
|
-
onProgressRef.current?.(progress);
|
|
101
|
-
};
|
|
102
|
-
const schedule = () => {
|
|
103
|
-
if (!rafId) rafId = requestAnimationFrame(measure);
|
|
104
|
-
};
|
|
105
|
-
const listen = (on: boolean) => {
|
|
106
|
-
if (on === listening) return;
|
|
107
|
-
listening = on;
|
|
108
|
-
if (on) {
|
|
109
|
-
window.addEventListener('scroll', schedule, { passive: true });
|
|
110
|
-
window.addEventListener('resize', schedule);
|
|
111
|
-
schedule();
|
|
112
|
-
} else {
|
|
113
|
-
window.removeEventListener('scroll', schedule);
|
|
114
|
-
window.removeEventListener('resize', schedule);
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
const observer = new IntersectionObserver(([entry]) => listen(entry.isIntersecting), { rootMargin: SCROLL_ROOT_MARGIN });
|
|
118
|
-
observer.observe(element);
|
|
119
|
-
return () => {
|
|
120
|
-
observer.disconnect();
|
|
121
|
-
listen(false);
|
|
122
|
-
if (rafId) cancelAnimationFrame(rafId);
|
|
123
|
-
};
|
|
124
|
-
}, [ref, count, stepHeight, heightsKey, top]);
|
|
125
|
-
|
|
126
|
-
return step;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/*
|
|
130
|
-
* The running total of step heights: where each step ends along the track.
|
|
131
|
-
*/
|
|
132
|
-
function getScrollStepEnds(heights: number[]) {
|
|
133
|
-
let total = 0;
|
|
134
|
-
return heights.map((height) => {
|
|
135
|
-
total += Math.max(0, height);
|
|
136
|
-
return total;
|
|
137
|
-
});
|
|
138
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
}
|
package/scenes/DotsBoard.ts
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
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
|
-
}
|
package/utils/align.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Edge keywords for anchoring things along an axis, shared by the dot grid, `<Stack>` and
|
|
3
|
-
* `<DotsBoard.Item>`.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export const ALIGN_X = ['left', 'center', 'right'] as const;
|
|
7
|
-
export const ALIGN_Y = ['top', 'center', 'bottom'] as const;
|
|
8
|
-
export type AlignX = (typeof ALIGN_X)[number];
|
|
9
|
-
export type AlignY = (typeof ALIGN_Y)[number];
|
|
10
|
-
|
|
11
|
-
/** CSS box alignment (`justify-content` / `align-items`) for each edge keyword. */
|
|
12
|
-
export const ALIGN_TO_FLEX: Record<AlignX | AlignY, string> = {
|
|
13
|
-
left: 'flex-start',
|
|
14
|
-
top: 'flex-start',
|
|
15
|
-
center: 'center',
|
|
16
|
-
right: 'flex-end',
|
|
17
|
-
bottom: 'flex-end',
|
|
18
|
-
};
|
package/utils/dotEffectSpecs.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/** Shared shapes and effect options for click, pulse, and resting dot animations; no OGL. */
|
|
2
|
-
export const DOT_SHAPES = ['circle', 'rounded_square', 'ring', 'ring_rectangle'] as const;
|
|
3
|
-
export type DotShape = (typeof DOT_SHAPES)[number];
|
|
4
|
-
|
|
5
|
-
/** Shared ordering for effect selection and matching shader IDs. */
|
|
6
|
-
export const DOT_EFFECTS = ['swell', 'ripple', 'vortex', 'magnet', 'color_wave', 'sparkle'] as const;
|
|
7
|
-
export type DotEffect = (typeof DOT_EFFECTS)[number];
|
|
8
|
-
export type DotEffectSelection = DotEffect | 'random';
|
|
9
|
-
|
|
10
|
-
/** Appearance and motion shared by every dot animation trigger. */
|
|
11
|
-
export interface DotEffectOptions {
|
|
12
|
-
/** Animation to run. Random picks once per click, pulse, or resting cycle. */
|
|
13
|
-
effect?: DotEffectSelection;
|
|
14
|
-
/** Shape at the animation's peak; defaults to the resting dot's shape. */
|
|
15
|
-
shape?: DotShape;
|
|
16
|
-
/** Maximum reach in CSS pixels from the effect's origin. */
|
|
17
|
-
radius?: number;
|
|
18
|
-
/** Half-width in CSS pixels of a ripple or color wave. */
|
|
19
|
-
width?: number;
|
|
20
|
-
/** Milliseconds for the effect to finish and return to normal. */
|
|
21
|
-
duration?: number;
|
|
22
|
-
/** Movement strength, 0..1, for vortex and magnet. */
|
|
23
|
-
strength?: number;
|
|
24
|
-
/** Dot size multiplier at the animation's peak. */
|
|
25
|
-
scale?: number;
|
|
26
|
-
/** 0..1 opacity at the peak; defaults to the resting dot's opacity. */
|
|
27
|
-
opacity?: number;
|
|
28
|
-
/** Optional hex tint, including over images. Color wave and sparkle use a seeded palette when omitted. */
|
|
29
|
-
color?: string | null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type ResolvedDotEffectOptions = Required<DotEffectOptions>;
|
|
33
|
-
|
|
34
|
-
export const DOT_EFFECT_DEFAULTS: ResolvedDotEffectOptions = {
|
|
35
|
-
effect: 'ripple',
|
|
36
|
-
shape: 'circle',
|
|
37
|
-
radius: 420,
|
|
38
|
-
width: 80,
|
|
39
|
-
duration: 1400,
|
|
40
|
-
strength: 0.35,
|
|
41
|
-
scale: 4,
|
|
42
|
-
opacity: 1,
|
|
43
|
-
color: null,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/*
|
|
47
|
-
* Clamp a finite option; malformed values fall back instead of putting NaNs in GPU uniforms.
|
|
48
|
-
*/
|
|
49
|
-
function finiteOption(value: number | undefined, fallback: number, min: number, max = Number.POSITIVE_INFINITY) {
|
|
50
|
-
return Math.min(max, Math.max(min, typeof value === 'number' && Number.isFinite(value) ? value : fallback));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/*
|
|
54
|
-
* Normalize effect settings independently of their trigger, with trigger-specific defaults.
|
|
55
|
-
*/
|
|
56
|
-
export function resolveDotEffectOptions(
|
|
57
|
-
options: DotEffectOptions,
|
|
58
|
-
defaults: Partial<ResolvedDotEffectOptions> = {},
|
|
59
|
-
): ResolvedDotEffectOptions {
|
|
60
|
-
const fallback = { ...DOT_EFFECT_DEFAULTS, ...defaults };
|
|
61
|
-
return {
|
|
62
|
-
effect: options.effect === 'random' || DOT_EFFECTS.includes(options.effect as DotEffect) ? options.effect! : fallback.effect,
|
|
63
|
-
shape: DOT_SHAPES.includes(options.shape as DotShape) ? options.shape! : fallback.shape,
|
|
64
|
-
radius: finiteOption(options.radius, fallback.radius, 1),
|
|
65
|
-
width: finiteOption(options.width, fallback.width, 1),
|
|
66
|
-
duration: finiteOption(options.duration, fallback.duration, 1),
|
|
67
|
-
strength: finiteOption(options.strength, fallback.strength, 0, 1),
|
|
68
|
-
scale: finiteOption(options.scale, fallback.scale, 0),
|
|
69
|
-
opacity: finiteOption(options.opacity, fallback.opacity, 0, 1),
|
|
70
|
-
color: options.color ?? fallback.color,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/*
|
|
75
|
-
* Convert an effect name to its shader ID; -1 asks the shader to choose from a stable seed.
|
|
76
|
-
*/
|
|
77
|
-
export function getDotEffectId(effect: DotEffectSelection) {
|
|
78
|
-
return effect === 'random' ? -1 : DOT_EFFECTS.indexOf(effect);
|
|
79
|
-
}
|
package/utils/motion.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Motion helpers for the package's JavaScript-driven animations: the visitor's reduced-motion
|
|
3
|
-
* preference, and a damped spring integrator for any 2D body. No OGL, safe for route bundles.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Types
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export interface Spring {
|
|
11
|
-
/** Pull toward the rest position, per unit of distance. */
|
|
12
|
-
stiffness: number;
|
|
13
|
-
/** Resistance to velocity; below about `2 × √stiffness` the motion overshoots and bounces. */
|
|
14
|
-
damping: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** A point with velocity that a spring drives. */
|
|
18
|
-
export interface SpringBody {
|
|
19
|
-
x: number;
|
|
20
|
-
y: number;
|
|
21
|
-
vx: number;
|
|
22
|
-
vy: number;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Constants
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
export const SPRING_PHYSICS = {
|
|
30
|
-
/** Longest frame the springs integrate; a background tab resuming does not explode. */
|
|
31
|
-
maxDt: 1 / 30,
|
|
32
|
-
/** Springs integrate in sub-steps no longer than this, which keeps stiff settings stable. */
|
|
33
|
-
subStep: 1 / 120,
|
|
34
|
-
/** A body within this many units of its target, moving slower than `settleVelocity`, snaps to rest. */
|
|
35
|
-
settleDistance: 0.05,
|
|
36
|
-
settleVelocity: 1,
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Helpers
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
/*
|
|
44
|
-
* Whether the visitor asked for reduced motion; animations then swap or snap instead of moving.
|
|
45
|
-
*/
|
|
46
|
-
export function prefersReducedMotion() {
|
|
47
|
-
return typeof window !== 'undefined' && !!window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/*
|
|
51
|
-
* A body at rest on a point.
|
|
52
|
-
*/
|
|
53
|
-
export function springBodyAt(x: number, y: number): SpringBody {
|
|
54
|
-
return { x, y, vx: 0, vy: 0 };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/*
|
|
58
|
-
* Put a body at rest on its target, for reduced motion or a first placement.
|
|
59
|
-
*/
|
|
60
|
-
export function snapSpring(body: SpringBody, targetX: number, targetY: number) {
|
|
61
|
-
body.x = targetX;
|
|
62
|
-
body.y = targetY;
|
|
63
|
-
body.vx = 0;
|
|
64
|
-
body.vy = 0;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/*
|
|
68
|
-
* Advance a damped spring toward its target by `dt` seconds (semi-implicit Euler in sub-steps),
|
|
69
|
-
* mutating the body. Returns true once it has come to rest, having snapped it exactly onto the
|
|
70
|
-
* target so no residual keeps a frame loop alive.
|
|
71
|
-
*/
|
|
72
|
-
export function stepSpring(body: SpringBody, targetX: number, targetY: number, dt: number, spring: Spring) {
|
|
73
|
-
const steps = Math.max(1, Math.ceil(dt / SPRING_PHYSICS.subStep));
|
|
74
|
-
const h = dt / steps;
|
|
75
|
-
for (let i = 0; i < steps; i++) {
|
|
76
|
-
body.vx += (-spring.stiffness * (body.x - targetX) - spring.damping * body.vx) * h;
|
|
77
|
-
body.vy += (-spring.stiffness * (body.y - targetY) - spring.damping * body.vy) * h;
|
|
78
|
-
body.x += body.vx * h;
|
|
79
|
-
body.y += body.vy * h;
|
|
80
|
-
}
|
|
81
|
-
const settled =
|
|
82
|
-
Math.abs(body.x - targetX) < SPRING_PHYSICS.settleDistance &&
|
|
83
|
-
Math.abs(body.y - targetY) < SPRING_PHYSICS.settleDistance &&
|
|
84
|
-
Math.abs(body.vx) < SPRING_PHYSICS.settleVelocity &&
|
|
85
|
-
Math.abs(body.vy) < SPRING_PHYSICS.settleVelocity;
|
|
86
|
-
if (settled) snapSpring(body, targetX, targetY);
|
|
87
|
-
return settled;
|
|
88
|
-
}
|
package/utils/random.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Small deterministic PRNG (Park–Miller): a given seed always produces the same sequence,
|
|
3
|
-
* across renders and on the server, so seeded wobble never twitches or fails hydration.
|
|
4
|
-
*/
|
|
5
|
-
export function createRandom(seed: number): () => number {
|
|
6
|
-
let state = (Math.abs(Math.floor(seed)) % 2147483646) + 1;
|
|
7
|
-
const next = () => {
|
|
8
|
-
state = (state * 16807) % 2147483647;
|
|
9
|
-
return state / 2147483647;
|
|
10
|
-
};
|
|
11
|
-
// A small seed's first outputs are tiny (seed × 16807 / 2³¹), which would make every
|
|
12
|
-
// seeded sequence start at the bottom of its range; spin past them.
|
|
13
|
-
for (let i = 0; i < 3; i++) next();
|
|
14
|
-
return next;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/*
|
|
18
|
-
* Random value in `-amount..amount`.
|
|
19
|
-
*/
|
|
20
|
-
export function jitter(random: () => number, amount: number): number {
|
|
21
|
-
return (random() - 0.5) * 2 * amount;
|
|
22
|
-
}
|