@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
@@ -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
+ }
package/tsconfig.json CHANGED
@@ -9,5 +9,5 @@
9
9
  "strict": true,
10
10
  "esModuleInterop": true
11
11
  },
12
- "include": ["main.ts", "libs/**/*", "utils/**/*", "scenes/**/*", "modules/**/*"]
12
+ "include": ["main.ts", "libs/**/*", "utils/**/*", "scenes/**/*", "modules/**/*", "elements/**/*"]
13
13
  }
package/utils/align.ts ADDED
@@ -0,0 +1,18 @@
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/color.ts CHANGED
@@ -36,8 +36,7 @@ export function cssColorToRgb(value: string, fallback: RGB = [0, 0, 0]): RGB {
36
36
  if (!match) return fallback;
37
37
  const parts = match[2]
38
38
  .replace(/^srgb\s+/i, '')
39
- .replace(/\//g, ' ')
40
- .split(/[\s,]+/)
39
+ .split(/[\s,/]+/)
41
40
  .filter(Boolean);
42
41
  if (parts.length < 3) return fallback;
43
42
  // rgb() channels are 0..255 (or percentages); color(srgb) channels are already 0..1.
@@ -48,3 +47,66 @@ export function cssColorToRgb(value: string, fallback: RGB = [0, 0, 0]): RGB {
48
47
  if (channels.some((channel) => Number.isNaN(channel))) return fallback;
49
48
  return channels as RGB;
50
49
  }
50
+
51
+ /*
52
+ * Convert a 0..1 RGB triple to #rrggbb.
53
+ */
54
+ export function rgbToHex(color: RGB): string {
55
+ const channel = (value: number) =>
56
+ Math.round(Math.min(1, Math.max(0, value)) * 255)
57
+ .toString(16)
58
+ .padStart(2, '0');
59
+ return `#${channel(color[0])}${channel(color[1])}${channel(color[2])}`;
60
+ }
61
+
62
+ /*
63
+ * Convert a 0..1 RGB triple to HSL (hue in degrees, saturation and lightness 0..1).
64
+ */
65
+ export function rgbToHsl(color: RGB): [number, number, number] {
66
+ const [r, g, b] = color;
67
+ const max = Math.max(r, g, b);
68
+ const min = Math.min(r, g, b);
69
+ const l = (max + min) / 2;
70
+ if (max === min) return [0, 0, l];
71
+ const d = max - min;
72
+ const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
73
+ let h: number;
74
+ if (max === r) h = (g - b) / d + (g < b ? 6 : 0);
75
+ else if (max === g) h = (b - r) / d + 2;
76
+ else h = (r - g) / d + 4;
77
+ return [h * 60, s, l];
78
+ }
79
+
80
+ /*
81
+ * Convert HSL (hue in degrees, saturation and lightness 0..1) to a 0..1 RGB triple.
82
+ */
83
+ export function hslToRgb(h: number, s: number, l: number): RGB {
84
+ const k = (n: number) => (n + h / 30) % 12;
85
+ const a = s * Math.min(l, 1 - l);
86
+ const f = (n: number) => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
87
+ return [f(0), f(8), f(4)];
88
+ }
89
+
90
+ /*
91
+ * Shift a hex color's lightness by `delta` (-1..1) and optionally its hue by `hueDelta` degrees,
92
+ * returning #rrggbb. Used to derive gradient stops and ink tones from one base color.
93
+ */
94
+ export function shiftHex(hex: string, delta: number, hueDelta = 0): string {
95
+ const [h, s, l] = rgbToHsl(hexToRgb(hex));
96
+ return rgbToHex(hslToRgb((h + hueDelta + 360) % 360, s, Math.min(1, Math.max(0, l + delta))));
97
+ }
98
+
99
+ /*
100
+ * Preserve alpha when converting a browser-computed rgb/rgba or color(srgb) value for WebGL.
101
+ * Resolve named colors, hex and theme variables through getComputedStyle before calling.
102
+ */
103
+ export function cssColorToRgba(value: string): [number, number, number, number] {
104
+ const parts = value
105
+ .slice(value.indexOf('(') + 1, value.lastIndexOf(')'))
106
+ .replace(/^srgb\s+/i, '')
107
+ .split(/[\s,/]+/)
108
+ .filter(Boolean);
109
+ const alpha = parts[3];
110
+ const opacity = alpha === undefined ? 1 : Number.parseFloat(alpha) / (alpha.endsWith('%') ? 100 : 1);
111
+ return [...cssColorToRgb(value), Number.isFinite(opacity) ? Math.min(1, Math.max(0, opacity)) : 1];
112
+ }
@@ -0,0 +1,79 @@
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
+ }