@jsb188/gfx 1.1.11 → 1.1.13

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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +248 -54
  3. package/elements/Shapes.helpers.ts +204 -0
  4. package/elements/Shapes.hooks.ts +171 -0
  5. package/elements/Shapes.tsx +505 -0
  6. package/elements/Shapes.types.ts +81 -0
  7. package/elements/Stack.helpers.ts +42 -0
  8. package/elements/Stack.tsx +143 -0
  9. package/elements/Text.helpers.ts +45 -0
  10. package/elements/Text.hooks.ts +197 -0
  11. package/elements/Text.tsx +98 -0
  12. package/libs/dotGrid.helpers.ts +53 -0
  13. package/libs/dotGrid.ts +480 -77
  14. package/libs/effects/dotEffects.ts +136 -0
  15. package/libs/effects/effectQuad.ts +8 -2
  16. package/libs/pointer.ts +6 -0
  17. package/libs/runtime.ts +6 -4
  18. package/libs/types.ts +8 -1
  19. package/main.ts +2 -0
  20. package/modules/Carousel.helpers.ts +234 -0
  21. package/modules/Carousel.hooks.ts +520 -0
  22. package/modules/Carousel.tsx +135 -29
  23. package/modules/Dots.tsx +269 -0
  24. package/modules/DotsBoard.helpers.ts +388 -0
  25. package/modules/DotsBoard.hooks.ts +144 -0
  26. package/modules/DotsBoard.strings.ts +649 -0
  27. package/modules/DotsBoard.types.ts +240 -0
  28. package/modules/DotsBoardCanvas.hooks.ts +49 -0
  29. package/modules/DotsBoardCanvas.tsx +190 -0
  30. package/modules/DotsBoardElements.tsx +330 -0
  31. package/modules/Gallery.helpers.ts +32 -0
  32. package/modules/Gallery.hooks.ts +149 -0
  33. package/modules/Gallery.tsx +194 -0
  34. package/modules/GfxCanvas.tsx +10 -2
  35. package/modules/KineticHeader.helpers.ts +44 -0
  36. package/modules/KineticHeader.hooks.ts +206 -0
  37. package/modules/KineticHeader.tsx +305 -0
  38. package/modules/KineticHeader.types.ts +56 -0
  39. package/modules/ScrollSteps.hooks.ts +138 -0
  40. package/modules/ScrollSteps.tsx +7 -52
  41. package/package.json +30 -7
  42. package/scenes/Carousel.ts +99 -18
  43. package/scenes/DotsBoard.helpers.ts +12 -0
  44. package/scenes/DotsBoard.ts +245 -0
  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 +316 -47
  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
  57. package/tsconfig.json +0 -13
@@ -0,0 +1,143 @@
1
+ import { cn } from '@jsb188/app/utils/string.ts';
2
+ import { Children, isValidElement, memo, useMemo, useState } from 'react';
3
+ import type { CSSProperties, ReactNode } from 'react';
4
+ import { ALIGN_TO_FLEX } from '../utils/align.ts';
5
+ import type { AlignX, AlignY } from '../utils/align.ts';
6
+ import { getPinnedOrigin, getStackAngles } from './Stack.helpers.ts';
7
+
8
+ const STACK = {
9
+ /** How long an item takes to fan out on hover, or to settle back. */
10
+ hoverDuration: 0.45,
11
+ /** A little overshoot, so the fan flicks open and eases back into the pile. */
12
+ hoverEasing: 'cubic-bezier(0.34, 1.3, 0.64, 1)',
13
+ };
14
+
15
+ /** Adjustments to one item of a stack, bottom to top. */
16
+ export interface StackItem {
17
+ /** Its lean in degrees (clockwise positive), in place of the seeded one. */
18
+ rotate?: number;
19
+ /** Pixels to shift it right (negative: left) and down (negative: up), before the lean. */
20
+ x?: number;
21
+ y?: number;
22
+ /**
23
+ * How it fans out while the stack is hovered: degrees and pixels added to its resting lean
24
+ * and shift, turning about the pin (or the stack's `hoverOriginX` / `hoverOriginY`).
25
+ */
26
+ hover?: { rotate?: number; x?: number; y?: number };
27
+ }
28
+
29
+ /*
30
+ * A pile of items laid over one another, each turned by a small, varying angle about the same
31
+ * pivot, like cards or photos dropped on a table: the first child at the bottom, the last on
32
+ * top. Every child sits in the same grid cell, so the stack is as big as its biggest item
33
+ * and needs no sizing of its own; smaller items are placed in that box per `alignX` /
34
+ * `alignY` (centered by default). The pivot (`originX` / `originY`, CSS lengths or
35
+ * percentages of the stack's box, its center by default) is where the pin through the pile
36
+ * would be, and is the same point for every item whatever its size. The angles come from
37
+ * `seed` within `±spread` degrees, so they are stable across renders and on the server;
38
+ * `items` sets any item's angle outright and shifts it by pixels, bottom to top.
39
+ *
40
+ * An item with a `hover` entry fans out while the pointer is over the pile, turning and
41
+ * shifting further from its resting place, and settles back when the pointer leaves. It turns
42
+ * about the pin itself, wherever the item was shifted to; or, given `hoverOriginX` /
43
+ * `hoverOriginY`, about that point of the stack's box carried along with the item (so `'0%'`
44
+ * hinges every item at its own left edge). The pile is unselectable and ignores clicks, so
45
+ * the items read as artwork, not controls.
46
+ */
47
+ export const Stack = memo(
48
+ (p: {
49
+ children?: ReactNode;
50
+ /** Largest lean in degrees either way; 0 stacks the items square. */
51
+ spread?: number;
52
+ /** Where an item smaller than the stack's box sits in it. */
53
+ alignX?: AlignX;
54
+ alignY?: AlignY;
55
+ /** The pivot the items turn about, as CSS values (`'50%'`, `12`, `'100%'`) in the stack's box. */
56
+ originX?: number | string;
57
+ originY?: number | string;
58
+ /** The pivot the items fan about on hover, in the same terms but riding with each item's shift; the pin itself unless given. */
59
+ hoverOriginX?: number | string;
60
+ hoverOriginY?: number | string;
61
+ /** Stable source for the angles, so re-renders never shuffle the pile. */
62
+ seed?: number;
63
+ /** Per-child adjustments, bottom to top; a missing entry or field keeps the seeded lean and no shift. */
64
+ items?: (StackItem | null | undefined)[];
65
+ className?: string;
66
+ style?: CSSProperties;
67
+ }) => {
68
+ const {
69
+ children,
70
+ spread = 6,
71
+ alignX = 'center',
72
+ alignY = 'center',
73
+ originX = '50%',
74
+ originY = '50%',
75
+ hoverOriginX,
76
+ hoverOriginY,
77
+ seed = 1,
78
+ items,
79
+ className,
80
+ style,
81
+ } = p;
82
+ const elements = Children.toArray(children);
83
+ const leans = useMemo(() => getStackAngles(elements.length, spread, seed), [elements.length, spread, seed]);
84
+ const origin = `${cssLength(originX)} ${cssLength(originY)}`;
85
+ const hoverOrigin =
86
+ hoverOriginX != null || hoverOriginY != null
87
+ ? `${cssLength(hoverOriginX ?? originX)} ${cssLength(hoverOriginY ?? originY)}`
88
+ : null;
89
+ const fans = items?.some((item) => item?.hover) ?? false;
90
+ const [hovered, setHovered] = useState(false);
91
+
92
+ return (
93
+ <div
94
+ // `noclick` also blanks pointer events on the items, so a pile that fans gets them back to be hoverable.
95
+ className={cn('rel unsel noclick', fans && 'pe_auto', className)}
96
+ style={{ display: 'grid', ...style }}
97
+ onPointerEnter={fans ? () => setHovered(true) : undefined}
98
+ onPointerLeave={fans ? () => setHovered(false) : undefined}
99
+ >
100
+ {/* Each wrapper fills the whole cell (so the pivots are points of the stack, not of the item); its inner one fans the item and places it inside. */}
101
+ {elements.map((element, i) => {
102
+ const item = items?.[i];
103
+ const shift = item?.x || item?.y ? `translate(${item.x ?? 0}px, ${item.y ?? 0}px) ` : '';
104
+ const lean = item?.rotate ?? leans[i];
105
+ const fan = item?.hover;
106
+ return (
107
+ <div
108
+ key={isValidElement(element) && element.key !== null ? element.key : i}
109
+ className={fans ? 'pe_auto' : undefined}
110
+ style={{
111
+ gridArea: '1 / 1',
112
+ display: 'grid',
113
+ transformOrigin: origin,
114
+ transform: `${shift}rotate(${lean}deg)`,
115
+ }}
116
+ >
117
+ <div
118
+ style={{
119
+ display: 'flex',
120
+ justifyContent: ALIGN_TO_FLEX[alignX],
121
+ alignItems: ALIGN_TO_FLEX[alignY],
122
+ transformOrigin:
123
+ hoverOrigin ?? getPinnedOrigin(cssLength(originX), cssLength(originY), item?.x ?? 0, item?.y ?? 0, lean),
124
+ transform: fan && hovered ? `translate(${fan.x ?? 0}px, ${fan.y ?? 0}px) rotate(${fan.rotate ?? 0}deg)` : undefined,
125
+ transition: fan ? `transform ${STACK.hoverDuration}s ${STACK.hoverEasing}` : undefined,
126
+ }}
127
+ >
128
+ {element}
129
+ </div>
130
+ </div>
131
+ );
132
+ })}
133
+ </div>
134
+ );
135
+ },
136
+ );
137
+
138
+ /*
139
+ * A CSS length: a number is pixels, a string is used as given.
140
+ */
141
+ function cssLength(value: number | string) {
142
+ return typeof value === 'number' ? `${value}px` : value;
143
+ }
@@ -0,0 +1,45 @@
1
+ import type { CSSProperties } from 'react';
2
+
3
+ /**
4
+ * Constants
5
+ */
6
+
7
+ /** Where the paint mask sits when nothing is painted yet, and when all of it is. */
8
+ export const PAINT_MASK_POSITION = { unpainted: '100% 0%', painted: '0% 0%' };
9
+
10
+ /**
11
+ * Helpers
12
+ */
13
+
14
+ /*
15
+ * The static mask that reveals a painted copy of text as it sweeps left to right. The mask is
16
+ * a gradient `2 + feather` times the element's width: an opaque stretch one width wide, a
17
+ * fade `feather` widths wide (the soft edge of the brush; 0 is a hard edge), then a
18
+ * transparent stretch one width wide. At mask position 100% only the transparent stretch
19
+ * covers the element, so none of it shows; sliding to 0% drags the opaque stretch across it.
20
+ */
21
+ export function paintMaskStyle(feather: number): CSSProperties {
22
+ const f = Math.max(0, feather);
23
+ const opaqueEnd = 100 / (2 + f);
24
+ const fadeEnd = (100 * (1 + f)) / (2 + f);
25
+ const image = `linear-gradient(90deg, #000 ${opaqueEnd}%, transparent ${fadeEnd}%)`;
26
+ const size = `${100 * (2 + f)}% 100%`;
27
+ return {
28
+ maskImage: image,
29
+ WebkitMaskImage: image,
30
+ maskSize: size,
31
+ WebkitMaskSize: size,
32
+ maskRepeat: 'no-repeat',
33
+ WebkitMaskRepeat: 'no-repeat',
34
+ maskPosition: PAINT_MASK_POSITION.unpainted,
35
+ WebkitMaskPosition: PAINT_MASK_POSITION.unpainted,
36
+ };
37
+ }
38
+
39
+ /*
40
+ * Pin an element's paint mask at a position, as the resting state after (or instead of) a sweep.
41
+ */
42
+ export function setPaintMaskPosition(el: HTMLElement, position: string) {
43
+ el.style.maskPosition = position;
44
+ el.style.webkitMaskPosition = position;
45
+ }
@@ -0,0 +1,197 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import type { RefObject } from 'react';
3
+ import { prefersReducedMotion } from '../utils/motion.ts';
4
+ import { PAINT_MASK_POSITION, setPaintMaskPosition } from './Text.helpers.ts';
5
+
6
+ /**
7
+ * Types
8
+ */
9
+
10
+ /** How fast the typing runs, in milliseconds per character, and the pause between deleting and typing. */
11
+ export interface TypedTextTiming {
12
+ /** Average time to type one character; a human's pace by default. */
13
+ typeMs?: number;
14
+ /** Time to delete one character, as a held backspace repeats; 0 (the default) clears the text at once. */
15
+ deleteMs?: number;
16
+ /** Pause between the last deletion and the first new character. */
17
+ pauseMs?: number;
18
+ /** 0..1 how unevenly the characters come: each waits `typeMs` varied by up to this fraction either way, so the typing does not tick like a clock. */
19
+ jitter?: number;
20
+ }
21
+
22
+ /**
23
+ * Constants
24
+ */
25
+
26
+ export const TYPED_TEXT_DEFAULTS: Required<TypedTextTiming> = { typeMs: 48, deleteMs: 0, pauseMs: 300, jitter: 0.6 };
27
+
28
+ /*
29
+ * The text currently on screen as it is retyped toward `target`, and whether that is under
30
+ * way. The text mounts as it is; on a change the shown text is deleted (at once, or a
31
+ * character at a time with a `deleteMs`), then the new one typed a character at a time. A run
32
+ * always finishes: a target that arrives mid-run (the page scrolled back before the retype was
33
+ * over) is taken up only once the current one has been typed out in full, and only the latest
34
+ * target counts then, so any number of changes settle in one more run at most. Reduced motion
35
+ * swaps the text at once. `typeOnMount` starts empty; changing `replayKey` retypes even an
36
+ * identical target, so consecutive equal words in an ordered sequence still animate.
37
+ * `interruptOnChange` cancels an unfinished run when its target or replay key changes.
38
+ */
39
+ export function useTypedText(
40
+ target: string,
41
+ timing: TypedTextTiming = {},
42
+ options: { typeOnMount?: boolean; replayKey?: number; interruptOnChange?: boolean } = {},
43
+ ) {
44
+ const [shown, setShown] = useState(options.typeOnMount ? '' : target);
45
+ const [typing, setTyping] = useState(false);
46
+ const replayKey = options.replayKey;
47
+ const interruptOnChange = options.interruptOnChange ?? false;
48
+ const previousReplayKey = useRef(replayKey);
49
+ // What a run reads as it goes: the latest target and timing, and the timer of its next keystroke.
50
+ const latest = useRef({ target, replayKey, timing: { ...TYPED_TEXT_DEFAULTS } });
51
+ const running = useRef(false);
52
+ const timer = useRef<number | null>(null);
53
+
54
+ useEffect(() => {
55
+ latest.current.target = target;
56
+ latest.current.replayKey = replayKey;
57
+ // Field by field, so an option passed as `undefined` still gets its default.
58
+ for (const key of Object.keys(TYPED_TEXT_DEFAULTS) as (keyof TypedTextTiming)[]) {
59
+ latest.current.timing[key] = timing[key] ?? TYPED_TEXT_DEFAULTS[key];
60
+ }
61
+ });
62
+
63
+ // A run reads `shown` once when it starts and owns it after; a new target or replay key triggers it.
64
+ useEffect(() => {
65
+ if (running.current || (target === shown && replayKey === previousReplayKey.current)) return;
66
+ previousReplayKey.current = replayKey;
67
+ if (prefersReducedMotion()) {
68
+ setShown(target);
69
+ setTyping(false);
70
+ return;
71
+ }
72
+ running.current = true;
73
+ setTyping(true);
74
+ const wait = (ms: number, next: () => void) => {
75
+ timer.current = window.setTimeout(next, ms);
76
+ };
77
+ // One run: delete what is shown, pause, type the goal; then look at the latest target again.
78
+ const run = (from: string, goal: string) => {
79
+ previousReplayKey.current = latest.current.replayKey;
80
+ let text = from;
81
+ const erase = () => {
82
+ const { deleteMs, pauseMs } = latest.current.timing;
83
+ if (!text) {
84
+ wait(pauseMs, type);
85
+ return;
86
+ }
87
+ text = deleteMs > 0 ? text.slice(0, -1) : '';
88
+ setShown(text);
89
+ wait(deleteMs, erase);
90
+ };
91
+ const type = () => {
92
+ if (text.length === goal.length) {
93
+ finish();
94
+ return;
95
+ }
96
+ text = goal.slice(0, text.length + 1);
97
+ setShown(text);
98
+ // Unevenly, like fingers, with the longest pause at a word's space.
99
+ const { typeMs, jitter } = latest.current.timing;
100
+ const pace = 1 + jitter * (Math.random() * 2 - 1);
101
+ wait(typeMs * pace * (text.endsWith(' ') ? 1.5 : 1), type);
102
+ };
103
+ const finish = () => {
104
+ timer.current = null;
105
+ if (latest.current.target !== goal || latest.current.replayKey !== previousReplayKey.current) {
106
+ run(goal, latest.current.target);
107
+ return;
108
+ }
109
+ running.current = false;
110
+ setTyping(false);
111
+ };
112
+ erase();
113
+ };
114
+ run(shown, target);
115
+ if (interruptOnChange) {
116
+ return () => {
117
+ if (timer.current !== null) clearTimeout(timer.current);
118
+ timer.current = null;
119
+ running.current = false;
120
+ };
121
+ }
122
+ }, [target, replayKey, interruptOnChange]);
123
+
124
+ // End the run along with its timer, so a remount (Strict Mode, Fast Refresh) retypes from what
125
+ // is shown instead of waiting forever on a run that will never finish.
126
+ useEffect(
127
+ () => () => {
128
+ if (timer.current !== null) clearTimeout(timer.current);
129
+ timer.current = null;
130
+ running.current = false;
131
+ },
132
+ [],
133
+ );
134
+
135
+ return { shown, typing };
136
+ }
137
+
138
+ /**
139
+ * Painted text
140
+ */
141
+
142
+ /** How the paint sweeps across the text. */
143
+ export interface PaintedTextTiming {
144
+ /** How long the sweep takes; 0 paints at once. */
145
+ durationMs?: number;
146
+ /** Wait after the text comes into view before the sweep starts. */
147
+ delayMs?: number;
148
+ /** CSS easing of the sweep. */
149
+ easing?: string;
150
+ /** Softness of the brush edge as a fraction of the text's width; 0 is a hard edge. */
151
+ feather?: number;
152
+ }
153
+
154
+ export const PAINTED_TEXT_DEFAULTS: Required<PaintedTextTiming> = {
155
+ durationMs: 3000,
156
+ delayMs: 0,
157
+ easing: 'cubic-bezier(0.4, 0, 0.2, 1)',
158
+ feather: 0.35,
159
+ };
160
+
161
+ /*
162
+ * Sweep an element's paint mask (from `paintMaskStyle`) across it when `painted` turns true,
163
+ * and pin it back off screen when it turns false. The sweep is a Web Animation on the mask
164
+ * position; its end state is committed to the inline style and the animation dropped, so
165
+ * nothing lingers. Reduced motion, a zero duration, or a browser without `animate` paint at
166
+ * once. A change mid-sweep cancels it, so toggling settles on the latest value.
167
+ */
168
+ export function usePaintSweep(ref: RefObject<HTMLElement | null>, painted: boolean, timing: PaintedTextTiming = {}) {
169
+ const {
170
+ durationMs = PAINTED_TEXT_DEFAULTS.durationMs,
171
+ delayMs = PAINTED_TEXT_DEFAULTS.delayMs,
172
+ easing = PAINTED_TEXT_DEFAULTS.easing,
173
+ } = timing;
174
+
175
+ useEffect(() => {
176
+ const el = ref.current;
177
+ if (!el) return;
178
+ const to = painted ? PAINT_MASK_POSITION.painted : PAINT_MASK_POSITION.unpainted;
179
+ if (!painted || durationMs <= 0 || prefersReducedMotion() || typeof el.animate !== 'function') {
180
+ setPaintMaskPosition(el, to);
181
+ return;
182
+ }
183
+ const { unpainted } = PAINT_MASK_POSITION;
184
+ const sweep = el.animate(
185
+ [
186
+ { maskPosition: unpainted, webkitMaskPosition: unpainted },
187
+ { maskPosition: to, webkitMaskPosition: to },
188
+ ],
189
+ { duration: durationMs, delay: delayMs, easing, fill: 'forwards' },
190
+ );
191
+ sweep.onfinish = () => {
192
+ setPaintMaskPosition(el, to);
193
+ sweep.cancel();
194
+ };
195
+ return () => sweep.cancel();
196
+ }, [ref, painted, durationMs, delayMs, easing]);
197
+ }
@@ -0,0 +1,98 @@
1
+ import { cn } from '@jsb188/app/utils/string.ts';
2
+ import { memo, useRef } from 'react';
3
+ import type { CSSProperties } from 'react';
4
+ import { useInView } from '../modules/ScrollSteps.hooks.ts';
5
+ import { paintMaskStyle } from './Text.helpers.ts';
6
+ import { PAINTED_TEXT_DEFAULTS, usePaintSweep, useTypedText } from './Text.hooks.ts';
7
+ import type { PaintedTextTiming, TypedTextTiming } from './Text.hooks.ts';
8
+
9
+ export { PAINTED_TEXT_DEFAULTS, TYPED_TEXT_DEFAULTS, usePaintSweep, useTypedText } from './Text.hooks.ts';
10
+ export type { PaintedTextTiming, TypedTextTiming } from './Text.hooks.ts';
11
+
12
+ /*
13
+ * Text that is retyped whenever it changes: what is shown is cleared (or deleted a character
14
+ * at a time, with a `deleteMs`), then the new text typed a character at a time at a human's
15
+ * uneven pace, with a caret at the end while that runs.
16
+ * It mounts already showing `text`. A change that arrives mid-retype waits its turn: the
17
+ * running one is typed out in full first, and then only the latest text is typed, so however
18
+ * often `text` flips back and forth the label settles on the last value in one more retype.
19
+ */
20
+ export const TypedText = memo(
21
+ (
22
+ p: TypedTextTiming & {
23
+ text: string;
24
+ /** Show a caret after the text while it is being retyped (default); `null` for none, or any string to use as the caret. */
25
+ caret?: string | null;
26
+ /** Extra class while the text is being retyped. */
27
+ typingClassName?: string;
28
+ className?: string;
29
+ style?: CSSProperties;
30
+ },
31
+ ) => {
32
+ const { text, caret = '|', typingClassName, className, style, typeMs, deleteMs, pauseMs, jitter } = p;
33
+ const { shown, typing } = useTypedText(text, { typeMs, deleteMs, pauseMs, jitter });
34
+
35
+ return (
36
+ <span
37
+ className={cn(className, typing && typingClassName)}
38
+ style={style}
39
+ >
40
+ {shown}
41
+ {caret !== null && typing && <span aria-hidden>{caret}</span>}
42
+ </span>
43
+ );
44
+ },
45
+ );
46
+
47
+ /*
48
+ * Text that is painted a second color, left to right, when it scrolls into view. The text is
49
+ * rendered twice in one grid cell: a base copy in the inherited color, and a copy in
50
+ * `paintClassName` on top of it, revealed by a soft-edged mask that sweeps across (so it
51
+ * wraps exactly like the base). Until the sweep, and on the server, only the base shows.
52
+ * Painted once by default and left that way; `once={false}` unpaints it when it leaves the
53
+ * viewport and paints it again on return. `block` makes the wrapper a block box, so text
54
+ * after it starts on a new line; otherwise it flows inline like a span.
55
+ */
56
+ export const PaintedText = memo(
57
+ (
58
+ p: PaintedTextTiming & {
59
+ text: string;
60
+ /** Class of the painted copy, which sets its color (`cl_primary`, say). */
61
+ paintClassName: string;
62
+ /** Wrapper as a block box rather than an inline one. */
63
+ block?: boolean;
64
+ /** Paint only the first time it comes into view (default), or every time. */
65
+ once?: boolean;
66
+ /** Passed to the viewport observer: how much of it must show (all of it by default), and how far outside the viewport counts. */
67
+ threshold?: number;
68
+ rootMargin?: string;
69
+ className?: string;
70
+ style?: CSSProperties;
71
+ },
72
+ ) => {
73
+ const { text, paintClassName, block, once = true, threshold = 1, rootMargin, className, style } = p;
74
+ const { durationMs, delayMs, easing, feather = PAINTED_TEXT_DEFAULTS.feather } = p;
75
+ const wrapperRef = useRef<HTMLSpanElement>(null);
76
+ const paintRef = useRef<HTMLSpanElement>(null);
77
+ const inView = useInView(wrapperRef, { once, threshold, rootMargin });
78
+ usePaintSweep(paintRef, inView, { durationMs, delayMs, easing });
79
+
80
+ return (
81
+ <span
82
+ ref={wrapperRef}
83
+ className={className}
84
+ style={{ display: block ? 'grid' : 'inline-grid', ...style }}
85
+ >
86
+ <span style={{ gridArea: '1 / 1' }}>{text}</span>
87
+ <span
88
+ ref={paintRef}
89
+ aria-hidden
90
+ className={paintClassName}
91
+ style={{ gridArea: '1 / 1', ...paintMaskStyle(feather) }}
92
+ >
93
+ {text}
94
+ </span>
95
+ </span>
96
+ );
97
+ },
98
+ );
@@ -0,0 +1,53 @@
1
+ import type { DotEffectSelection } from '../utils/dotEffectSpecs.ts';
2
+ import { DOT_EFFECTS, getDotEffectId } from '../utils/dotEffectSpecs.ts';
3
+
4
+ /** Maximum simultaneous click effects; another click replaces the oldest. */
5
+ export const DOT_CLICK_MAX_EFFECTS = 8;
6
+
7
+ /*
8
+ * Keep click origins, start times, and chosen effect IDs in a fixed GPU uniform buffer (x, y, seconds, active).
9
+ * Repeated clicks overlap without allocating per frame or growing the buffer indefinitely.
10
+ */
11
+ export function createDotClickEffects() {
12
+ // OGL recognizes uniform arrays through Array.isArray, so this must stay a plain array.
13
+ const data: number[] = Array(DOT_CLICK_MAX_EFFECTS * 4).fill(0);
14
+ const effects: number[] = Array(DOT_CLICK_MAX_EFFECTS).fill(0);
15
+ let next = 0;
16
+
17
+ /*
18
+ * Record a click, replacing the oldest when full, and return its shader ID. Random is chosen once here.
19
+ */
20
+ function add(x: number, y: number, seconds: number, effect: DotEffectSelection) {
21
+ const offset = next * 4;
22
+ data[offset] = x;
23
+ data[offset + 1] = y;
24
+ data[offset + 2] = seconds;
25
+ data[offset + 3] = 1;
26
+ if (effect === 'random') {
27
+ // The same board click selects the same effect in every scrolling and sticky layer.
28
+ let seed = Math.round(seconds * 1000) | 0;
29
+ seed = Math.imul(seed ^ (seed >>> 16), 0x45d9f3b);
30
+ seed = Math.imul(seed ^ (seed >>> 16), 0x45d9f3b);
31
+ effects[next] = ((seed ^ (seed >>> 16)) >>> 0) % DOT_EFFECTS.length;
32
+ } else {
33
+ effects[next] = getDotEffectId(effect);
34
+ }
35
+ const selectedEffect = effects[next];
36
+ next = (next + 1) % DOT_CLICK_MAX_EFFECTS;
37
+ return selectedEffect;
38
+ }
39
+
40
+ /*
41
+ * Expire completed effects and report whether more frames are needed; duration 0 clears all.
42
+ */
43
+ function update(seconds: number, duration: number) {
44
+ let active = false;
45
+ for (let i = 0; i < data.length; i += 4) {
46
+ if (duration <= 0 || seconds - data[i + 2] >= duration) data[i + 3] = 0;
47
+ if (data[i + 3] > 0) active = true;
48
+ }
49
+ return active;
50
+ }
51
+
52
+ return { data, effects, add, update };
53
+ }