@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,171 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import type { RefObject } from 'react';
|
|
3
|
+
import { prefersReducedMotion } from '../utils/motion.ts';
|
|
4
|
+
import { pulseHaloKeyframes } from './Shapes.helpers.ts';
|
|
5
|
+
import type { ShapeState } from './Shapes.types.ts';
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* Whether a reveal may start: `false` until `seen` has been true for `delayMs`, then `true`
|
|
9
|
+
* for good, so a shape that has scrolled into view waits a beat before it paints in.
|
|
10
|
+
*/
|
|
11
|
+
export function useRevealed(seen: boolean, delayMs: number) {
|
|
12
|
+
const [revealed, setRevealed] = useState(seen && delayMs <= 0);
|
|
13
|
+
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (!seen || revealed) return;
|
|
16
|
+
if (delayMs <= 0) {
|
|
17
|
+
setRevealed(true);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const timer = setTimeout(() => setRevealed(true), delayMs);
|
|
21
|
+
return () => clearTimeout(timer);
|
|
22
|
+
}, [seen, revealed, delayMs]);
|
|
23
|
+
|
|
24
|
+
return revealed;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The states in painting order: each step from one to the next paints one layer in, or back out. */
|
|
28
|
+
export const SHAPE_STATES: ShapeState[] = ['empty', 'base', 'active'];
|
|
29
|
+
|
|
30
|
+
/*
|
|
31
|
+
* The state a shape is currently painted toward, walking one step at a time toward `target`,
|
|
32
|
+
* and whether a step is under way: a step starts a layer's sweep and holds for `stepMs`, and
|
|
33
|
+
* only then is the (latest) target looked at again, so a sweep always finishes before the
|
|
34
|
+
* next begins, whichever way the target moved in the meantime. With `startEmpty` the walk
|
|
35
|
+
* starts from `empty`; otherwise the shape mounts already at `target`. `ref` is the element
|
|
36
|
+
* the sweeps run on; its style is flushed before each step so the transition has a starting
|
|
37
|
+
* value to leave from. With `animate` off the shape simply shows `target`, never stepping.
|
|
38
|
+
*/
|
|
39
|
+
export function useShapeState(
|
|
40
|
+
target: ShapeState,
|
|
41
|
+
stepMs: number,
|
|
42
|
+
animate: boolean,
|
|
43
|
+
startEmpty: boolean,
|
|
44
|
+
ref: RefObject<Element | null>,
|
|
45
|
+
) {
|
|
46
|
+
const [shown, setShown] = useState<ShapeState>(startEmpty ? 'empty' : target);
|
|
47
|
+
const [animating, setAnimating] = useState(false);
|
|
48
|
+
// What the running step will read when it lands, and the step's timer.
|
|
49
|
+
const latest = useRef({ target, stepMs, shown });
|
|
50
|
+
const timer = useRef<number | null>(null);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
latest.current.target = target;
|
|
54
|
+
latest.current.stepMs = stepMs;
|
|
55
|
+
}, [target, stepMs]);
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (!animate) {
|
|
59
|
+
latest.current.shown = target;
|
|
60
|
+
setShown(target);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// A step is under way; its timer takes the next one.
|
|
64
|
+
if (timer.current !== null) return;
|
|
65
|
+
const step = () => {
|
|
66
|
+
const { target: want, shown: from, stepMs: hold } = latest.current;
|
|
67
|
+
if (from === want) {
|
|
68
|
+
setAnimating(false);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const at = SHAPE_STATES.indexOf(from);
|
|
72
|
+
const next = SHAPE_STATES[at + (SHAPE_STATES.indexOf(want) > at ? 1 : -1)];
|
|
73
|
+
// Commit the current state's style (on mount it may never have been painted), so the sweep transitions from it.
|
|
74
|
+
void ref.current?.getBoundingClientRect();
|
|
75
|
+
latest.current.shown = next;
|
|
76
|
+
setShown(next);
|
|
77
|
+
setAnimating(true);
|
|
78
|
+
timer.current = window.setTimeout(() => {
|
|
79
|
+
timer.current = null;
|
|
80
|
+
step();
|
|
81
|
+
}, hold);
|
|
82
|
+
};
|
|
83
|
+
step();
|
|
84
|
+
}, [target, animate]);
|
|
85
|
+
|
|
86
|
+
// Forget the step's timer along with clearing it, so a remount (Strict Mode, Fast Refresh) walks on
|
|
87
|
+
// from `shown` instead of waiting forever for a timer that will never fire.
|
|
88
|
+
useEffect(
|
|
89
|
+
() => () => {
|
|
90
|
+
if (timer.current !== null) clearTimeout(timer.current);
|
|
91
|
+
timer.current = null;
|
|
92
|
+
},
|
|
93
|
+
[],
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
return { shown, animating };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/*
|
|
100
|
+
* Runs a `<PulsingMarker>`'s halos as Web Animations: each grows from the dot's `size` to
|
|
101
|
+
* `pulseSize` over `durationMs` and fades out, the halos staggered evenly through the cycle,
|
|
102
|
+
* over and over. While `held`, no new halo starts: each one finishes the cycle it is in, and
|
|
103
|
+
* then the pulse is still. Released, the pulse carries on: a halo still
|
|
104
|
+
* finishing its cycle simply keeps going, and the others wait for their turn at the usual
|
|
105
|
+
* spacing, so none pops in mid-growth.
|
|
106
|
+
*/
|
|
107
|
+
export function usePulseHalos(
|
|
108
|
+
halosRef: RefObject<(HTMLElement | null)[]>,
|
|
109
|
+
size: number,
|
|
110
|
+
pulseSize: number,
|
|
111
|
+
durationMs: number,
|
|
112
|
+
easing: string,
|
|
113
|
+
held: boolean,
|
|
114
|
+
) {
|
|
115
|
+
const loopsRef = useRef<(Animation | null)[]>([]);
|
|
116
|
+
// Whether the pulse was stopped, so only a release (not the first render) restarts it.
|
|
117
|
+
const heldRef = useRef(false);
|
|
118
|
+
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (prefersReducedMotion()) return;
|
|
121
|
+
const halos = halosRef.current;
|
|
122
|
+
loopsRef.current = halos.map((el, i) => {
|
|
123
|
+
if (!el || typeof el.animate !== 'function') return null;
|
|
124
|
+
const animation = el.animate(pulseHaloKeyframes(size, pulseSize), { duration: durationMs, iterations: Infinity, easing });
|
|
125
|
+
// Each halo starts its turn a share of the cycle after the last; before it, the halo's hidden base style shows.
|
|
126
|
+
animation.currentTime = (-i * durationMs) / halos.length;
|
|
127
|
+
return animation;
|
|
128
|
+
});
|
|
129
|
+
return () => {
|
|
130
|
+
for (const animation of loopsRef.current) animation?.cancel();
|
|
131
|
+
loopsRef.current = [];
|
|
132
|
+
};
|
|
133
|
+
}, [size, pulseSize, durationMs, easing]);
|
|
134
|
+
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
const loops = loopsRef.current;
|
|
137
|
+
if (!loops.some(Boolean)) return;
|
|
138
|
+
|
|
139
|
+
const wasHeld = heldRef.current;
|
|
140
|
+
heldRef.current = held;
|
|
141
|
+
if (held) {
|
|
142
|
+
// Each halo ends after the cycle it is in; one still waiting for its first turn never starts.
|
|
143
|
+
for (const loop of loops) {
|
|
144
|
+
if (!loop) continue;
|
|
145
|
+
const time = Number(loop.currentTime ?? 0);
|
|
146
|
+
if (time < 0) loop.pause();
|
|
147
|
+
else loop.effect?.updateTiming({ iterations: Math.floor(time / durationMs) + 1 });
|
|
148
|
+
}
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (!wasHeld) return;
|
|
153
|
+
const count = loops.length;
|
|
154
|
+
const isRunning = (loop: Animation | null) => !!loop && loop.playState === 'running' && Number(loop.currentTime) >= 0;
|
|
155
|
+
// Where the first halo would be in the pulse: kept from a halo still finishing its cycle, or starting afresh.
|
|
156
|
+
const runningAt = loops.findIndex(isRunning);
|
|
157
|
+
const base = runningAt < 0 ? 0 : Number(loops[runningAt]?.currentTime) + (runningAt * durationMs) / count;
|
|
158
|
+
const running = loops.map(isRunning);
|
|
159
|
+
loops.forEach((loop, i) => {
|
|
160
|
+
if (!loop) return;
|
|
161
|
+
loop.effect?.updateTiming({ iterations: Infinity });
|
|
162
|
+
// Played first: `play()` rewinds a time before the start to 0.
|
|
163
|
+
loop.play();
|
|
164
|
+
if (!running[i]) {
|
|
165
|
+
// Wait (before its active phase, so hidden) until its place in the spacing next comes round to the start of a cycle.
|
|
166
|
+
const phase = (((base - (i * durationMs) / count) % durationMs) + durationMs) % durationMs;
|
|
167
|
+
loop.currentTime = phase === 0 ? 0 : phase - durationMs;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}, [held]);
|
|
171
|
+
}
|
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
import { cn } from '@jsb188/app/utils/string.ts';
|
|
2
|
+
import { memo, useId, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import type { CSSProperties, ReactNode } from 'react';
|
|
4
|
+
import { useInView } from '../modules/ScrollSteps.hooks.ts';
|
|
5
|
+
import { shiftHex } from '../utils/color.ts';
|
|
6
|
+
import { createRandom } from '../utils/random.ts';
|
|
7
|
+
import { brushPath, brushRows, brushWidth, pixelateCells, pixelateTile, shapeOutline } from './Shapes.helpers.ts';
|
|
8
|
+
import { SHAPE_STATES, usePulseHalos, useRevealed, useShapeState } from './Shapes.hooks.ts';
|
|
9
|
+
import type { ShapeEffect, ShapeProps } from './Shapes.types.ts';
|
|
10
|
+
|
|
11
|
+
export type { ShapeEffect, ShapeIcon, ShapeKind, ShapeLineStyle, ShapeProps, ShapeState } from './Shapes.types.ts';
|
|
12
|
+
|
|
13
|
+
const SHAPE = {
|
|
14
|
+
/** Brush passes across a square shape box; more rows = thinner, more visible strokes. */
|
|
15
|
+
brushRows: 5,
|
|
16
|
+
/** Cells per side of the repeating pixelate tile, and how far (0..1 of full white or black) a cell may shade from the paint. */
|
|
17
|
+
pixelTile: 8,
|
|
18
|
+
pixelShade: 0.14,
|
|
19
|
+
/** Hatch streaks: about how many rows of them span the shape's height, how many times longer than tall they run, and the noise threshold they are cut at from intensity 0 to 1 (lower cuts more). */
|
|
20
|
+
hatchRows: 28,
|
|
21
|
+
hatchStretch: 40,
|
|
22
|
+
hatchThreshold: [0.72, 0.5],
|
|
23
|
+
/** Sweep timing in seconds and its easing. */
|
|
24
|
+
paintDuration: 1.3,
|
|
25
|
+
easing: 'cubic-bezier(0.4, 0, 0.2, 1)',
|
|
26
|
+
defaultEffect: { name: 'grain', intensity: 0.6 } as ShapeEffect,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/*
|
|
30
|
+
* A painted illustration shape in two layers, each laid down as one hand-swept brush stroke
|
|
31
|
+
* with a texture `effect` (film grain, a pixelate mosaic of cells a shade lighter or darker
|
|
32
|
+
* than the paint, which sweeps in with it, or the ragged streaks of a woodcut print): a flat
|
|
33
|
+
* `baseColor` base, and over it the active `color` as a gradient (or flat; always flat under a
|
|
34
|
+
* mosaic). The shape is a built-in one or a custom icon path. `state` says which layers show:
|
|
35
|
+
* `empty`, `base`, or `active`. A state change sweeps one layer in (or out again, going the
|
|
36
|
+
* other way); a change that skips a state, or arrives mid-sweep, waits for the running sweep
|
|
37
|
+
* and then takes the next step, so a shape sent from empty to active always finishes painting
|
|
38
|
+
* its base before the active layer starts. The edge is `rounded`, `straight`, or a hand-drawn
|
|
39
|
+
* `sketch` wobble, and the shape is drawn inside `size` × `height` (square unless `height` is
|
|
40
|
+
* given) with a `padding` margin (10% a side by default) so the wobble, brush overspill and
|
|
41
|
+
* filters are not clipped; `padding: 0` makes those the painted size. With `reveal` the shape
|
|
42
|
+
* starts empty and paints up to `state` on mount, or once it has scrolled into view (and
|
|
43
|
+
* stays painted from then on). Every wobble comes from `seed`, so the same props always give
|
|
44
|
+
* the same picture, on the server too.
|
|
45
|
+
*/
|
|
46
|
+
export const Shape = memo((p: ShapeProps) => {
|
|
47
|
+
const {
|
|
48
|
+
shape,
|
|
49
|
+
state,
|
|
50
|
+
baseColor,
|
|
51
|
+
color,
|
|
52
|
+
size = 160,
|
|
53
|
+
height = size,
|
|
54
|
+
padding = 0.1,
|
|
55
|
+
line = 'rounded',
|
|
56
|
+
effect = SHAPE.defaultEffect,
|
|
57
|
+
gradient = true,
|
|
58
|
+
animate = true,
|
|
59
|
+
reveal,
|
|
60
|
+
delay = 0,
|
|
61
|
+
revealDelay = 0,
|
|
62
|
+
seed = 1,
|
|
63
|
+
className,
|
|
64
|
+
stateClassName,
|
|
65
|
+
style,
|
|
66
|
+
} = p;
|
|
67
|
+
const id = useId().replace(/[^a-zA-Z0-9]/g, '');
|
|
68
|
+
const ids = { gradient: `${id}g`, grain: `${id}n`, hatch: `${id}h`, pixel: `${id}p`, rough: `${id}r`, clip: `${id}c` };
|
|
69
|
+
const intensity = effect ? Math.min(1, Math.max(0, effect.intensity)) : 0;
|
|
70
|
+
const grain = effect?.name === 'grain' ? intensity : 0;
|
|
71
|
+
const hatch = effect?.name === 'hatch' ? intensity : 0;
|
|
72
|
+
const pixelate = effect?.name === 'pixelate';
|
|
73
|
+
const sketch = line === 'sketch';
|
|
74
|
+
// A custom icon is keyed by its data, so a fresh object with the same path recomputes nothing.
|
|
75
|
+
const kindKey = typeof shape === 'string' ? shape : `${shape.viewBox ?? 24}|${shape.d}`;
|
|
76
|
+
|
|
77
|
+
// The box the shape is painted in, inset by the padding, and the brush passes it takes.
|
|
78
|
+
const inset = 0.5 - Math.min(0.45, Math.max(0, padding));
|
|
79
|
+
const box = { cx: size / 2, cy: height / 2, rx: size * inset, ry: height * inset };
|
|
80
|
+
const rows = brushRows(box, SHAPE.brushRows);
|
|
81
|
+
// Geometry is seeded, so it only changes when the props that shape it change.
|
|
82
|
+
const geometry = useMemo(() => {
|
|
83
|
+
const random = createRandom(seed);
|
|
84
|
+
return {
|
|
85
|
+
outline: shapeOutline(shape, line, box, random),
|
|
86
|
+
brush: brushPath(box, rows, random),
|
|
87
|
+
pixels: pixelateTile(SHAPE.pixelTile, SHAPE.pixelTile, random),
|
|
88
|
+
};
|
|
89
|
+
}, [kindKey, line, box.cx, box.cy, box.rx, box.ry, rows, seed]);
|
|
90
|
+
// The mosaic: `cell` px squares, in a tile that repeats over the shape.
|
|
91
|
+
const cell = size / pixelateCells(intensity);
|
|
92
|
+
const tile = cell * SHAPE.pixelTile;
|
|
93
|
+
// The streaks' noise: rows of streaks over the height, stretched sideways, cut at a threshold set by the intensity.
|
|
94
|
+
const hatchFrequency = `${SHAPE.hatchRows / height / SHAPE.hatchStretch} ${SHAPE.hatchRows / height}`;
|
|
95
|
+
const hatchCut = SHAPE.hatchThreshold[0] + (SHAPE.hatchThreshold[1] - SHAPE.hatchThreshold[0]) * hatch;
|
|
96
|
+
// Which texture filter the paint goes through, if any.
|
|
97
|
+
const paintFilter = grain > 0 ? ids.grain : hatch > 0 ? ids.hatch : null;
|
|
98
|
+
// Shades are derived only from a hex `color`; any other CSS color (a `var()`, `rgb()`) is used as is.
|
|
99
|
+
const hex = /^#[0-9a-f]{3,8}$/i.test(color);
|
|
100
|
+
const stops = gradient && hex && !pixelate ? [shiftHex(color, -0.12), shiftHex(color, 0.22, -8)] : [color, color];
|
|
101
|
+
|
|
102
|
+
const ref = useRef<SVGSVGElement>(null);
|
|
103
|
+
// Until a `view` reveal has seen the shape (plus `revealDelay`), it is painted toward `empty`; then toward `state`.
|
|
104
|
+
const seen = useInView(ref, { once: true, disabled: reveal !== 'view' }) || reveal !== 'view';
|
|
105
|
+
const revealed = useRevealed(seen, reveal === 'view' ? revealDelay * 1000 : 0);
|
|
106
|
+
const { shown, animating } = useShapeState(revealed ? state : 'empty', (delay + SHAPE.paintDuration) * 1000, animate, !!reveal, ref);
|
|
107
|
+
const baseDrawn = SHAPE_STATES.indexOf(shown) >= 1;
|
|
108
|
+
const activeDrawn = shown === 'active';
|
|
109
|
+
const stateClass = animating && stateClassName?.animating !== undefined ? stateClassName.animating : stateClassName?.[shown];
|
|
110
|
+
|
|
111
|
+
/*
|
|
112
|
+
* A mosaic pattern to paint a layer with: the layer's color under the shaded cells, so the
|
|
113
|
+
* pixels are part of the paint and sweep in with it.
|
|
114
|
+
*/
|
|
115
|
+
const mosaic = (key: string, fill: string) => (
|
|
116
|
+
<pattern
|
|
117
|
+
id={ids.pixel + key}
|
|
118
|
+
patternUnits="userSpaceOnUse"
|
|
119
|
+
width={tile}
|
|
120
|
+
height={tile}
|
|
121
|
+
>
|
|
122
|
+
<rect
|
|
123
|
+
width={tile}
|
|
124
|
+
height={tile}
|
|
125
|
+
style={{ fill }}
|
|
126
|
+
/>
|
|
127
|
+
{geometry.pixels.map(({ col, row, shade }) => (
|
|
128
|
+
<rect
|
|
129
|
+
key={`${col}-${row}`}
|
|
130
|
+
x={col * cell}
|
|
131
|
+
y={row * cell}
|
|
132
|
+
width={cell}
|
|
133
|
+
height={cell}
|
|
134
|
+
fill={shade < 0 ? '#000' : '#fff'}
|
|
135
|
+
opacity={Math.abs(shade) * SHAPE.pixelShade}
|
|
136
|
+
/>
|
|
137
|
+
))}
|
|
138
|
+
</pattern>
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
/*
|
|
142
|
+
* One layer: the brush stroke clipped to the shape, drawn in (or out) by its dash offset,
|
|
143
|
+
* which transitions between hidden (1) and drawn (0). A hidden layer is also made
|
|
144
|
+
* invisible once its sweep is over, because the round cap of the dash boundary sitting on
|
|
145
|
+
* the path's start would otherwise still paint a blob of the color at that corner; it is
|
|
146
|
+
* made visible again the instant it starts sweeping in. In sketch style the clipped edge
|
|
147
|
+
* is roughened after clipping, so it reads as paint rather than a vector cut.
|
|
148
|
+
*/
|
|
149
|
+
const layer = (drawn: boolean, key: string, stroke: string) => (
|
|
150
|
+
<g filter={sketch ? `url(#${ids.rough})` : undefined}>
|
|
151
|
+
<g clipPath={`url(#${ids.clip})`}>
|
|
152
|
+
<path
|
|
153
|
+
d={geometry.brush}
|
|
154
|
+
fill="none"
|
|
155
|
+
strokeWidth={brushWidth(box, rows)}
|
|
156
|
+
strokeLinecap="round"
|
|
157
|
+
strokeLinejoin="round"
|
|
158
|
+
pathLength={1}
|
|
159
|
+
strokeDasharray="1 1"
|
|
160
|
+
// As style, not presentation attributes, so a `var()` color resolves and the offset transitions.
|
|
161
|
+
style={
|
|
162
|
+
{
|
|
163
|
+
stroke: pixelate ? `url(#${ids.pixel}${key})` : stroke,
|
|
164
|
+
strokeDashoffset: drawn ? 0 : 1,
|
|
165
|
+
visibility: drawn ? 'visible' : 'hidden',
|
|
166
|
+
transition: animate
|
|
167
|
+
? `stroke-dashoffset ${SHAPE.paintDuration}s ${SHAPE.easing} ${delay}s, visibility 0s linear ${drawn ? 0 : delay + SHAPE.paintDuration}s`
|
|
168
|
+
: 'none',
|
|
169
|
+
} as CSSProperties
|
|
170
|
+
}
|
|
171
|
+
filter={paintFilter ? `url(#${paintFilter})` : undefined}
|
|
172
|
+
/>
|
|
173
|
+
</g>
|
|
174
|
+
</g>
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
<svg
|
|
179
|
+
ref={ref}
|
|
180
|
+
viewBox={`0 0 ${size} ${height}`}
|
|
181
|
+
width={size}
|
|
182
|
+
height={height}
|
|
183
|
+
className={cn(className, stateClass)}
|
|
184
|
+
style={style}
|
|
185
|
+
aria-hidden="true"
|
|
186
|
+
>
|
|
187
|
+
<defs>
|
|
188
|
+
<linearGradient
|
|
189
|
+
id={ids.gradient}
|
|
190
|
+
x1="0"
|
|
191
|
+
y1="1"
|
|
192
|
+
x2="1"
|
|
193
|
+
y2="0"
|
|
194
|
+
>
|
|
195
|
+
{/* Colors go in `style`, not presentation attributes, so a `var()` color resolves. */}
|
|
196
|
+
<stop
|
|
197
|
+
offset="0"
|
|
198
|
+
style={{ stopColor: stops[0] }}
|
|
199
|
+
/>
|
|
200
|
+
<stop
|
|
201
|
+
offset="1"
|
|
202
|
+
style={{ stopColor: stops[1] }}
|
|
203
|
+
/>
|
|
204
|
+
</linearGradient>
|
|
205
|
+
{pixelate && mosaic('b', baseColor)}
|
|
206
|
+
{pixelate && mosaic('a', color)}
|
|
207
|
+
{grain > 0 && (
|
|
208
|
+
<filter
|
|
209
|
+
id={ids.grain}
|
|
210
|
+
x="-20%"
|
|
211
|
+
y="-20%"
|
|
212
|
+
width="140%"
|
|
213
|
+
height="140%"
|
|
214
|
+
>
|
|
215
|
+
<feTurbulence
|
|
216
|
+
type="fractalNoise"
|
|
217
|
+
baseFrequency="1.1"
|
|
218
|
+
numOctaves="3"
|
|
219
|
+
seed={seed}
|
|
220
|
+
result="noise"
|
|
221
|
+
/>
|
|
222
|
+
{/* Threshold the noise into a speckle alpha; the intercept sets how much of the paint the speckle covers. */}
|
|
223
|
+
<feColorMatrix
|
|
224
|
+
in="noise"
|
|
225
|
+
type="matrix"
|
|
226
|
+
values={`0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1.6 ${0.4 + 0.85 * grain}`}
|
|
227
|
+
result="speckle"
|
|
228
|
+
/>
|
|
229
|
+
<feComposite
|
|
230
|
+
in="speckle"
|
|
231
|
+
in2="SourceGraphic"
|
|
232
|
+
operator="in"
|
|
233
|
+
result="speckleInShape"
|
|
234
|
+
/>
|
|
235
|
+
<feBlend
|
|
236
|
+
in="SourceGraphic"
|
|
237
|
+
in2="speckleInShape"
|
|
238
|
+
mode="multiply"
|
|
239
|
+
/>
|
|
240
|
+
</filter>
|
|
241
|
+
)}
|
|
242
|
+
{hatch > 0 && (
|
|
243
|
+
<filter
|
|
244
|
+
id={ids.hatch}
|
|
245
|
+
x="-10%"
|
|
246
|
+
y="-10%"
|
|
247
|
+
width="120%"
|
|
248
|
+
height="120%"
|
|
249
|
+
>
|
|
250
|
+
{/* Noise stretched sideways into streaks, thresholded into a mask that is cut out of the paint, leaving bare paper. */}
|
|
251
|
+
<feTurbulence
|
|
252
|
+
type="fractalNoise"
|
|
253
|
+
baseFrequency={hatchFrequency}
|
|
254
|
+
numOctaves="2"
|
|
255
|
+
seed={seed + 1}
|
|
256
|
+
result="streaks"
|
|
257
|
+
/>
|
|
258
|
+
<feColorMatrix
|
|
259
|
+
in="streaks"
|
|
260
|
+
type="matrix"
|
|
261
|
+
values={`0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14 ${-14 * hatchCut}`}
|
|
262
|
+
result="cut"
|
|
263
|
+
/>
|
|
264
|
+
<feComposite
|
|
265
|
+
in="SourceGraphic"
|
|
266
|
+
in2="cut"
|
|
267
|
+
operator="out"
|
|
268
|
+
/>
|
|
269
|
+
</filter>
|
|
270
|
+
)}
|
|
271
|
+
{sketch && (
|
|
272
|
+
<filter
|
|
273
|
+
id={ids.rough}
|
|
274
|
+
x="-10%"
|
|
275
|
+
y="-10%"
|
|
276
|
+
width="120%"
|
|
277
|
+
height="120%"
|
|
278
|
+
>
|
|
279
|
+
<feTurbulence
|
|
280
|
+
type="fractalNoise"
|
|
281
|
+
baseFrequency="0.04 0.09"
|
|
282
|
+
numOctaves="3"
|
|
283
|
+
seed={seed + 2}
|
|
284
|
+
result="warp"
|
|
285
|
+
/>
|
|
286
|
+
<feDisplacementMap
|
|
287
|
+
in="SourceGraphic"
|
|
288
|
+
in2="warp"
|
|
289
|
+
scale={Math.min(size, height) * 0.05}
|
|
290
|
+
xChannelSelector="R"
|
|
291
|
+
yChannelSelector="G"
|
|
292
|
+
/>
|
|
293
|
+
</filter>
|
|
294
|
+
)}
|
|
295
|
+
<clipPath id={ids.clip}>
|
|
296
|
+
<path
|
|
297
|
+
d={geometry.outline.d}
|
|
298
|
+
transform={geometry.outline.transform}
|
|
299
|
+
/>
|
|
300
|
+
</clipPath>
|
|
301
|
+
</defs>
|
|
302
|
+
|
|
303
|
+
{/* The base layer, flat; then the active layer over it. */}
|
|
304
|
+
{layer(baseDrawn, 'b', baseColor)}
|
|
305
|
+
{layer(activeDrawn, 'a', `url(#${ids.gradient})`)}
|
|
306
|
+
</svg>
|
|
307
|
+
);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Markers
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
/** A bold check mark, drawn in a `MARKER_CHECK_BOX` square. */
|
|
315
|
+
const MARKER_CHECK_BOX = 24;
|
|
316
|
+
const MARKER_CHECK_PATH =
|
|
317
|
+
'M23.48925781,2.43798828c-.09765625-.60839844-.42675781-1.14160156-.91210938-1.49072266l-.02246094-.01757812c-1.03027344-.74755859-2.47363281-.52050781-3.23046875.52001953L7.71386719,17.64550781l-3.30224609-3.30175781c-.93457031-.87109375-2.40185547-.81933594-3.27050781.11621094-.82470703.88476562-.82470703,2.27050781.02392578,3.1796875l5.21386719,5.22265625c.453125.43164062,1.03466797.64550781,1.61523438.64550781.61767578,0,1.23388672-.2421875,1.69091797-.72167969.06640625-.06933594.12841797-.14257812.19433594-.23144531L23.08300781,4.15527344c.36035156-.49951172.50488281-1.109375.40625-1.71728516Z';
|
|
318
|
+
|
|
319
|
+
/*
|
|
320
|
+
* A marker for a point, such as the pin a `<DotsBoard.Item>` puts on its dot: by default a
|
|
321
|
+
* small disc, or, as a `check`, a check mark `size` px square, either in `color` on a halo
|
|
322
|
+
* `haloWidth` px bigger all round, translucent by default so what is under it shows through.
|
|
323
|
+
* The halo is drawn outside the marker's box, so it centers on the point by its `size`.
|
|
324
|
+
*/
|
|
325
|
+
export const Marker = memo(
|
|
326
|
+
(p: {
|
|
327
|
+
shape?: 'dot' | 'check';
|
|
328
|
+
size?: number;
|
|
329
|
+
color?: string;
|
|
330
|
+
haloColor?: string;
|
|
331
|
+
haloWidth?: number;
|
|
332
|
+
className?: string;
|
|
333
|
+
style?: CSSProperties;
|
|
334
|
+
}) => {
|
|
335
|
+
const {
|
|
336
|
+
shape = 'dot',
|
|
337
|
+
size = shape === 'check' ? 14 : 8,
|
|
338
|
+
color = 'rgba(0, 0, 0, 0.4)',
|
|
339
|
+
haloColor = 'rgba(0, 0, 0, 0.1)',
|
|
340
|
+
haloWidth = 2,
|
|
341
|
+
className,
|
|
342
|
+
style,
|
|
343
|
+
} = p;
|
|
344
|
+
|
|
345
|
+
if (shape === 'check') {
|
|
346
|
+
// The halo is the same path stroked underneath: a stroke straddles the edge, so twice the halo's width, in the box's units.
|
|
347
|
+
const haloStroke = (2 * haloWidth * MARKER_CHECK_BOX) / size;
|
|
348
|
+
return (
|
|
349
|
+
<svg
|
|
350
|
+
className={cn('bl', className)}
|
|
351
|
+
width={size}
|
|
352
|
+
height={size}
|
|
353
|
+
viewBox={`0 0 ${MARKER_CHECK_BOX} ${MARKER_CHECK_BOX}`}
|
|
354
|
+
style={{ overflow: 'visible', ...style }}
|
|
355
|
+
aria-hidden="true"
|
|
356
|
+
>
|
|
357
|
+
{haloWidth > 0 && (
|
|
358
|
+
<path
|
|
359
|
+
d={MARKER_CHECK_PATH}
|
|
360
|
+
fill="none"
|
|
361
|
+
stroke={haloColor}
|
|
362
|
+
strokeWidth={haloStroke}
|
|
363
|
+
strokeLinejoin="round"
|
|
364
|
+
/>
|
|
365
|
+
)}
|
|
366
|
+
<path
|
|
367
|
+
d={MARKER_CHECK_PATH}
|
|
368
|
+
fill={color}
|
|
369
|
+
/>
|
|
370
|
+
</svg>
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return (
|
|
375
|
+
<span
|
|
376
|
+
className={cn('bl', className)}
|
|
377
|
+
style={{
|
|
378
|
+
width: size,
|
|
379
|
+
height: size,
|
|
380
|
+
borderRadius: '50%',
|
|
381
|
+
backgroundColor: color,
|
|
382
|
+
boxShadow: haloWidth > 0 ? `0 0 0 ${haloWidth}px ${haloColor}` : undefined,
|
|
383
|
+
...style,
|
|
384
|
+
}}
|
|
385
|
+
/>
|
|
386
|
+
);
|
|
387
|
+
},
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
/** How many pulses are growing at once, each a `duration / halos` behind the last, the ease they grow (and fade) with, and how the dot and its own halo turn to the hover color and back. */
|
|
391
|
+
const MARKER_PULSE = {
|
|
392
|
+
halos: 2,
|
|
393
|
+
easing: 'cubic-bezier(0.2, 0.6, 0.4, 1)',
|
|
394
|
+
colorTransition: 'background-color 0.4s ease-out, box-shadow 0.4s ease-out',
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
/*
|
|
398
|
+
* A `<Marker>` dot that pulses: a flat halo in `pulseColor` grows out from under it to
|
|
399
|
+
* `pulseSize` px across (64 by default), fading out as it grows, while the next halo starts
|
|
400
|
+
* growing behind it, one after another every `duration / 2` seconds for as long as it is
|
|
401
|
+
* mounted. Like the dot's own halo, the pulses are drawn outside its box, so it centers on the
|
|
402
|
+
* point by its `size`. They run as Web Animations, so no stylesheet is needed; before
|
|
403
|
+
* hydration, without the API, or with reduced motion, only the dot shows. When `hoverable`
|
|
404
|
+
* (or given `hoverContent`), a pointer over the pulse stops it: no new halo starts and the ones
|
|
405
|
+
* under way finish their cycle, while the dot and its own halo turn `hoverColor` at once, one
|
|
406
|
+
* color, until the pointer leaves and the pulse carries on (the dot turning back). Meanwhile
|
|
407
|
+
* `hoverContent` shows centered above the pulse, and keeps it stopped while the pointer is over
|
|
408
|
+
* it too.
|
|
409
|
+
*/
|
|
410
|
+
export const PulsingMarker = memo(
|
|
411
|
+
(p: {
|
|
412
|
+
size?: number;
|
|
413
|
+
color?: string;
|
|
414
|
+
haloColor?: string;
|
|
415
|
+
haloWidth?: number;
|
|
416
|
+
pulseColor?: string;
|
|
417
|
+
pulseSize?: number;
|
|
418
|
+
/** Seconds one halo takes to grow to `pulseSize` and fade out. */
|
|
419
|
+
duration?: number;
|
|
420
|
+
/** Hold the pulse at full size while the pointer is over it. On whenever `hoverContent` is given. */
|
|
421
|
+
hoverable?: boolean;
|
|
422
|
+
/** Shown centered above the pulse while the pointer is over it. */
|
|
423
|
+
hoverContent?: ReactNode;
|
|
424
|
+
/** The one color the dot and its own halo turn while the pointer is over the pulse. */
|
|
425
|
+
hoverColor?: string;
|
|
426
|
+
className?: string;
|
|
427
|
+
}) => {
|
|
428
|
+
const {
|
|
429
|
+
size = 8,
|
|
430
|
+
color,
|
|
431
|
+
haloColor,
|
|
432
|
+
haloWidth,
|
|
433
|
+
pulseColor = 'rgb(var(--color-marketing))',
|
|
434
|
+
hoverColor = 'rgb(var(--color-marketing-intense))',
|
|
435
|
+
pulseSize = 64,
|
|
436
|
+
duration = 4,
|
|
437
|
+
hoverContent,
|
|
438
|
+
hoverable = hoverContent != null,
|
|
439
|
+
className,
|
|
440
|
+
} = p;
|
|
441
|
+
const halosRef = useRef<(HTMLSpanElement | null)[]>([]);
|
|
442
|
+
const [hovered, setHovered] = useState(false);
|
|
443
|
+
usePulseHalos(halosRef, size, pulseSize, duration * 1000, MARKER_PULSE.easing, hovered);
|
|
444
|
+
// The pulse's full disc, centered on the dot.
|
|
445
|
+
const pulseBox = {
|
|
446
|
+
left: (size - pulseSize) / 2,
|
|
447
|
+
top: (size - pulseSize) / 2,
|
|
448
|
+
width: pulseSize,
|
|
449
|
+
height: pulseSize,
|
|
450
|
+
borderRadius: '50%',
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
return (
|
|
454
|
+
<span
|
|
455
|
+
className={cn('rel bl', className)}
|
|
456
|
+
style={{ width: size, height: size }}
|
|
457
|
+
onPointerEnter={hoverable ? () => setHovered(true) : undefined}
|
|
458
|
+
onPointerLeave={hoverable ? () => setHovered(false) : undefined}
|
|
459
|
+
>
|
|
460
|
+
{/* The hover target: the whole pulse, not just the dot. */}
|
|
461
|
+
{hoverable && (
|
|
462
|
+
<span
|
|
463
|
+
className="abs"
|
|
464
|
+
style={pulseBox}
|
|
465
|
+
/>
|
|
466
|
+
)}
|
|
467
|
+
{/* Hidden until animated; each halo is one flat disc, so its whole color fades at once. */}
|
|
468
|
+
{Array.from({ length: MARKER_PULSE.halos }, (_, i) => (
|
|
469
|
+
<span
|
|
470
|
+
key={i}
|
|
471
|
+
ref={(el) => {
|
|
472
|
+
halosRef.current[i] = el;
|
|
473
|
+
}}
|
|
474
|
+
className="abs noclick"
|
|
475
|
+
style={{ ...pulseBox, backgroundColor: pulseColor, opacity: 0 }}
|
|
476
|
+
/>
|
|
477
|
+
))}
|
|
478
|
+
{/* Positioned, so the dot paints over the halos. */}
|
|
479
|
+
<Marker
|
|
480
|
+
size={size}
|
|
481
|
+
color={hovered ? hoverColor : color}
|
|
482
|
+
haloColor={hovered ? hoverColor : haloColor}
|
|
483
|
+
haloWidth={haloWidth}
|
|
484
|
+
className="rel"
|
|
485
|
+
style={{ transition: MARKER_PULSE.colorTransition }}
|
|
486
|
+
/>
|
|
487
|
+
{/* Right on top of the pulse, so the pointer can move onto it without letting go. */}
|
|
488
|
+
{hovered && hoverContent != null && (
|
|
489
|
+
<span
|
|
490
|
+
className="abs bl"
|
|
491
|
+
style={{
|
|
492
|
+
left: '50%',
|
|
493
|
+
bottom: (size + pulseSize) / 2,
|
|
494
|
+
width: 'max-content',
|
|
495
|
+
lineHeight: 'normal',
|
|
496
|
+
transform: 'translateX(-50%)',
|
|
497
|
+
}}
|
|
498
|
+
>
|
|
499
|
+
{hoverContent}
|
|
500
|
+
</span>
|
|
501
|
+
)}
|
|
502
|
+
</span>
|
|
503
|
+
);
|
|
504
|
+
},
|
|
505
|
+
);
|