@jsb188/gfx 1.1.11 → 1.1.12

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 (56) hide show
  1. package/README.md +248 -54
  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/dotGrid.helpers.ts +53 -0
  12. package/libs/dotGrid.ts +480 -77
  13. package/libs/effects/dotEffects.ts +136 -0
  14. package/libs/effects/effectQuad.ts +8 -2
  15. package/libs/pointer.ts +6 -0
  16. package/libs/runtime.ts +6 -4
  17. package/libs/types.ts +8 -1
  18. package/main.ts +2 -0
  19. package/modules/Carousel.helpers.ts +234 -0
  20. package/modules/Carousel.hooks.ts +520 -0
  21. package/modules/Carousel.tsx +135 -29
  22. package/modules/Dots.tsx +269 -0
  23. package/modules/DotsBoard.helpers.ts +388 -0
  24. package/modules/DotsBoard.hooks.ts +144 -0
  25. package/modules/DotsBoard.strings.ts +649 -0
  26. package/modules/DotsBoard.types.ts +240 -0
  27. package/modules/DotsBoardCanvas.hooks.ts +49 -0
  28. package/modules/DotsBoardCanvas.tsx +190 -0
  29. package/modules/DotsBoardElements.tsx +330 -0
  30. package/modules/Gallery.helpers.ts +32 -0
  31. package/modules/Gallery.hooks.ts +149 -0
  32. package/modules/Gallery.tsx +194 -0
  33. package/modules/GfxCanvas.tsx +10 -2
  34. package/modules/KineticHeader.helpers.ts +44 -0
  35. package/modules/KineticHeader.hooks.ts +206 -0
  36. package/modules/KineticHeader.tsx +305 -0
  37. package/modules/KineticHeader.types.ts +56 -0
  38. package/modules/ScrollSteps.hooks.ts +138 -0
  39. package/modules/ScrollSteps.tsx +7 -52
  40. package/package.json +15 -5
  41. package/scenes/Carousel.ts +99 -18
  42. package/scenes/DotsBoard.helpers.ts +12 -0
  43. package/scenes/DotsBoard.ts +245 -0
  44. package/tsconfig.json +1 -1
  45. package/utils/align.ts +18 -0
  46. package/utils/color.ts +64 -2
  47. package/utils/dotEffectSpecs.ts +79 -0
  48. package/utils/dotGridSpecs.ts +304 -46
  49. package/utils/motion.ts +88 -0
  50. package/utils/random.ts +22 -0
  51. package/utils/strings.ts +116 -0
  52. package/modules/DotsArea.tsx +0 -120
  53. package/modules/VisionBoard.helpers.ts +0 -561
  54. package/modules/VisionBoard.tsx +0 -358
  55. package/modules/VisionBoard.types.ts +0 -88
  56. package/scenes/DotsArea.ts +0 -208
@@ -1,12 +1,28 @@
1
1
  /**
2
- * Dot grid options, their defaults, and a matching static SVG tile. This module has no
3
- * runtime dependency on OGL, so route bundles can import it to render a CSS fallback of the
4
- * same grid before (or without) the engine.
2
+ * Dot grid options, their defaults, a matching static SVG tile, and the CSS that draws the grid
3
+ * with it. This module has no runtime dependency on OGL, so route bundles can import it to
4
+ * render a CSS fallback of the same grid before (or without) the engine.
5
5
  */
6
6
 
7
- /** Shapes a dot can take; `size` is the shape's bounding diameter either way. */
8
- export const DOT_SHAPES = ['circle', 'rounded_square'] as const;
9
- export type DotShape = (typeof DOT_SHAPES)[number];
7
+ import type { CSSProperties } from 'react';
8
+ import { ALIGN_X, ALIGN_Y } from './align.ts';
9
+ import type { AlignX, AlignY } from './align.ts';
10
+ import { DOT_EFFECT_DEFAULTS, DOT_SHAPES, resolveDotEffectOptions } from './dotEffectSpecs.ts';
11
+ import type { DotEffectOptions, DotShape } from './dotEffectSpecs.ts';
12
+
13
+ export { DOT_SHAPES, DOT_EFFECTS } from './dotEffectSpecs.ts';
14
+ export type { DotEffect, DotEffectSelection, DotEffectOptions, DotShape } from './dotEffectSpecs.ts';
15
+
16
+ /** Where the grid is anchored along each axis, like the keywords of CSS `mask-position`. */
17
+ export const DOT_ALIGN_X = ALIGN_X;
18
+ export const DOT_ALIGN_Y = ALIGN_Y;
19
+ export type DotAlignX = AlignX;
20
+ export type DotAlignY = AlignY;
21
+ /** A dot center a number of CSS pixels in from one edge; `{ left }` / `{ top }` are the same as a bare number. */
22
+ export type DotAlignXOffset = { left: number } | { right: number };
23
+ export type DotAlignYOffset = { top: number } | { bottom: number };
24
+ /** Any anchor along either axis, as the grid helpers accept it. */
25
+ export type DotAlign = DotAlignX | DotAlignY | number | DotAlignXOffset | DotAlignYOffset;
10
26
 
11
27
  /** The grid at rest. */
12
28
  export interface DotStyle {
@@ -20,14 +36,20 @@ export interface DotStyle {
20
36
  color?: string;
21
37
  /** 0..1 opacity of the dots. */
22
38
  opacity?: number;
39
+ /** Horizontal anchor: `center` puts a column on the middle of the area; `left` hides the first column just past the left edge (its center one `size` out, so the first visible column is about a spacing in); `right` sets the last column flush against the right edge; a number (or `{ left }`) puts a column's center that many CSS pixels from the left edge, `{ right }` that many from the right edge. */
40
+ alignX?: DotAlignX | number | DotAlignXOffset;
41
+ /** Vertical anchor: `center` puts a row on the middle of the area; `top` hides the first row just past the top edge (its center one `size` up, so the first visible row is about a spacing down); `bottom` sets the last row flush against the bottom edge; a number (or `{ top }`) puts a row's center that many CSS pixels from the top edge, `{ bottom }` that many from the bottom edge. */
42
+ alignY?: DotAlignY | number | DotAlignYOffset;
23
43
  }
24
44
 
25
45
  /** How a moving cursor throws the dots, and how they come back. */
26
46
  export interface DotKick {
27
- /** Radius in CSS pixels around the cursor within which its movement kicks the dots. */
47
+ /** Radius in CSS pixels around the moving cursor within which dots are kicked. */
28
48
  radius?: number;
29
- /** A fast swipe throws the dots at its centre about `radius × strength` pixels; negative pulls them toward the cursor's path. */
49
+ /** A fast swipe throws dots about `radius × strength` pixels; negative pulls inward. */
30
50
  strength?: number;
51
+ /** Extra per-dot variation in strength and direction, from 0 (default) to 1; stable across hover frames. */
52
+ randomness?: number;
31
53
  /** Milliseconds for a thrown dot to settle back home. */
32
54
  bounceDuration?: number;
33
55
  /** 0..1 how springy the return is: 0 glides back with no overshoot, 1 rings for the whole duration. */
@@ -44,38 +66,59 @@ export interface ThrownDotStyle {
44
66
  scale?: number;
45
67
  }
46
68
 
47
- /** Ambient animation: every cycle a random handful of dots swell and shrink back. Starts only once any reveal has finished. */
48
- export interface DotPulse {
49
- /** About how many dots pulse per cycle. */
69
+ /** A random handful of dots animate each cycle, once the reveal finishes. Defaults to a simple swell. */
70
+ export interface DotPulse extends DotEffectOptions {
71
+ /** About how many dots animate per cycle. */
50
72
  count?: number;
51
73
  /** Milliseconds per cycle; a new random set is picked each cycle. */
52
74
  interval?: number;
53
- /** Milliseconds one dot takes to swell and return; capped at `interval`. */
54
- duration?: number;
55
- /** Size multiplier at the peak of a pulse. */
56
- scale?: number;
57
- /** 0..1 opacity at the peak of a pulse; defaults to the resting opacity. */
58
- opacity?: number;
59
- /** Shape a pulsing dot morphs into at its peak; defaults to `dots.shape`. */
60
- shape?: DotShape;
61
75
  }
62
76
 
63
- /** Intro: the dots start large enough to fill the whole area, then shrink to their size. */
77
+ /** A shared effect triggered at the click or tap position. */
78
+ export interface DotClick extends DotEffectOptions {
79
+ /** Ripple-click impulse strength, independent of cursor kicks; 0 (default) disables it, negative pulls inward. */
80
+ kickStrength?: number;
81
+ /** Radius in CSS pixels of the ripple-click impulse, independent of the visual ripple and cursor; defaults to 80. */
82
+ kickRadius?: number;
83
+ /** Extra per-click variation in each dot's impulse strength and direction, from 0 (default) to 1. */
84
+ kickRandomness?: number;
85
+ }
86
+
87
+ /** An effect across the resting grid, repeated without pointer interaction after the reveal. */
88
+ export interface DotRest extends DotEffectOptions {
89
+ /** Milliseconds between cycles; duration is capped at this interval. */
90
+ interval?: number;
91
+ /** Center of the area, or a new random point inside it each cycle. */
92
+ origin?: 'center' | 'random';
93
+ }
94
+
95
+ /** A fade along the bottom edge, this many CSS pixels high: to transparent, or with `color` (hex) to that flat color. */
96
+ export type DotFade = number | { height: number; color?: string };
97
+
98
+ /** Intro: shrink a filled area into dots, or grow dots from an empty background. */
64
99
  export interface DotReveal {
65
- /** Milliseconds the filled state holds before the dots start shrinking. */
100
+ /** `fade_out` (default) shrinks the filled image or color into dots; `appear` grows dots from `initialSize`. */
101
+ effect?: 'fade_out' | 'appear';
102
+ /** Starting diameter in CSS pixels for `appear`. Defaults to 0 (an empty background). */
103
+ initialSize?: number;
104
+ /** Peak diameter in CSS pixels for `appear`, then shrink to `dots.size`. Defaults to `dots.size`; smaller values are clamped to it. */
105
+ appearSize?: number;
106
+ /** Milliseconds the initial state holds before the dots animate. */
66
107
  delay?: number;
67
- /** Milliseconds the shrink takes. */
108
+ /** Milliseconds for the whole reveal, including growth and settling for `appear`. */
68
109
  duration?: number;
69
110
  /** Scale the overlay image starts at on mount (1.25 = 125%), easing to 1 over `zoomDuration`; 1 for no zoom. Runs independently of `delay`: the dots start shrinking when the delay is up whether or not the zoom has finished. Only with an image. */
70
111
  zoomOnMount?: number;
71
112
  /** Milliseconds the zoom to 100% takes. */
72
113
  zoomDuration?: number;
114
+ /** A bottom fade for `fade_out`'s filled state; it holds through the shrink and then hands over to the dots' fade. `appear` uses the dots' fade throughout. */
115
+ fadeBottom?: DotFade;
73
116
  }
74
117
 
75
118
  /** Resolved options are themselves valid options, so a scene can hand them straight to the grid. */
76
119
  export interface DotGridOptions {
77
120
  dots?: DotStyle;
78
- /** `false` for a static grid. */
121
+ /** `false` disables cursor kicks; click kicks have independent settings. */
79
122
  kick?: DotKick | false | null;
80
123
  /** `false` (or omitted) leaves thrown dots looking like resting ones. */
81
124
  thrown?: ThrownDotStyle | false | null;
@@ -83,28 +126,57 @@ export interface DotGridOptions {
83
126
  reveal?: DotReveal | boolean | null;
84
127
  /** `true` for the defaults; `false` (or omitted) for no pulsing. */
85
128
  pulse?: DotPulse | boolean | null;
129
+ /** `true` for the default ripple; `false` (or omitted) disables click effects. */
130
+ click?: DotClick | boolean | null;
131
+ /** Ambient effects across resting dots; omitted or false leaves the grid at rest. */
132
+ rest?: DotRest | boolean | null;
86
133
  }
87
134
 
88
135
  export interface ResolvedDotGridOptions {
89
136
  dots: Required<DotStyle>;
90
- /** Null when the grid is static. */
137
+ /** Null when cursor kicks are disabled. */
91
138
  kick: Required<DotKick> | null;
92
139
  /** Null when thrown dots keep the resting look. */
93
140
  thrown: (Required<Omit<ThrownDotStyle, 'color'>> & { color: string | null }) | null;
94
- /** Null when there is no intro. */
95
- reveal: Required<DotReveal> | null;
141
+ /** Null when there is no intro. The filled state's bottom fade is `fadeBottom` pixels high (0 for none), to `fadeColor` (null for transparent). */
142
+ reveal: (Required<Omit<DotReveal, 'fadeBottom'>> & { fadeBottom: number; fadeColor: string | null }) | null;
96
143
  /** Null when nothing pulses. */
97
144
  pulse: Required<DotPulse> | null;
145
+ /** Null when clicks have no effect. */
146
+ click: Required<DotClick> | null;
147
+ /** Null when there is no animation across the resting grid. */
148
+ rest: Required<DotRest> | null;
98
149
  }
99
150
 
100
151
  /** Matches the `.pattern_speckle` mask tile: a 7×7 tile with a centered 2.25px dot. */
101
152
  export const DOT_GRID_DEFAULTS = {
102
- dots: { size: 2.25, shape: 'circle' as DotShape, spacing: 7, color: '#ffffff', opacity: 1 },
103
- kick: { radius: 80, strength: 1, bounceDuration: 1200, bounciness: 0.5 },
104
- reveal: { delay: 1000, duration: 800, zoomOnMount: 1, zoomDuration: 1000 },
153
+ dots: {
154
+ size: 2.25,
155
+ shape: 'circle' as DotShape,
156
+ spacing: 7,
157
+ color: '#ffffff',
158
+ opacity: 1,
159
+ alignX: 'center' as DotAlignX | number | DotAlignXOffset,
160
+ alignY: 'center' as DotAlignY | number | DotAlignYOffset,
161
+ },
162
+ kick: { radius: 80, strength: 1, randomness: 0, bounceDuration: 1200, bounciness: 0.5 },
163
+ reveal: { effect: 'fade_out' as const, initialSize: 0, delay: 1000, duration: 800, zoomOnMount: 1, zoomDuration: 1000 },
105
164
  pulse: { count: 20, interval: 3000, duration: 1500, scale: 3 },
165
+ click: { ...DOT_EFFECT_DEFAULTS, kickStrength: 0, kickRadius: 80, kickRandomness: 0 },
166
+ rest: { interval: 6000, duration: 1800, scale: 2, origin: 'center' as const },
106
167
  };
107
168
 
169
+ /*
170
+ * A bottom fade's height in CSS pixels (0 without one) and the flat color it fades to (null
171
+ * for transparent).
172
+ */
173
+ export function resolveDotFade(fade: DotFade | undefined) {
174
+ return {
175
+ height: Math.max(0, (typeof fade === 'number' ? fade : fade?.height) ?? 0),
176
+ color: (typeof fade === 'object' && fade.color) || null,
177
+ };
178
+ }
179
+
108
180
  /*
109
181
  * Clamp a number into a range, falling back to a default when it is not given.
110
182
  */
@@ -112,32 +184,64 @@ function clampOption(value: number | undefined, fallback: number, min: number, m
112
184
  return Math.min(max, Math.max(min, value ?? fallback));
113
185
  }
114
186
 
187
+ /*
188
+ * The pixel offset of an `{ edge: px }` anchor when it names one of `edges` with a finite
189
+ * number, or null when it is anything else.
190
+ */
191
+ function getAlignOffset(value: unknown, edges: readonly string[]): { edge: string; offset: number } | null {
192
+ if (!value || typeof value !== 'object') return null;
193
+ for (const edge of edges) {
194
+ const offset = (value as Record<string, unknown>)[edge];
195
+ if (typeof offset === 'number' && Number.isFinite(offset)) return { edge, offset };
196
+ }
197
+ return null;
198
+ }
199
+
200
+ /*
201
+ * An alignment as given, when it is one of the keywords, a finite pixel offset, or an
202
+ * `{ edge: px }` offset from the axis's start or end edge; the default otherwise.
203
+ */
204
+ function resolveAlign<T extends string, O>(value: T | number | O | undefined, keywords: readonly T[], fallback: T | number | O) {
205
+ if (typeof value === 'number') return Number.isFinite(value) ? value : fallback;
206
+ if (getAlignOffset(value, keywords)) return value as O;
207
+ return typeof value === 'string' && keywords.includes(value as T) ? (value as T) : fallback;
208
+ }
209
+
115
210
  /*
116
211
  * Fill in defaults and clamp to values the renderer can lay out (a zero spacing would mean an
117
212
  * infinite grid). A kick with a non-positive radius or zero strength is off; the strength
118
213
  * keeps its sign so negative pulls dots toward the cursor.
119
214
  */
120
215
  export function resolveDotGridOptions(options: DotGridOptions = {}): ResolvedDotGridOptions {
121
- const { dots = {}, kick = {}, thrown = false, reveal = false, pulse = false } = options;
216
+ const { dots = {}, kick = {}, thrown = false, reveal = false, pulse = false, click = false, rest = false } = options;
122
217
  const shape = dots.shape && DOT_SHAPES.includes(dots.shape) ? dots.shape : DOT_GRID_DEFAULTS.dots.shape;
218
+ const revealOptions = reveal === true ? {} : reveal;
219
+ const revealFade = resolveDotFade(revealOptions ? revealOptions.fadeBottom : undefined);
123
220
  const pulseOptions = pulse === true ? {} : pulse;
221
+ const clickOptions = click === true ? {} : click;
222
+ const restOptions = rest === true ? {} : rest;
223
+ const restInterval = restOptions ? clampOption(restOptions.interval, DOT_GRID_DEFAULTS.rest.interval, 1) : 0;
124
224
  const pulseInterval = pulseOptions ? clampOption(pulseOptions.interval, DOT_GRID_DEFAULTS.pulse.interval, 1) : 0;
125
225
  const opacity = clampOption(dots.opacity, DOT_GRID_DEFAULTS.dots.opacity, 0, 1);
226
+ const dotSize = clampOption(dots.size, DOT_GRID_DEFAULTS.dots.size, 0);
126
227
  const kickRadius = kick ? clampOption(kick.radius, DOT_GRID_DEFAULTS.kick.radius, 0) : 0;
127
228
  const kickStrength = kick ? (kick.strength ?? DOT_GRID_DEFAULTS.kick.strength) : 0;
128
229
  return {
129
230
  dots: {
130
- size: clampOption(dots.size, DOT_GRID_DEFAULTS.dots.size, 0),
231
+ size: dotSize,
131
232
  shape,
132
233
  spacing: clampOption(dots.spacing, DOT_GRID_DEFAULTS.dots.spacing, 1),
133
234
  color: dots.color ?? DOT_GRID_DEFAULTS.dots.color,
134
235
  opacity,
236
+ alignX: resolveAlign(dots.alignX, DOT_ALIGN_X, DOT_GRID_DEFAULTS.dots.alignX),
237
+ alignY: resolveAlign(dots.alignY, DOT_ALIGN_Y, DOT_GRID_DEFAULTS.dots.alignY),
135
238
  },
136
239
  kick:
137
240
  kick && kickRadius > 0 && kickStrength !== 0
138
241
  ? {
139
242
  radius: kickRadius,
140
243
  strength: kickStrength,
244
+ randomness: clampOption(kick.randomness, DOT_GRID_DEFAULTS.kick.randomness, 0, 1),
141
245
  bounceDuration: clampOption(kick.bounceDuration, DOT_GRID_DEFAULTS.kick.bounceDuration, 50),
142
246
  bounciness: clampOption(kick.bounciness, DOT_GRID_DEFAULTS.kick.bounciness, 0, 1),
143
247
  }
@@ -149,22 +253,47 @@ export function resolveDotGridOptions(options: DotGridOptions = {}): ResolvedDot
149
253
  scale: clampOption(thrown.scale, 1, 0),
150
254
  }
151
255
  : null,
152
- reveal: reveal
256
+ reveal: revealOptions
153
257
  ? {
154
- delay: clampOption(reveal === true ? undefined : reveal.delay, DOT_GRID_DEFAULTS.reveal.delay, 0),
155
- duration: clampOption(reveal === true ? undefined : reveal.duration, DOT_GRID_DEFAULTS.reveal.duration, 1),
156
- zoomOnMount: clampOption(reveal === true ? undefined : reveal.zoomOnMount, DOT_GRID_DEFAULTS.reveal.zoomOnMount, 0.01),
157
- zoomDuration: clampOption(reveal === true ? undefined : reveal.zoomDuration, DOT_GRID_DEFAULTS.reveal.zoomDuration, 1),
258
+ effect: revealOptions.effect === 'appear' ? 'appear' : DOT_GRID_DEFAULTS.reveal.effect,
259
+ initialSize: clampOption(revealOptions.initialSize, DOT_GRID_DEFAULTS.reveal.initialSize, 0),
260
+ appearSize: clampOption(revealOptions.appearSize, dotSize, dotSize),
261
+ delay: clampOption(revealOptions.delay, DOT_GRID_DEFAULTS.reveal.delay, 0),
262
+ duration: clampOption(revealOptions.duration, DOT_GRID_DEFAULTS.reveal.duration, 1),
263
+ zoomOnMount: clampOption(revealOptions.zoomOnMount, DOT_GRID_DEFAULTS.reveal.zoomOnMount, 0.01),
264
+ zoomDuration: clampOption(revealOptions.zoomDuration, DOT_GRID_DEFAULTS.reveal.zoomDuration, 1),
265
+ fadeBottom: revealFade.height,
266
+ fadeColor: revealFade.color,
158
267
  }
159
268
  : null,
160
269
  pulse: pulseOptions
161
270
  ? {
271
+ ...resolveDotEffectOptions(pulseOptions, {
272
+ ...DOT_GRID_DEFAULTS.pulse,
273
+ effect: 'swell',
274
+ shape,
275
+ opacity,
276
+ radius: (dots.spacing ?? DOT_GRID_DEFAULTS.dots.spacing) * 3,
277
+ }),
162
278
  count: clampOption(pulseOptions.count, DOT_GRID_DEFAULTS.pulse.count, 0),
163
279
  interval: pulseInterval,
164
280
  duration: clampOption(pulseOptions.duration, DOT_GRID_DEFAULTS.pulse.duration, 1, pulseInterval),
165
- scale: clampOption(pulseOptions.scale, DOT_GRID_DEFAULTS.pulse.scale, 0),
166
- opacity: clampOption(pulseOptions.opacity, opacity, 0, 1),
167
- shape: pulseOptions.shape && DOT_SHAPES.includes(pulseOptions.shape) ? pulseOptions.shape : shape,
281
+ }
282
+ : null,
283
+ click: clickOptions
284
+ ? {
285
+ ...resolveDotEffectOptions(clickOptions, { shape, opacity }),
286
+ kickStrength: clampOption(clickOptions.kickStrength, DOT_GRID_DEFAULTS.click.kickStrength, Number.NEGATIVE_INFINITY),
287
+ kickRadius: clampOption(clickOptions.kickRadius, DOT_GRID_DEFAULTS.click.kickRadius, 0),
288
+ kickRandomness: clampOption(clickOptions.kickRandomness, DOT_GRID_DEFAULTS.click.kickRandomness, 0, 1),
289
+ }
290
+ : null,
291
+ rest: restOptions
292
+ ? {
293
+ ...resolveDotEffectOptions(restOptions, { ...DOT_GRID_DEFAULTS.rest, shape, opacity }),
294
+ interval: restInterval,
295
+ duration: clampOption(restOptions.duration, DOT_GRID_DEFAULTS.rest.duration, 1, restInterval),
296
+ origin: restOptions.origin === 'random' ? 'random' : DOT_GRID_DEFAULTS.rest.origin,
168
297
  }
169
298
  : null,
170
299
  };
@@ -180,13 +309,16 @@ export function isDotGridInteractive(options: ResolvedDotGridOptions) {
180
309
  /** Corner radius of a rounded square as a fraction of its half-size. */
181
310
  export const DOT_ROUNDED_SQUARE_CORNER = 0.5;
182
311
 
312
+ /** Ring hole radius as a fraction of the dot's outer radius, shared by SVG and WebGL. */
313
+ export const DOT_RING_INNER_RADIUS = 0.6;
314
+
183
315
  /*
184
316
  * Dot diameter in CSS pixels at which a square grid leaves no pixel uncovered, plus a little
185
317
  * for the anti-aliased rim. A circle must reach the cell corners, √2 × spacing away; a rounded
186
318
  * square only needs to span the cell plus what its rounded corners cut away.
187
319
  */
188
320
  export function getDotGridCoverSize(spacing: number, shape: DotShape) {
189
- if (shape === 'rounded_square') {
321
+ if (shape === 'rounded_square' || shape === 'ring_rectangle') {
190
322
  // Half-size h covers the corner (spacing/2, spacing/2) when (spacing/2 - h + c)·√2 ≤ c, c = corner·h.
191
323
  const corner = DOT_ROUNDED_SQUARE_CORNER;
192
324
  return spacing / (1 - corner * (1 - Math.SQRT1_2)) + 1;
@@ -194,18 +326,144 @@ export function getDotGridCoverSize(spacing: number, shape: DotShape) {
194
326
  return spacing * Math.SQRT2 + 1;
195
327
  }
196
328
 
329
+ /*
330
+ * Whether an anchor is measured from the axis's end edge (`right` / `bottom`, as a keyword or
331
+ * an `{ edge: px }` offset).
332
+ */
333
+ function isEndAlign(align: DotAlign): boolean {
334
+ return align === 'right' || align === 'bottom' || getAlignOffset(align, ['right', 'bottom']) !== null;
335
+ }
336
+
337
+ /*
338
+ * The coordinate along one axis (CSS pixels from the area's start edge) of a dot center the
339
+ * grid passes through, for an area `extent` long: the middle for `center`; one `size` before
340
+ * the start edge for `left` / `top`, so that dot is hidden and the next one sits about a
341
+ * spacing in; half a `size` before the end edge for `right` / `bottom`, so that dot sits flush
342
+ * against it; a number (or `{ left }` / `{ top }`) as it is; `{ right }` / `{ bottom }` that
343
+ * many pixels before the end edge. The other dots follow at `spacing` steps either way.
344
+ */
345
+ export function getDotGridAnchor(align: DotAlign, size: number, extent: number) {
346
+ if (typeof align === 'number') return align;
347
+ const offset = getAlignOffset(align, ['left', 'top', 'right', 'bottom']);
348
+ if (offset) return isEndAlign(align) ? extent - offset.offset : offset.offset;
349
+ if (align === 'left' || align === 'top') return -size;
350
+ if (align === 'right' || align === 'bottom') return extent - size / 2;
351
+ return extent / 2;
352
+ }
353
+
354
+ /*
355
+ * The dot center nearest to `coordinate` along one axis, on the grid `getDotGridAnchor`
356
+ * anchors for an area `extent` long.
357
+ */
358
+ export function getNearestDotGridCenter(coordinate: number, align: DotAlign, size: number, spacing: number, extent: number) {
359
+ const anchor = getDotGridAnchor(align, size, extent);
360
+ return anchor + Math.round((coordinate - anchor) / spacing) * spacing;
361
+ }
362
+
363
+ /*
364
+ * The CSS `mask-position` (or `background-position`) that lays `makeDotGridTileUrl`'s tile,
365
+ * repeated at `spacing × spacing`, on the same dots as `getDotGridAnchor` would for either
366
+ * axis, without knowing the area's size: the tile's dot is centered, so `center` is exact, a
367
+ * start-edge or pixel anchor puts the first tile half a spacing before the anchor, and an
368
+ * end-edge anchor moves the last tile past the end edge by half a spacing less its anchor's
369
+ * distance from that edge (half a dot for the keyword, the given pixels for `{ right }` /
370
+ * `{ bottom }`).
371
+ */
372
+ export function getDotGridMaskPosition({ size, spacing, alignX, alignY }: Required<DotStyle>) {
373
+ const axis = (align: DotAlign) => {
374
+ if (align === 'center') return 'center';
375
+ // A tile position of 100% puts the last tile's end on the end edge, so its dot center
376
+ // sits half a spacing before it; the anchor (negative, relative to the edge) is the rest.
377
+ if (isEndAlign(align)) return `calc(100% + ${spacing / 2 + getDotGridAnchor(align, size, 0)}px)`;
378
+ return `${getDotGridAnchor(align, size, 0) - spacing / 2}px`;
379
+ };
380
+ return `${axis(alignX)} ${axis(alignY)}`;
381
+ }
382
+
197
383
  /*
198
384
  * One tile of the grid as an SVG data URL, for `mask-image` or `background-image`. Repeat
199
- * it at `spacing × spacing` with `center` positioning and it lines up exactly with the WebGL
200
- * grid, which also centers a dot on the canvas.
385
+ * it at `spacing × spacing` positioned by `getDotGridMaskPosition` and it lines up exactly
386
+ * with the WebGL grid.
201
387
  */
202
388
  export function makeDotGridTileUrl({ size, shape, spacing }: Pick<Required<DotStyle>, 'size' | 'shape' | 'spacing'>) {
203
389
  const half = spacing / 2;
204
390
  const r = size / 2;
205
- const dot =
206
- shape === 'rounded_square'
207
- ? `<rect x="${half - r}" y="${half - r}" width="${size}" height="${size}" rx="${r * DOT_ROUNDED_SQUARE_CORNER}"/>`
208
- : `<circle cx="${half}" cy="${half}" r="${r}"/>`;
391
+ const stroke = r * (1 - DOT_RING_INNER_RADIUS);
392
+ let dot: string;
393
+ if (shape === 'rounded_square' || shape === 'ring_rectangle') {
394
+ const inset = shape === 'ring_rectangle' ? stroke / 2 : 0;
395
+ const outline = shape === 'ring_rectangle' ? ` fill="none" stroke="black" stroke-width="${stroke}"` : '';
396
+ dot = `<rect x="${half - r + inset}" y="${half - r + inset}" width="${size - inset * 2}" height="${size - inset * 2}" rx="${r * DOT_ROUNDED_SQUARE_CORNER - inset}"${outline}/>`;
397
+ } else if (shape === 'ring') {
398
+ dot = `<circle cx="${half}" cy="${half}" r="${r - stroke / 2}" fill="none" stroke="black" stroke-width="${stroke}"/>`;
399
+ } else {
400
+ dot = `<circle cx="${half}" cy="${half}" r="${r}"/>`;
401
+ }
209
402
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${spacing} ${spacing}">${dot}</svg>`;
210
403
  return `data:image/svg+xml,${encodeURIComponent(svg)}`;
211
404
  }
405
+
406
+ /*
407
+ * A mask that fades an element to transparent over the bottom `fadeBottom` pixels, for a
408
+ * static stand-in that must match the canvas's bottom fade. To fade to a color instead, give
409
+ * the element's parent that color as its background.
410
+ */
411
+ export function getBottomFadeMaskStyle(fadeBottom: number): CSSProperties | undefined {
412
+ if (fadeBottom <= 0) return undefined;
413
+ return { maskImage: `linear-gradient(to bottom, #000 calc(100% - ${fadeBottom}px), transparent)` };
414
+ }
415
+
416
+ /*
417
+ * CSS that draws a resting dot grid with no WebGL: the element's background masked by the
418
+ * grid's SVG tile, repeated at the dot spacing and anchored per `alignX` / `alignY` so it lines
419
+ * up with the canvas grid. `fadeBottom` fades the dots over that many pixels along the bottom
420
+ * edge: to transparent through a second mask layer, or to `fadeColor` through a background
421
+ * gradient over the element's own background (which an image placeholder covers, so that one
422
+ * only fades to transparent). Set the color as the element's background separately, since an
423
+ * image may be what shows through the mask.
424
+ */
425
+ export function getDotGridMaskStyle(dots: Required<DotStyle>, fadeBottom = 0, fadeColor: string | null = null): CSSProperties {
426
+ const position = getDotGridMaskPosition(dots);
427
+ if (fadeBottom > 0 && fadeColor) {
428
+ return {
429
+ opacity: dots.opacity,
430
+ backgroundImage: `linear-gradient(to bottom, transparent calc(100% - ${fadeBottom}px), ${fadeColor})`,
431
+ maskImage: `url("${makeDotGridTileUrl(dots)}")`,
432
+ maskRepeat: 'repeat',
433
+ maskSize: `${dots.spacing}px ${dots.spacing}px`,
434
+ maskPosition: position,
435
+ };
436
+ }
437
+ const fadeMask = fadeBottom > 0 ? `, linear-gradient(to bottom, #000 calc(100% - ${fadeBottom}px), transparent)` : '';
438
+ return {
439
+ opacity: dots.opacity,
440
+ maskImage: `url("${makeDotGridTileUrl(dots)}")${fadeMask}`,
441
+ maskRepeat: fadeMask ? 'repeat, no-repeat' : 'repeat',
442
+ maskSize: `${dots.spacing}px ${dots.spacing}px${fadeMask ? ', 100% 100%' : ''}`,
443
+ maskPosition: fadeMask ? `${position}, center` : position,
444
+ maskComposite: fadeMask ? 'intersect' : undefined,
445
+ };
446
+ }
447
+
448
+ /*
449
+ * The same grid for a layer whose top-left corner rests `offset` pixels into an area
450
+ * `width` × `height` (a `<DotsBoard.Sticky>` inside its board): the area's anchor dot, taken
451
+ * into the layer's pixels, positions the tile so the layer's dots fall exactly on the area's.
452
+ * Numeric, so it needs the area measured; until then it is `getDotGridMaskStyle`, which can be
453
+ * off by up to half a spacing.
454
+ */
455
+ export function getDotGridLayerMaskStyle(
456
+ dots: Required<DotStyle>,
457
+ offset: { left: number; top: number },
458
+ width: number,
459
+ height: number,
460
+ ): CSSProperties {
461
+ if (!width || !height) return getDotGridMaskStyle(dots);
462
+ const { size, spacing, alignX, alignY } = dots;
463
+ // The tile's dot is at its center, so the tile starts half a spacing before any dot center;
464
+ // wrapped into the first tile so the position stays small.
465
+ const tileStart = (anchor: number) => ((((anchor - spacing / 2) % spacing) + spacing) % spacing) - spacing;
466
+ const x = tileStart(getDotGridAnchor(alignX, size, width) - offset.left);
467
+ const y = tileStart(getDotGridAnchor(alignY, size, height) - offset.top);
468
+ return { ...getDotGridMaskStyle(dots), maskPosition: `${x}px ${y}px` };
469
+ }
@@ -0,0 +1,88 @@
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
+ }
@@ -0,0 +1,22 @@
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
+ }