@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,240 @@
1
+ import type { RefObject } from 'react';
2
+ import type { AlignX, AlignY } from '../utils/align.ts';
3
+ import type { DotStyle, DotGridOptions, ResolvedDotGridOptions } from '../utils/dotGridSpecs.ts';
4
+ import type { Point } from '../utils/strings.ts';
5
+ import type { Spring } from '../utils/motion.ts';
6
+ import type { DotsStringEngine } from './DotsBoard.strings.ts';
7
+
8
+ /**
9
+ * Props
10
+ */
11
+
12
+ /** Where an item goes along one axis of its stage: CSS pixels, a percentage of the stage, or its middle. */
13
+ export type DotsBoardCoordinate = number | 'center' | `${number}%`;
14
+
15
+ /** A string run through `<DotsBoard.Item>`s by id, in order; drawn as one sagging stretch per neighbouring pair. */
16
+ export interface DotsBoardConnection {
17
+ /** Identity of the string across changes: a connection whose key survives with different `items` keeps its string hanging and only re-pins it, instead of fading out and in. Defaults to the connection's index. */
18
+ key?: string;
19
+ /** Ids of the items the string runs through, in order; at least two. */
20
+ items: string[];
21
+ /** Overrides the board's `strings.color`. */
22
+ color?: string;
23
+ /** Overrides the board's `strings.width`. */
24
+ width?: number;
25
+ /** Overrides the board's `strings.slack`. */
26
+ slack?: number;
27
+ /** Overrides the board's `strings.maxSag`. */
28
+ maxSag?: number;
29
+ dashed?: boolean;
30
+ }
31
+
32
+ /** A connection with the board's default look is just its item ids. */
33
+ export type DotsBoardConnections = (DotsBoardConnection | string[])[];
34
+
35
+ /** How a board draws its strings. */
36
+ export interface DotsBoardStrings {
37
+ /** CSS stroke color. */
38
+ color?: string;
39
+ /** Stroke width in CSS pixels. */
40
+ width?: number;
41
+ /** Sag of each stretch at rest as a fraction of its length; 0 draws it straight. */
42
+ slack?: number;
43
+ /** Most a stretch sags at rest, in CSS pixels, however long it is; unset lets sag grow with length. */
44
+ maxSag?: number;
45
+ /** Spring on the strings' knots and bends, so a new string drops into its sag and a re-pinned one swings to its new dots; `false` snaps them into place. */
46
+ spring?: Partial<Spring> | false;
47
+ /** Milliseconds a string takes to fade in or out. */
48
+ fadeMs?: number;
49
+ /** Roughly how many pixels a pointer crossing a string pulls it sideways before it springs back; `false` for no tug. */
50
+ tug?: number | false;
51
+ /** Pixels a string can be dragged by the pointer before it slips from the grip and springs back; `false` makes strings not grabbable. */
52
+ pull?: number | false;
53
+ }
54
+
55
+ /** Image region anchored to the top of the board, independent of its full page height. */
56
+ export interface DotsBoardImage {
57
+ src: string;
58
+ height: number;
59
+ /** Pixels over which the image blends into dots.color at the end of the region. */
60
+ fade?: number;
61
+ }
62
+
63
+ /** Blend dots to a color or background blend in a band measured upward from the bottom. */
64
+ export type DotsBoardBottomFade = {
65
+ /** Distance from the bottom where normal dots start blending, in CSS pixels. */
66
+ start: number;
67
+ /** Distance from the bottom where the target appearance is fully reached and held below. */
68
+ end: number;
69
+ /** Independently fade all dots to transparent; distances are CSS pixels upward from the board bottom. */
70
+ fadeOut?: {
71
+ /** Distance where dots start disappearing. */
72
+ start: number;
73
+ /** Distance where dots are fully hidden, including throughout the area below. */
74
+ end: number;
75
+ };
76
+ } & (
77
+ | {
78
+ mode?: 'color';
79
+ /** Target CSS color, including alpha and theme variables; overrides hover and image tints. */
80
+ color: string;
81
+ opacity?: never;
82
+ }
83
+ | {
84
+ mode: 'invert' | 'soft-light' | 'overlay' | 'color-burn';
85
+ /** Strength of the background blend, from 0 to 1; defaults to 1. Color burn uses neutral gray; other modes use white. */
86
+ opacity?: number;
87
+ color?: never;
88
+ }
89
+ );
90
+
91
+ /** Shared geometry and appearance for the board's normal and blended canvas surfaces. */
92
+ export interface DotsBoardCanvasProps {
93
+ hostRef: RefObject<HTMLDivElement | null>;
94
+ origin: Offset | null;
95
+ clipPath?: string;
96
+ fadeDotsBottom?: DotsBoardBottomFade;
97
+ }
98
+
99
+ /** Interactive and ambient effects, shared by scrolling and sticky layers. */
100
+ export type DotsBoardEffects = Omit<DotGridOptions, 'dots' | 'reveal'> & { hover?: DotsBoardHover };
101
+
102
+ /** Pointer tint for base-colored dots; the image fades into this tint where hovered. */
103
+ export interface DotsBoardHover {
104
+ /** Any CSS color, including a theme variable. */
105
+ color: string;
106
+ /** Reach in CSS pixels; defaults to 175. */
107
+ radius?: number;
108
+ /** False keeps the tint solid throughout the radius; defaults to a soft edge. */
109
+ fade?: boolean;
110
+ }
111
+
112
+ /** Appear once on the board's clock; lazy layers never replay the intro. */
113
+ export interface DotsBoardIntro {
114
+ delay?: number;
115
+ duration?: number;
116
+ initialSize?: number;
117
+ peakSize?: number;
118
+ }
119
+
120
+ /** A section's size along one axis: CSS pixels or a percentage of its stage. */
121
+ export type DotsBoardLength = number | `${number}%`;
122
+
123
+ /** How a `<DotsBoard.Section>` draws the outline through the dots along its edges. */
124
+ export interface DotsBoardOutline {
125
+ /** CSS stroke color. */
126
+ color?: string;
127
+ /** Stroke width in CSS pixels. */
128
+ width?: number;
129
+ dashed?: boolean;
130
+ }
131
+
132
+ /**
133
+ * Resolved
134
+ */
135
+
136
+ /** Where a `<DotsBoard.Section>` asks to go and how large, with its defaults filled in. */
137
+ export interface DotsBoardSectionSpec {
138
+ x: DotsBoardCoordinate;
139
+ y: DotsBoardCoordinate;
140
+ dx: number;
141
+ dy: number;
142
+ anchorX: AlignX;
143
+ anchorY: AlignY;
144
+ /** Unset fits the content. */
145
+ width?: DotsBoardLength;
146
+ /** Unset fits the content. */
147
+ height?: DotsBoardLength;
148
+ /** Least CSS pixels between the outline and the content on every side. */
149
+ padding: number;
150
+ }
151
+
152
+ /** A rectangle in CSS pixels. */
153
+ export interface Rect extends Offset {
154
+ width: number;
155
+ height: number;
156
+ }
157
+
158
+ /** Part of a layer's dots a section leaves undrawn, in the layer's pixels: a rectangle with corners rounded `radius` pixels, less a spot `keepRadius` pixels around each of the `keep` dots, which stay drawn. */
159
+ export interface DotsBoardHole extends Rect {
160
+ radius: number;
161
+ keep: Point[];
162
+ keepRadius: number;
163
+ }
164
+
165
+ /** One connection as the engine draws it: a single string pinned to the items' dots in order. */
166
+ export interface DotsBoardStringSpec {
167
+ key: string;
168
+ items: string[];
169
+ color: string;
170
+ width: number;
171
+ slack: number;
172
+ maxSag: number;
173
+ dashed: boolean;
174
+ }
175
+
176
+ /** The board-wide string settings the engine animates with. */
177
+ export interface DotsBoardStringOptions {
178
+ /** Null snaps knots and bends into place instead of springing. */
179
+ spring: Spring | null;
180
+ fadeMs: number;
181
+ /** Pixels a crossing pointer pulls a string; 0 for none. */
182
+ tug: number;
183
+ /** Pixels a string can be dragged before it slips from the pointer; 0 for not grabbable. */
184
+ pull: number;
185
+ }
186
+
187
+ /**
188
+ * Contexts
189
+ */
190
+
191
+ /** An element's size in CSS pixels, with a counter that ticks on every change so dependents can re-measure. */
192
+ export interface ElementSize {
193
+ width: number;
194
+ height: number;
195
+ version: number;
196
+ }
197
+
198
+ /** Where an element's top-left corner sits inside another, in CSS pixels. */
199
+ export interface Offset {
200
+ left: number;
201
+ top: number;
202
+ }
203
+
204
+ /** What a `<DotsBoard>` shares with the layers, stages and items inside it. */
205
+ export interface DotsBoardContextValue extends ElementSize {
206
+ /** The grid at rest, resolved. */
207
+ grid: Required<DotStyle>;
208
+ ref: RefObject<HTMLDivElement | null>;
209
+ /** Draws the connections; items report their dots to it in board pixels. */
210
+ strings: DotsStringEngine;
211
+ animation: ResolvedDotGridOptions;
212
+ /** Artwork drawn by the in-flow footer beneath the board's dots. */
213
+ footerImageUrl?: string;
214
+ background: { image?: DotsBoardImage; intro?: DotsBoardIntro; hover?: DotsBoardHover };
215
+ /** Shared performance.now() origin for intros, pulses and click effects. */
216
+ begin: number;
217
+ }
218
+
219
+ /**
220
+ * A layer of the board that paints its own dots: the board itself, or a `<DotsBoard.Sticky>`.
221
+ * Stages measure against its element, so their items stay on its dots when it moves as one
222
+ * with them; `left` / `top` is where its top-left corner rests in board pixels.
223
+ */
224
+ export interface DotsLayerContextValue extends Offset {
225
+ width: number;
226
+ height: number;
227
+ ref: RefObject<HTMLDivElement | null>;
228
+ /** Leaves a section's hole in this layer's dots undrawn, keyed by the section; null takes it away again. */
229
+ setHole: (key: string, hole: DotsBoardHole | null) => void;
230
+ }
231
+
232
+ /** A stage's rectangle in board pixels. */
233
+ export interface DotsStageContextValue {
234
+ left: number;
235
+ top: number;
236
+ width: number;
237
+ height: number;
238
+ /** False until the board and the stage have been measured, so items can stay hidden instead of jumping. */
239
+ ready: boolean;
240
+ }
@@ -0,0 +1,49 @@
1
+ import { useLayoutEffect, useRef } from 'react';
2
+ import type { RefObject } from 'react';
3
+
4
+ /** The visible slice of a layer, shared directly with its renderer without React scroll updates. */
5
+ export interface DotsBoardView {
6
+ top: number;
7
+ invalidate?: () => void;
8
+ }
9
+
10
+ /*
11
+ * Keep a bounded canvas over the visible part of a potentially long layer. The simulation
12
+ * retains its logical coordinates while this window moves, so scrolling never resets dots.
13
+ * minTop confines an optional blend surface to the bottom band.
14
+ * Overscan keeps point sprites whose centers are outside the canvas away from visible edges.
15
+ */
16
+ export function useDotsBoardView(host: RefObject<HTMLDivElement | null>, height: number, viewHeight: number, minTop = 0) {
17
+ const ref = useRef<HTMLDivElement>(null);
18
+ const view = useRef<DotsBoardView>({ top: 0 });
19
+ useLayoutEffect(() => {
20
+ let previousRect: DOMRect | undefined;
21
+
22
+ /*
23
+ * Move the canvas window and refresh pointer coordinates only when its host has moved.
24
+ */
25
+ const place = () => {
26
+ const element = host.current;
27
+ if (!element || !ref.current) return;
28
+ const rect = element.getBoundingClientRect();
29
+ const top = Math.max(minTop, Math.min(height - viewHeight, Math.floor(-rect.top) - 256));
30
+ const moved = top !== view.current.top;
31
+ if (!previousRect || moved) {
32
+ view.current.top = top;
33
+ ref.current.style.transform = `translateY(${top}px)`;
34
+ }
35
+ // Even a clamped canvas must update hover when its host moves beneath a still pointer.
36
+ if (!previousRect || moved || rect.left !== previousRect.left || rect.top !== previousRect.top) view.current.invalidate?.();
37
+ previousRect = rect;
38
+ };
39
+ place();
40
+ // Keep placement ahead of any already queued render frame so its pixels and position agree.
41
+ window.addEventListener('scroll', place, { passive: true, capture: true });
42
+ window.addEventListener('resize', place);
43
+ return () => {
44
+ window.removeEventListener('scroll', place, true);
45
+ window.removeEventListener('resize', place);
46
+ };
47
+ }, [host, height, viewHeight, minTop]);
48
+ return { ref, view };
49
+ }
@@ -0,0 +1,190 @@
1
+ import { useMemo, useState } from 'react';
2
+ import type { DotsBoardSceneParams } from '../scenes/DotsBoard.ts';
3
+ import { getBottomFadeMaskStyle, getDotGridAnchor, getDotGridMaskStyle } from '../utils/dotGridSpecs.ts';
4
+ import {
5
+ getDotsBoardBlendColor,
6
+ getDotsBoardBlendMode,
7
+ getDotsBoardFadeGradient,
8
+ resolveDotsBoardBottomFade,
9
+ } from './DotsBoard.helpers.ts';
10
+ import { useDotsBoard, useDotsLayer, useViewportHeight } from './DotsBoard.hooks.ts';
11
+ import type { DotsBoardCanvasProps } from './DotsBoard.types.ts';
12
+ import { useDotsBoardView } from './DotsBoardCanvas.hooks.ts';
13
+ import { useContentMemo } from './GfxCanvas.hooks.ts';
14
+ import { GfxCanvas } from './GfxCanvas.tsx';
15
+
16
+ /*
17
+ * Load WebGL only once a layer's canvas approaches the viewport.
18
+ */
19
+ const loadDotsScene = () => import('../scenes/DotsBoard.ts');
20
+
21
+ /*
22
+ * Internal background for both scrolling and sticky layers. Its static fallback, image,
23
+ * animated grid and section cutouts all use the same local grid origin. Only this background
24
+ * clips overflow; its content and sticky descendants remain in the document's scroll context.
25
+ */
26
+ const DotsBoardSurface = (p: DotsBoardCanvasProps & { blend?: boolean }) => {
27
+ const { hostRef, origin, clipPath, fadeDotsBottom, blend = false } = p;
28
+ const board = useDotsBoard();
29
+ const fade = useContentMemo(() => resolveDotsBoardBottomFade(fadeDotsBottom), [fadeDotsBottom]);
30
+ const blendMode = getDotsBoardBlendMode(fade);
31
+ const [red, green, blue, opacity] = getDotsBoardBlendColor(fade);
32
+ const [ready, setReady] = useState(false);
33
+ const size = useDotsLayer();
34
+ const viewportHeight = useViewportHeight();
35
+ const minTop = blend && fade ? Math.max(0, size.height - fade.start) : 0;
36
+ const viewHeight = Math.max(1, Math.min(size.height - minTop || 1, (viewportHeight || 800) + 512));
37
+ const { ref, view } = useDotsBoardView(hostRef, size.height, viewHeight, minTop);
38
+ const left = origin?.left ?? 0;
39
+ const top = origin?.top ?? 0;
40
+ const dots = useMemo(
41
+ () => ({
42
+ ...board.grid,
43
+ alignX: getDotGridAnchor(board.grid.alignX, board.grid.size, board.width) - left,
44
+ alignY: getDotGridAnchor(board.grid.alignY, board.grid.size, board.height) - top,
45
+ }),
46
+ [board.grid, board.width, board.height, left, top],
47
+ );
48
+ const image = board.background.image;
49
+ // Layers wholly below the image region need neither its download nor a GPU texture.
50
+ const layerImage = !blend && image && top < image.height ? image : undefined;
51
+ const params = useMemo<DotsBoardSceneParams>(
52
+ () => ({
53
+ ...board.animation,
54
+ dots,
55
+ boardRef: board.ref,
56
+ hostRef,
57
+ view: view.current,
58
+ width: size.width,
59
+ height: size.height,
60
+ image: layerImage,
61
+ imageOffset: { left, top },
62
+ imageWidth: board.width,
63
+ intro: board.background.intro,
64
+ hover: board.background.hover,
65
+ begin: board.begin,
66
+ fadeDotsBottom: fade,
67
+ blend,
68
+ }),
69
+ [
70
+ board.animation,
71
+ dots,
72
+ board.ref,
73
+ hostRef,
74
+ size.width,
75
+ size.height,
76
+ layerImage,
77
+ left,
78
+ top,
79
+ board.width,
80
+ board.background.intro,
81
+ board.background.hover,
82
+ board.begin,
83
+ fade,
84
+ blend,
85
+ ],
86
+ );
87
+ const placeholderStyle = ready ? undefined : getDotGridMaskStyle(board.width ? dots : board.grid);
88
+
89
+ return (
90
+ <div
91
+ className='abs_full of noclick'
92
+ style={{
93
+ clipPath,
94
+ mixBlendMode: blend ? blendMode : undefined,
95
+ maskImage: fade?.fadeOut ? getDotsBoardFadeGradient(fade.fadeOut, '#000', 'transparent') : undefined,
96
+ }}
97
+ aria-hidden
98
+ >
99
+ {!ready && blend && fade && blendMode && (
100
+ <div
101
+ className='abs_full'
102
+ style={{
103
+ ...placeholderStyle,
104
+ backgroundImage: getDotsBoardFadeGradient(
105
+ fade,
106
+ 'transparent',
107
+ `rgba(${red * 255},${green * 255},${blue * 255},${opacity})`,
108
+ ),
109
+ }}
110
+ />
111
+ )}
112
+ {!ready && !blend && (
113
+ <div
114
+ className='abs_full'
115
+ style={{ ...placeholderStyle, isolation: 'isolate' }}
116
+ >
117
+ <div
118
+ className='abs_full'
119
+ style={{
120
+ backgroundColor: dots.color,
121
+ maskImage: fade ? getDotsBoardFadeGradient(fade, '#000', 'transparent') : undefined,
122
+ }}
123
+ >
124
+ {layerImage && (
125
+ <div
126
+ className='abs'
127
+ style={{
128
+ left: -left,
129
+ top: -top,
130
+ width: board.width || '100%',
131
+ height: layerImage.height,
132
+ backgroundImage: `url(${JSON.stringify(layerImage.src)})`,
133
+ backgroundSize: 'cover',
134
+ backgroundPosition: 'center',
135
+ ...getBottomFadeMaskStyle(Math.min(layerImage.height, Math.max(0, layerImage.fade ?? 0))),
136
+ }}
137
+ />
138
+ )}
139
+ </div>
140
+ {fade && fade.color !== undefined && (
141
+ <div
142
+ className='abs_full'
143
+ style={{
144
+ backgroundImage: getDotsBoardFadeGradient(fade, 'transparent', fade.color),
145
+ // Add premultiplied colors, matching the GPU even when the target is translucent.
146
+ mixBlendMode: 'plus-lighter',
147
+ }}
148
+ />
149
+ )}
150
+ </div>
151
+ )}
152
+ <div
153
+ ref={ref}
154
+ className='abs w_f'
155
+ style={{ top: 0, height: viewHeight }}
156
+ >
157
+ <GfxCanvas
158
+ load={loadDotsScene}
159
+ params={params}
160
+ fill
161
+ transparent
162
+ // Preserve opaque dot centers on Retina displays instead of upscaling a 1.5x texture.
163
+ maxPixelRatio={blend ? 2 : 1.5}
164
+ fadeIn={0}
165
+ onReady={() => setReady(true)}
166
+ onError={() => setReady(false)}
167
+ />
168
+ </div>
169
+ </div>
170
+ );
171
+ };
172
+
173
+ /*
174
+ * Normal colors and backdrop blends must composite separately: CSS blending belongs on
175
+ * the outer surface so it sees the footer beneath, not an isolated canvas wrapper. Both
176
+ * surfaces inherit the same grid, effects and clock; only backdrop blend modes need a second canvas.
177
+ */
178
+ export const DotsBoardCanvas = (p: DotsBoardCanvasProps) => {
179
+ return (
180
+ <>
181
+ <DotsBoardSurface {...p} />
182
+ {getDotsBoardBlendMode(p.fadeDotsBottom) && (
183
+ <DotsBoardSurface
184
+ {...p}
185
+ blend
186
+ />
187
+ )}
188
+ </>
189
+ );
190
+ };