@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.
- package/README.md +248 -54
- package/elements/Shapes.helpers.ts +204 -0
- package/elements/Shapes.hooks.ts +171 -0
- package/elements/Shapes.tsx +505 -0
- package/elements/Shapes.types.ts +81 -0
- package/elements/Stack.helpers.ts +42 -0
- package/elements/Stack.tsx +143 -0
- package/elements/Text.helpers.ts +45 -0
- package/elements/Text.hooks.ts +197 -0
- package/elements/Text.tsx +98 -0
- package/libs/dotGrid.helpers.ts +53 -0
- package/libs/dotGrid.ts +480 -77
- package/libs/effects/dotEffects.ts +136 -0
- package/libs/effects/effectQuad.ts +8 -2
- package/libs/pointer.ts +6 -0
- package/libs/runtime.ts +6 -4
- package/libs/types.ts +8 -1
- package/main.ts +2 -0
- package/modules/Carousel.helpers.ts +234 -0
- package/modules/Carousel.hooks.ts +520 -0
- package/modules/Carousel.tsx +135 -29
- package/modules/Dots.tsx +269 -0
- package/modules/DotsBoard.helpers.ts +388 -0
- package/modules/DotsBoard.hooks.ts +144 -0
- package/modules/DotsBoard.strings.ts +649 -0
- package/modules/DotsBoard.types.ts +240 -0
- package/modules/DotsBoardCanvas.hooks.ts +49 -0
- package/modules/DotsBoardCanvas.tsx +190 -0
- package/modules/DotsBoardElements.tsx +330 -0
- package/modules/Gallery.helpers.ts +32 -0
- package/modules/Gallery.hooks.ts +149 -0
- package/modules/Gallery.tsx +194 -0
- package/modules/GfxCanvas.tsx +10 -2
- package/modules/KineticHeader.helpers.ts +44 -0
- package/modules/KineticHeader.hooks.ts +206 -0
- package/modules/KineticHeader.tsx +305 -0
- package/modules/KineticHeader.types.ts +56 -0
- package/modules/ScrollSteps.hooks.ts +138 -0
- package/modules/ScrollSteps.tsx +7 -52
- package/package.json +15 -5
- package/scenes/Carousel.ts +99 -18
- package/scenes/DotsBoard.helpers.ts +12 -0
- package/scenes/DotsBoard.ts +245 -0
- package/tsconfig.json +1 -1
- package/utils/align.ts +18 -0
- package/utils/color.ts +64 -2
- package/utils/dotEffectSpecs.ts +79 -0
- package/utils/dotGridSpecs.ts +304 -46
- package/utils/motion.ts +88 -0
- package/utils/random.ts +22 -0
- package/utils/strings.ts +116 -0
- package/modules/DotsArea.tsx +0 -120
- package/modules/VisionBoard.helpers.ts +0 -561
- package/modules/VisionBoard.tsx +0 -358
- package/modules/VisionBoard.types.ts +0 -88
- package/scenes/DotsArea.ts +0 -208
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { cn } from '@jsb188/app/utils/string.ts';
|
|
2
|
+
import { memo, useId, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import type { CSSProperties, ReactNode } from 'react';
|
|
4
|
+
import { Marker } from '../elements/Shapes.tsx';
|
|
5
|
+
import type { AlignX, AlignY } from '../utils/align.ts';
|
|
6
|
+
import {
|
|
7
|
+
getDotsBoardHole,
|
|
8
|
+
getDotsBoardItemDot,
|
|
9
|
+
getDotsBoardItemTransform,
|
|
10
|
+
getDotsBoardSectionContentStyle,
|
|
11
|
+
getDotsBoardSectionRect,
|
|
12
|
+
resolveDotsBoardOutline,
|
|
13
|
+
} from './DotsBoard.helpers.ts';
|
|
14
|
+
import { DotsStageContext, useDotsBoard, useDotsLayer, useDotsStage, useElementOffset, useElementSize } from './DotsBoard.hooks.ts';
|
|
15
|
+
import type {
|
|
16
|
+
DotsBoardCoordinate,
|
|
17
|
+
DotsBoardLength,
|
|
18
|
+
DotsBoardOutline,
|
|
19
|
+
DotsBoardSectionSpec,
|
|
20
|
+
DotsStageContextValue,
|
|
21
|
+
} from './DotsBoard.types.ts';
|
|
22
|
+
import { useContentMemo } from './GfxCanvas.hooks.ts';
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
* An in-flow area of a `<DotsBoard>` that reserves `height` and pins its `<DotsBoard.Item>`s
|
|
26
|
+
* to the board's dots. It measures where it sits inside its layer (the board, or the
|
|
27
|
+
* `<DotsBoard.Sticky>` it is in), so when the flow content above it changes height (a heading
|
|
28
|
+
* wrapping to another line) its items re-snap to the dots now under them instead of drifting
|
|
29
|
+
* off the grid.
|
|
30
|
+
*/
|
|
31
|
+
export const DotsBoardStage = memo(
|
|
32
|
+
(p: { height: number | string; children?: ReactNode; className?: string; style?: CSSProperties }) => {
|
|
33
|
+
const { height, children, className, style } = p;
|
|
34
|
+
const board = useDotsBoard();
|
|
35
|
+
const layer = useDotsLayer();
|
|
36
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
37
|
+
const size = useElementSize(ref);
|
|
38
|
+
// Re-measure whenever the board, the layer's rest, or the stage itself has changed.
|
|
39
|
+
const offset = useElementOffset(ref, layer.ref, [board.version, size.version, layer.left, layer.top]);
|
|
40
|
+
const ready = board.version > 0 && size.version > 0;
|
|
41
|
+
const stage = useMemo<DotsStageContextValue>(
|
|
42
|
+
() => ({ left: layer.left + offset.left, top: layer.top + offset.top, width: size.width, height: size.height, ready }),
|
|
43
|
+
[layer.left, layer.top, offset, size.width, size.height, ready],
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<DotsStageContext.Provider value={stage}>
|
|
48
|
+
<div
|
|
49
|
+
ref={ref}
|
|
50
|
+
className={cn('rel', className)}
|
|
51
|
+
style={{ height, ...style }}
|
|
52
|
+
>
|
|
53
|
+
{children}
|
|
54
|
+
</div>
|
|
55
|
+
</DotsStageContext.Provider>
|
|
56
|
+
);
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The marker sits centered on the dot, above the children, so the pin stays visible over content
|
|
62
|
+
* centered on it. While the pointer is over it, it rises above other items' content too (up to the
|
|
63
|
+
* `z1` class), so hover content it shows (a `<PulsingMarker>`'s card) is never covered by them.
|
|
64
|
+
*/
|
|
65
|
+
const MARKER_WRAPPER_STYLE: CSSProperties = { left: 0, top: 0, zIndex: 1, lineHeight: 0, transform: 'translate(-50%, -50%)' };
|
|
66
|
+
const MARKER_WRAPPER_RAISED_STYLE: CSSProperties = { ...MARKER_WRAPPER_STYLE, zIndex: 20 };
|
|
67
|
+
|
|
68
|
+
/*
|
|
69
|
+
* Content pinned to a dot of the enclosing `<DotsBoard>`, placed inside a `<DotsBoard.Stage>`.
|
|
70
|
+
* `x` / `y` say roughly where in the stage it goes (pixels, a percentage of the stage, or
|
|
71
|
+
* `center`); the item snaps to the nearest dot, then moves `dx` / `dy` whole dots, so it reads
|
|
72
|
+
* the same at any width while always sitting exactly on the grid. A `marker` sits centered on
|
|
73
|
+
* the dot, above the children (a `<Marker>` unless one is given; `null` for none), and
|
|
74
|
+
* the children hang off the dot with their `anchorX` / `anchorY` edge (or middle) on it (below
|
|
75
|
+
* it, centered, by default), shifted by `offsetX` / `offsetY` pixels; the marker stays put.
|
|
76
|
+
* Hidden until the board has been measured, so it never paints off-grid. An `id` lets the
|
|
77
|
+
* board's `connections` run strings through the item's dot.
|
|
78
|
+
*/
|
|
79
|
+
export const DotsBoardItem = memo(
|
|
80
|
+
(p: {
|
|
81
|
+
/** Name for the board's `connections`; items without one cannot be connected. */
|
|
82
|
+
id?: string;
|
|
83
|
+
x: DotsBoardCoordinate;
|
|
84
|
+
y: DotsBoardCoordinate;
|
|
85
|
+
/** Whole dots to move right (negative: left) after snapping. */
|
|
86
|
+
dx?: number;
|
|
87
|
+
/** Whole dots to move down (negative: up) after snapping. */
|
|
88
|
+
dy?: number;
|
|
89
|
+
/** Which edge (or the middle) of the children sits on the dot. */
|
|
90
|
+
anchorX?: AlignX;
|
|
91
|
+
anchorY?: AlignY;
|
|
92
|
+
/** Pixels to shift the children right (negative: left) from where the anchor puts them; the marker does not move. */
|
|
93
|
+
offsetX?: number;
|
|
94
|
+
/** Pixels to shift the children down (negative: up) from where the anchor puts them; the marker does not move. */
|
|
95
|
+
offsetY?: number;
|
|
96
|
+
marker?: ReactNode;
|
|
97
|
+
children?: ReactNode;
|
|
98
|
+
/** Applied to the children's wrapper. */
|
|
99
|
+
className?: string;
|
|
100
|
+
style?: CSSProperties;
|
|
101
|
+
}) => {
|
|
102
|
+
const {
|
|
103
|
+
id,
|
|
104
|
+
x,
|
|
105
|
+
y,
|
|
106
|
+
dx = 0,
|
|
107
|
+
dy = 0,
|
|
108
|
+
anchorX = 'center',
|
|
109
|
+
anchorY = 'top',
|
|
110
|
+
offsetX = 0,
|
|
111
|
+
offsetY = 0,
|
|
112
|
+
marker,
|
|
113
|
+
children,
|
|
114
|
+
className,
|
|
115
|
+
style,
|
|
116
|
+
} = p;
|
|
117
|
+
const { grid, width, height, strings, ref: boardRef } = useDotsBoard();
|
|
118
|
+
// Inside a `<DotsBoard.Sticky>`, whose own `<svg>`s then draw the item's strings.
|
|
119
|
+
const layered = useDotsLayer().ref !== boardRef;
|
|
120
|
+
const stage = useDotsStage();
|
|
121
|
+
const dot = useMemo(
|
|
122
|
+
() => getDotsBoardItemDot({ x, y, dx, dy }, stage, grid, width, height),
|
|
123
|
+
[x, y, dx, dy, stage, grid, width, height],
|
|
124
|
+
);
|
|
125
|
+
const transform = useMemo(
|
|
126
|
+
() => getDotsBoardItemTransform(anchorX, anchorY, offsetX, offsetY),
|
|
127
|
+
[anchorX, anchorY, offsetX, offsetY],
|
|
128
|
+
);
|
|
129
|
+
const [markerRaised, setMarkerRaised] = useState(false);
|
|
130
|
+
|
|
131
|
+
// Report the dot to the strings once it is real, and take it away again in the same phase.
|
|
132
|
+
// A move clears and re-reports it in one commit, before the strings' next frame, so they
|
|
133
|
+
// re-pin instead of tearing down; a Fast Refresh, which re-runs every effect, ends with
|
|
134
|
+
// the dot reported instead of cleared.
|
|
135
|
+
useLayoutEffect(() => {
|
|
136
|
+
if (!id || !stage.ready) return;
|
|
137
|
+
strings.setDot(id, dot, layered);
|
|
138
|
+
return () => strings.setDot(id, null);
|
|
139
|
+
}, [strings, id, stage.ready, dot, layered]);
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<div
|
|
143
|
+
className='abs'
|
|
144
|
+
style={{ left: dot.x - stage.left, top: dot.y - stage.top, visibility: stage.ready ? undefined : 'hidden' }}
|
|
145
|
+
>
|
|
146
|
+
{children != null && (
|
|
147
|
+
<div
|
|
148
|
+
className={cn('abs', className)}
|
|
149
|
+
style={{ left: 0, top: 0, width: 'max-content', transform, ...style }}
|
|
150
|
+
>
|
|
151
|
+
{children}
|
|
152
|
+
</div>
|
|
153
|
+
)}
|
|
154
|
+
{marker !== null && (
|
|
155
|
+
<div
|
|
156
|
+
className='abs'
|
|
157
|
+
style={markerRaised ? MARKER_WRAPPER_RAISED_STYLE : MARKER_WRAPPER_STYLE}
|
|
158
|
+
onPointerEnter={() => setMarkerRaised(true)}
|
|
159
|
+
onPointerLeave={() => setMarkerRaised(false)}
|
|
160
|
+
>
|
|
161
|
+
{marker ?? <Marker />}
|
|
162
|
+
</div>
|
|
163
|
+
)}
|
|
164
|
+
</div>
|
|
165
|
+
);
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
/*
|
|
170
|
+
* A rectangle of the enclosing `<DotsBoard>` that holds content of any kind, placed inside a
|
|
171
|
+
* `<DotsBoard.Stage>`: its corners sit on the board's dots, an outline is drawn along the row
|
|
172
|
+
* of dots at its edges in place of them, and the dots inside it are left undrawn, so whatever
|
|
173
|
+
* is behind the board shows around the content (without an outline, the dots on the edge
|
|
174
|
+
* stay and mark it out). `x` / `y` say roughly where in the stage it goes, as for
|
|
175
|
+
* `<DotsBoard.Item>`, with its `anchorX` / `anchorY` edge (or middle) there (its top edge,
|
|
176
|
+
* centered, by default); it snaps to the nearest dots, then moves `dx` / `dy` whole dots. A
|
|
177
|
+
* `width` / `height` is rounded to the nearest whole dots and the content fills the room inside
|
|
178
|
+
* the `padding`; without one the rectangle fits the content, rounded up to whole dots with at
|
|
179
|
+
* least `padding` pixels around it. Either way the content lays out like a regular block from
|
|
180
|
+
* the top-left corner, `padding` pixels in, so its own spacing always shows: only the outline
|
|
181
|
+
* snaps to the dots, and the rounding up leaves extra room at the far edge. The
|
|
182
|
+
* shared background renderer leaves these cutouts empty. Until the board
|
|
183
|
+
* has been measured (and on the server) the content shows about where it belongs, without the
|
|
184
|
+
* outline, and settles onto the dots once it is. Sections must not overlap, or the dots where
|
|
185
|
+
* they do are drawn again.
|
|
186
|
+
*/
|
|
187
|
+
export const DotsBoardSection = memo(
|
|
188
|
+
(p: {
|
|
189
|
+
x: DotsBoardCoordinate;
|
|
190
|
+
y: DotsBoardCoordinate;
|
|
191
|
+
/** Whole dots to move right (negative: left) after snapping. */
|
|
192
|
+
dx?: number;
|
|
193
|
+
/** Whole dots to move down (negative: up) after snapping. */
|
|
194
|
+
dy?: number;
|
|
195
|
+
/** Which edge (or the middle) of the rectangle sits at `x` / `y`. */
|
|
196
|
+
anchorX?: AlignX;
|
|
197
|
+
anchorY?: AlignY;
|
|
198
|
+
/** Pixels or a percentage of the stage, rounded to whole dots; unset fits the content. */
|
|
199
|
+
width?: DotsBoardLength;
|
|
200
|
+
/** Pixels or a percentage of the stage, rounded to whole dots; unset fits the content. */
|
|
201
|
+
height?: DotsBoardLength;
|
|
202
|
+
/** Least pixels between the outline and the content on every side; one dot spacing by default. */
|
|
203
|
+
padding?: number;
|
|
204
|
+
/** Corner radius in pixels, like CSS `border-radius`; square corners by default. With an outline, the dots a rounded corner curves the stroke away from stay drawn. */
|
|
205
|
+
radius?: number;
|
|
206
|
+
/** The stroke along the edges, drawn in place of the dots there, in the strings' default color and width unless given; `false` for none, which keeps those dots. */
|
|
207
|
+
outline?: DotsBoardOutline | false;
|
|
208
|
+
children?: ReactNode;
|
|
209
|
+
/** Applied to the rectangle, under the content: a background fills the area inside the outline. */
|
|
210
|
+
className?: string;
|
|
211
|
+
style?: CSSProperties;
|
|
212
|
+
}) => {
|
|
213
|
+
const {
|
|
214
|
+
x,
|
|
215
|
+
y,
|
|
216
|
+
dx = 0,
|
|
217
|
+
dy = 0,
|
|
218
|
+
anchorX = 'center',
|
|
219
|
+
anchorY = 'top',
|
|
220
|
+
width,
|
|
221
|
+
height,
|
|
222
|
+
padding,
|
|
223
|
+
radius = 0,
|
|
224
|
+
outline,
|
|
225
|
+
children,
|
|
226
|
+
className,
|
|
227
|
+
style,
|
|
228
|
+
} = p;
|
|
229
|
+
const { grid, width: boardWidth, height: boardHeight } = useDotsBoard();
|
|
230
|
+
const layer = useDotsLayer();
|
|
231
|
+
const stage = useDotsStage();
|
|
232
|
+
const key = useId();
|
|
233
|
+
const contentRef = useRef<HTMLDivElement>(null);
|
|
234
|
+
const content = useElementSize(contentRef);
|
|
235
|
+
const section = useMemo<DotsBoardSectionSpec>(
|
|
236
|
+
() => ({ x, y, dx, dy, anchorX, anchorY, width, height, padding: padding ?? grid.spacing }),
|
|
237
|
+
[x, y, dx, dy, anchorX, anchorY, width, height, padding, grid.spacing],
|
|
238
|
+
);
|
|
239
|
+
// The rectangle, once the board, the stage and the content have all been measured.
|
|
240
|
+
const rect = useMemo(
|
|
241
|
+
() =>
|
|
242
|
+
stage.ready && content.version > 0 ? getDotsBoardSectionRect(section, content, stage, grid, boardWidth, boardHeight) : null,
|
|
243
|
+
[section, content, stage, grid, boardWidth, boardHeight],
|
|
244
|
+
);
|
|
245
|
+
const contentStyle = useMemo(
|
|
246
|
+
() => getDotsBoardSectionContentStyle(section, rect, stage, grid.spacing),
|
|
247
|
+
[section, rect, stage, grid.spacing],
|
|
248
|
+
);
|
|
249
|
+
const line = useContentMemo(() => resolveDotsBoardOutline(outline), [outline]);
|
|
250
|
+
|
|
251
|
+
// Leave the dots inside undrawn, and take the hole away again in the same phase, so a move
|
|
252
|
+
// re-cuts it in one commit, before the paint.
|
|
253
|
+
const hole = useMemo(
|
|
254
|
+
() => rect && getDotsBoardHole(rect, layer, grid, radius, line ? line.width : null),
|
|
255
|
+
[rect, layer, grid, radius, line],
|
|
256
|
+
);
|
|
257
|
+
const { setHole } = layer;
|
|
258
|
+
useLayoutEffect(() => {
|
|
259
|
+
if (!hole) return;
|
|
260
|
+
setHole(key, hole);
|
|
261
|
+
return () => setHole(key, null);
|
|
262
|
+
}, [setHole, key, hole]);
|
|
263
|
+
|
|
264
|
+
return (
|
|
265
|
+
<>
|
|
266
|
+
{rect && (
|
|
267
|
+
<div
|
|
268
|
+
className={cn('abs', className)}
|
|
269
|
+
style={{
|
|
270
|
+
left: rect.left - stage.left,
|
|
271
|
+
top: rect.top - stage.top,
|
|
272
|
+
width: rect.width,
|
|
273
|
+
height: rect.height,
|
|
274
|
+
borderRadius: radius,
|
|
275
|
+
...style,
|
|
276
|
+
}}
|
|
277
|
+
>
|
|
278
|
+
{line && (
|
|
279
|
+
// Overflowing, so the half of the stroke outside the rectangle shows too.
|
|
280
|
+
<svg
|
|
281
|
+
className='abs_full w_f h_f noclick'
|
|
282
|
+
style={{ overflow: 'visible' }}
|
|
283
|
+
aria-hidden='true'
|
|
284
|
+
>
|
|
285
|
+
<rect
|
|
286
|
+
width='100%'
|
|
287
|
+
height='100%'
|
|
288
|
+
rx={radius}
|
|
289
|
+
fill='none'
|
|
290
|
+
strokeDasharray={line.dashed ? `${line.width * 2} ${line.width * 4}` : undefined}
|
|
291
|
+
style={{ stroke: line.color, strokeWidth: line.width }}
|
|
292
|
+
/>
|
|
293
|
+
</svg>
|
|
294
|
+
)}
|
|
295
|
+
</div>
|
|
296
|
+
)}
|
|
297
|
+
<div
|
|
298
|
+
ref={contentRef}
|
|
299
|
+
className='abs'
|
|
300
|
+
style={contentStyle}
|
|
301
|
+
>
|
|
302
|
+
{children}
|
|
303
|
+
</div>
|
|
304
|
+
</>
|
|
305
|
+
);
|
|
306
|
+
},
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
/*
|
|
310
|
+
* The footer's content determines its responsive height. Its artwork paints below the dots
|
|
311
|
+
* in the root board's isolated stacking context, without measurements or hydration shifts.
|
|
312
|
+
* Page CSS styles [data-dots-footer-background] to match the standalone footer's artwork.
|
|
313
|
+
* Place directly in the board's flow, outside sticky layers and other stacking contexts.
|
|
314
|
+
*/
|
|
315
|
+
export const DotsBoardFooter = memo((p: { children: ReactNode }) => {
|
|
316
|
+
const { footerImageUrl } = useDotsBoard();
|
|
317
|
+
return (
|
|
318
|
+
<div className='rel'>
|
|
319
|
+
{footerImageUrl && (
|
|
320
|
+
<div
|
|
321
|
+
aria-hidden
|
|
322
|
+
data-dots-footer-background
|
|
323
|
+
className='abs_full noclick'
|
|
324
|
+
style={{ backgroundImage: `url(${JSON.stringify(footerImageUrl)})`, zIndex: -1 }}
|
|
325
|
+
/>
|
|
326
|
+
)}
|
|
327
|
+
{p.children}
|
|
328
|
+
</div>
|
|
329
|
+
);
|
|
330
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const FILE_CONSTANTS = {
|
|
2
|
+
opacities: [1, 0.85, 0.7, 0.55],
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Pick each thumbnail's next opacity independently, always changing from its previous choice.
|
|
7
|
+
*/
|
|
8
|
+
export function getGalleryOpacities(count: number, previous: readonly number[] = []): number[] {
|
|
9
|
+
const alphas = FILE_CONSTANTS.opacities;
|
|
10
|
+
return Array.from({ length: count }, (_, index) => {
|
|
11
|
+
const current = alphas.indexOf(previous[index]);
|
|
12
|
+
const next =
|
|
13
|
+
current < 0
|
|
14
|
+
? Math.floor(Math.random() * alphas.length)
|
|
15
|
+
: (current + 1 + Math.floor(Math.random() * (alphas.length - 1))) % alphas.length;
|
|
16
|
+
return alphas[next];
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
* Center a larger preview over its fixed thumbnail slot, keeping it inside the viewport horizontally.
|
|
22
|
+
*/
|
|
23
|
+
export function getGalleryPreviewBounds(slotLeft: number, viewportWidth: number, slotWidth = 200) {
|
|
24
|
+
const width = Math.max(0, Math.min(slotWidth * 1.3, viewportWidth - 16));
|
|
25
|
+
const height = width * 0.65;
|
|
26
|
+
return {
|
|
27
|
+
width,
|
|
28
|
+
height,
|
|
29
|
+
left: Math.max(8 - slotLeft, Math.min((slotWidth - width) / 2, viewportWidth - 8 - slotLeft - width)),
|
|
30
|
+
top: (slotWidth * 0.65 - height) / 2,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import type { RefObject } from 'react';
|
|
3
|
+
import { playVideo } from './Carousel.helpers.ts';
|
|
4
|
+
import { getGalleryOpacities, getGalleryPreviewBounds } from './Gallery.helpers.ts';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Publish video registrations as immutable snapshots so lazily mounted frames wake the demand renderer.
|
|
8
|
+
*/
|
|
9
|
+
export function useGalleryVideos() {
|
|
10
|
+
const [videos, setVideos] = useState<ReadonlyMap<string, HTMLVideoElement>>(() => new Map());
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* Register a thumbnail video and remove only that same element when React releases its ref.
|
|
14
|
+
*/
|
|
15
|
+
const registerVideo = useCallback((key: string, video: HTMLVideoElement) => {
|
|
16
|
+
setVideos((previous) => new Map(previous).set(key, video));
|
|
17
|
+
return () => {
|
|
18
|
+
setVideos((previous) => {
|
|
19
|
+
if (previous.get(key) !== video) return previous;
|
|
20
|
+
const next = new Map(previous);
|
|
21
|
+
next.delete(key);
|
|
22
|
+
return next;
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
}, []);
|
|
26
|
+
|
|
27
|
+
return { videos, registerVideo };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
* Register a video once it is needed and play only the active preview while its gallery and page are visible.
|
|
32
|
+
*/
|
|
33
|
+
export function useGalleryVideo({
|
|
34
|
+
videoKey,
|
|
35
|
+
shouldLoad,
|
|
36
|
+
playing,
|
|
37
|
+
registerVideo,
|
|
38
|
+
}: {
|
|
39
|
+
videoKey: string;
|
|
40
|
+
shouldLoad: boolean;
|
|
41
|
+
playing: boolean;
|
|
42
|
+
registerVideo: ReturnType<typeof useGalleryVideos>['registerVideo'];
|
|
43
|
+
}) {
|
|
44
|
+
const videoRef = useRef<HTMLVideoElement | null>(null);
|
|
45
|
+
|
|
46
|
+
/*
|
|
47
|
+
* Leave source-free videos out of the renderer until a thumbnail enters view or receives focus.
|
|
48
|
+
*/
|
|
49
|
+
const attachVideo = useCallback(
|
|
50
|
+
(video: HTMLVideoElement | null) => {
|
|
51
|
+
videoRef.current = video;
|
|
52
|
+
if (!video || !shouldLoad) return;
|
|
53
|
+
video.muted = true;
|
|
54
|
+
return registerVideo(videoKey, video);
|
|
55
|
+
},
|
|
56
|
+
[videoKey, shouldLoad, registerVideo],
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const video = videoRef.current;
|
|
61
|
+
if (!video) return;
|
|
62
|
+
if (!playing) {
|
|
63
|
+
video.pause();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
* Retry after buffering and resume a focused preview when its page becomes visible again.
|
|
69
|
+
*/
|
|
70
|
+
const syncPlayback = () => {
|
|
71
|
+
if (document.hidden) video.pause();
|
|
72
|
+
else playVideo(video);
|
|
73
|
+
};
|
|
74
|
+
video.addEventListener('canplay', syncPlayback);
|
|
75
|
+
document.addEventListener('visibilitychange', syncPlayback);
|
|
76
|
+
syncPlayback();
|
|
77
|
+
return () => {
|
|
78
|
+
video.removeEventListener('canplay', syncPlayback);
|
|
79
|
+
document.removeEventListener('visibilitychange', syncPlayback);
|
|
80
|
+
video.pause();
|
|
81
|
+
};
|
|
82
|
+
}, [playing, shouldLoad]);
|
|
83
|
+
|
|
84
|
+
return attachVideo;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/*
|
|
88
|
+
* Keep the active preview centered on its slot and inside the viewport after resizing or scrolling.
|
|
89
|
+
*/
|
|
90
|
+
export function useGalleryPreview(slotRef: RefObject<HTMLElement | null>, width: number, active: boolean) {
|
|
91
|
+
const [preview, setPreview] = useState(() => ({ width, height: width * 0.65, left: 0, top: 0 }));
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* Read the slot only on interaction or layout changes, skipping state updates when its bounds are unchanged.
|
|
95
|
+
*/
|
|
96
|
+
const measurePreview = useCallback(() => {
|
|
97
|
+
const slot = slotRef.current;
|
|
98
|
+
if (!slot) return;
|
|
99
|
+
const next = getGalleryPreviewBounds(slot.getBoundingClientRect().left, window.innerWidth, width);
|
|
100
|
+
setPreview((previous) =>
|
|
101
|
+
previous.width === next.width && previous.left === next.left && previous.top === next.top ? previous : next,
|
|
102
|
+
);
|
|
103
|
+
}, [slotRef, width]);
|
|
104
|
+
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (!active) return;
|
|
107
|
+
measurePreview();
|
|
108
|
+
window.addEventListener('resize', measurePreview);
|
|
109
|
+
window.addEventListener('scroll', measurePreview, { passive: true });
|
|
110
|
+
return () => {
|
|
111
|
+
window.removeEventListener('resize', measurePreview);
|
|
112
|
+
window.removeEventListener('scroll', measurePreview);
|
|
113
|
+
};
|
|
114
|
+
}, [active, measurePreview]);
|
|
115
|
+
|
|
116
|
+
return { preview, measurePreview };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/*
|
|
120
|
+
* Change paused thumbnail opacity every two seconds only while the gallery and document are visible.
|
|
121
|
+
*/
|
|
122
|
+
export function useGalleryOpacities(count: number, visible: boolean) {
|
|
123
|
+
const [opacities, setOpacities] = useState(() => getGalleryOpacities(count));
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (!count || !visible) return;
|
|
127
|
+
setOpacities((previous) => (previous.length === count ? previous : getGalleryOpacities(count, previous)));
|
|
128
|
+
let timer: number | undefined;
|
|
129
|
+
|
|
130
|
+
/*
|
|
131
|
+
* Stop the interval itself while the tab is hidden instead of waking it to do no work.
|
|
132
|
+
*/
|
|
133
|
+
const syncTimer = () => {
|
|
134
|
+
window.clearInterval(timer);
|
|
135
|
+
if (document.hidden) return;
|
|
136
|
+
timer = window.setInterval(() => {
|
|
137
|
+
setOpacities((previous) => getGalleryOpacities(count, previous));
|
|
138
|
+
}, 2000);
|
|
139
|
+
};
|
|
140
|
+
document.addEventListener('visibilitychange', syncTimer);
|
|
141
|
+
syncTimer();
|
|
142
|
+
return () => {
|
|
143
|
+
window.clearInterval(timer);
|
|
144
|
+
document.removeEventListener('visibilitychange', syncTimer);
|
|
145
|
+
};
|
|
146
|
+
}, [count, visible]);
|
|
147
|
+
|
|
148
|
+
return opacities;
|
|
149
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { memo, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import type { CarouselSceneParams } from '../scenes/Carousel.ts';
|
|
3
|
+
import { useGalleryOpacities, useGalleryPreview, useGalleryVideo, useGalleryVideos } from './Gallery.hooks.ts';
|
|
4
|
+
import { GfxCanvas } from './GfxCanvas.tsx';
|
|
5
|
+
import { useInView } from './ScrollSteps.hooks.ts';
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* Load the shared video-effect renderer only when the gallery is visible.
|
|
9
|
+
*/
|
|
10
|
+
function loadGalleryScene() {
|
|
11
|
+
return import('../scenes/Carousel.ts');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* Reserve a fixed thumbnail slot and show an enlarged video with its caption on hover or keyboard focus.
|
|
16
|
+
*/
|
|
17
|
+
const GalleryThumb = memo(
|
|
18
|
+
(p: {
|
|
19
|
+
videoUrl: string;
|
|
20
|
+
title?: string;
|
|
21
|
+
index: number;
|
|
22
|
+
videoKey: string;
|
|
23
|
+
width: number;
|
|
24
|
+
registerVideo: ReturnType<typeof useGalleryVideos>['registerVideo'];
|
|
25
|
+
active: boolean;
|
|
26
|
+
playing: boolean;
|
|
27
|
+
onHover: (index: number | null) => void;
|
|
28
|
+
onFocus: (index: number | null) => void;
|
|
29
|
+
}) => {
|
|
30
|
+
const { videoUrl, title, index, videoKey, width, registerVideo, active, playing, onHover, onFocus } = p;
|
|
31
|
+
const height = width * 0.65;
|
|
32
|
+
const slotRef = useRef<HTMLElement>(null);
|
|
33
|
+
const inView = useInView(slotRef, { once: true });
|
|
34
|
+
const shouldLoad = inView || active;
|
|
35
|
+
const attachVideo = useGalleryVideo({ videoKey, shouldLoad, playing, registerVideo });
|
|
36
|
+
const { preview, measurePreview } = useGalleryPreview(slotRef, width, active);
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<figure
|
|
40
|
+
ref={slotRef}
|
|
41
|
+
className='unsel'
|
|
42
|
+
draggable={false}
|
|
43
|
+
aria-label={title}
|
|
44
|
+
tabIndex={title ? 0 : undefined}
|
|
45
|
+
onFocus={() => {
|
|
46
|
+
measurePreview();
|
|
47
|
+
onFocus(index);
|
|
48
|
+
}}
|
|
49
|
+
onBlur={() => onFocus(null)}
|
|
50
|
+
onPointerEnter={(event) => {
|
|
51
|
+
if (event.pointerType === 'touch') return;
|
|
52
|
+
measurePreview();
|
|
53
|
+
onHover(index);
|
|
54
|
+
}}
|
|
55
|
+
onPointerLeave={() => onHover(null)}
|
|
56
|
+
onPointerCancel={() => onHover(null)}
|
|
57
|
+
style={{ position: 'relative', flexShrink: 0, width, height, margin: 0, zIndex: active ? 1 : undefined }}
|
|
58
|
+
>
|
|
59
|
+
<video
|
|
60
|
+
ref={attachVideo}
|
|
61
|
+
className='shadow_float'
|
|
62
|
+
draggable={false}
|
|
63
|
+
src={shouldLoad ? videoUrl : undefined}
|
|
64
|
+
aria-label={title}
|
|
65
|
+
width={width}
|
|
66
|
+
height={height}
|
|
67
|
+
preload={shouldLoad ? 'auto' : 'none'}
|
|
68
|
+
muted
|
|
69
|
+
loop
|
|
70
|
+
playsInline
|
|
71
|
+
crossOrigin='anonymous'
|
|
72
|
+
style={{
|
|
73
|
+
position: 'absolute',
|
|
74
|
+
display: 'block',
|
|
75
|
+
width: active ? preview.width : width,
|
|
76
|
+
height: active ? preview.height : height,
|
|
77
|
+
left: active ? preview.left : 0,
|
|
78
|
+
top: active ? preview.top : 0,
|
|
79
|
+
objectFit: 'cover',
|
|
80
|
+
// Native frames appear only for an active preview; the canvas owns every resting thumbnail.
|
|
81
|
+
opacity: active ? 1 : 0,
|
|
82
|
+
transition: 'opacity 200ms ease, width 160ms ease, height 160ms ease, left 160ms ease, top 160ms ease',
|
|
83
|
+
}}
|
|
84
|
+
/>
|
|
85
|
+
{title && (
|
|
86
|
+
<figcaption
|
|
87
|
+
aria-hidden
|
|
88
|
+
className='bg_active cl_df ft_xs lh_2 p_6 shadow_float'
|
|
89
|
+
style={{
|
|
90
|
+
position: 'absolute',
|
|
91
|
+
left: active ? preview.left : 0,
|
|
92
|
+
bottom: active ? height - preview.top : height,
|
|
93
|
+
width: active ? preview.width : width,
|
|
94
|
+
boxSizing: 'border-box',
|
|
95
|
+
margin: 0,
|
|
96
|
+
opacity: active ? 1 : 0,
|
|
97
|
+
transition: 'opacity 200ms ease, width 160ms ease, left 160ms ease, bottom 160ms ease',
|
|
98
|
+
}}
|
|
99
|
+
>
|
|
100
|
+
{title}
|
|
101
|
+
</figcaption>
|
|
102
|
+
)}
|
|
103
|
+
</figure>
|
|
104
|
+
);
|
|
105
|
+
},
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
GalleryThumb.displayName = 'GalleryThumb';
|
|
109
|
+
|
|
110
|
+
/*
|
|
111
|
+
* Animate paused halftone thumbnails' opacity every two seconds; hovered items play in an enlarged native overlay.
|
|
112
|
+
*/
|
|
113
|
+
export const GalleryThumbs = memo(
|
|
114
|
+
(p: {
|
|
115
|
+
/** Each title is an already translated caption shown above the active preview. */
|
|
116
|
+
items: readonly { videoUrl: string; title?: string }[];
|
|
117
|
+
count?: number;
|
|
118
|
+
className?: string;
|
|
119
|
+
thumbnailWidth?: number;
|
|
120
|
+
}) => {
|
|
121
|
+
const { items, count = items.length, className, thumbnailWidth = 200 } = p;
|
|
122
|
+
const thumbnailCount = items.length ? Math.max(0, Math.floor(Number.isFinite(count) ? count : items.length)) : 0;
|
|
123
|
+
const width = Number.isFinite(thumbnailWidth) && thumbnailWidth > 0 ? thumbnailWidth : 200;
|
|
124
|
+
const galleryRef = useRef<HTMLDivElement>(null);
|
|
125
|
+
const inView = useInView(galleryRef, { disabled: !thumbnailCount });
|
|
126
|
+
const { videos, registerVideo } = useGalleryVideos();
|
|
127
|
+
const opacities = useGalleryOpacities(thumbnailCount, inView);
|
|
128
|
+
const [hoveredIndex, setHoveredIndex] = useState<number | null>(null);
|
|
129
|
+
const [focusedIndex, setFocusedIndex] = useState<number | null>(null);
|
|
130
|
+
const activeIndex = hoveredIndex ?? focusedIndex;
|
|
131
|
+
const thumbnails = useMemo(
|
|
132
|
+
() =>
|
|
133
|
+
Array.from({ length: thumbnailCount }, (_, index) => {
|
|
134
|
+
const item = items[index % items.length];
|
|
135
|
+
return { ...item, key: `${item.videoUrl}-${index}` };
|
|
136
|
+
}),
|
|
137
|
+
[items, thumbnailCount],
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
const params = useMemo<CarouselSceneParams>(
|
|
141
|
+
() => ({
|
|
142
|
+
slides: thumbnails.map(({ key }, index) => ({
|
|
143
|
+
key,
|
|
144
|
+
focused: false,
|
|
145
|
+
opacity: index === activeIndex ? 0 : (opacities[index] ?? 1),
|
|
146
|
+
})),
|
|
147
|
+
videos,
|
|
148
|
+
effects: [{ name: 'halftone', paper: '#FFFFFF', ink: '#888888', size: 6 }],
|
|
149
|
+
transitionMs: 200,
|
|
150
|
+
borderRadius: 0,
|
|
151
|
+
unfocusedOpacity: 1,
|
|
152
|
+
backdropColor: '#FFFFFF',
|
|
153
|
+
waitForAllVideos: false,
|
|
154
|
+
pausedThumbnails: true,
|
|
155
|
+
}),
|
|
156
|
+
[thumbnails, opacities, activeIndex, videos],
|
|
157
|
+
);
|
|
158
|
+
if (!thumbnailCount) return null;
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<div
|
|
162
|
+
ref={galleryRef}
|
|
163
|
+
className={className}
|
|
164
|
+
style={{ position: 'relative', display: 'flex', flexWrap: 'wrap', justifyContent: 'center', gap: 2 }}
|
|
165
|
+
>
|
|
166
|
+
{thumbnails.map((item, index) => (
|
|
167
|
+
<GalleryThumb
|
|
168
|
+
key={item.key}
|
|
169
|
+
videoKey={item.key}
|
|
170
|
+
videoUrl={item.videoUrl}
|
|
171
|
+
title={item.title}
|
|
172
|
+
index={index}
|
|
173
|
+
width={width}
|
|
174
|
+
registerVideo={registerVideo}
|
|
175
|
+
active={activeIndex === index}
|
|
176
|
+
playing={activeIndex === index && inView}
|
|
177
|
+
onHover={setHoveredIndex}
|
|
178
|
+
onFocus={setFocusedIndex}
|
|
179
|
+
/>
|
|
180
|
+
))}
|
|
181
|
+
<GfxCanvas
|
|
182
|
+
load={loadGalleryScene}
|
|
183
|
+
params={params}
|
|
184
|
+
fill
|
|
185
|
+
transparent
|
|
186
|
+
fadeIn={0}
|
|
187
|
+
className='noclick'
|
|
188
|
+
/>
|
|
189
|
+
</div>
|
|
190
|
+
);
|
|
191
|
+
},
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
GalleryThumbs.displayName = 'GalleryThumbs';
|