@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,116 @@
1
+ /**
2
+ * The geometry of a string hung between points (`DotsBoard`'s connections): the quadratic curve
3
+ * that sags under gravity between two pins, and the SVG path of a whole string through several.
4
+ * No OGL, safe for route bundles.
5
+ */
6
+
7
+ /**
8
+ * Types
9
+ */
10
+
11
+ export interface Point {
12
+ x: number;
13
+ y: number;
14
+ }
15
+
16
+ /**
17
+ * Helpers
18
+ */
19
+
20
+ /*
21
+ * Rest position of a string's bend between two points: the quadratic control point that makes
22
+ * the curve sag by `slack × length` straight down at its middle, but never more than `maxSag`
23
+ * pixels, so a long stretch hangs no lower than the short ones.
24
+ */
25
+ export function getStringRestControl(a: Point, b: Point, slack: number, maxSag = Number.POSITIVE_INFINITY): Point {
26
+ const sag = Math.min(slack * Math.hypot(b.x - a.x, b.y - a.y), maxSag);
27
+ return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 + 2 * sag };
28
+ }
29
+
30
+ /*
31
+ * The point at `t` (0..1) along a quadratic curve from `a` to `b` through control `c`.
32
+ */
33
+ export function getQuadraticPoint(a: Point, c: Point, b: Point, t: number): Point {
34
+ const u = 1 - t;
35
+ return {
36
+ x: u * u * a.x + 2 * u * t * c.x + t * t * b.x,
37
+ y: u * u * a.y + 2 * u * t * c.y + t * t * b.y,
38
+ };
39
+ }
40
+
41
+ /*
42
+ * The point `t` (0..1) of the way from `a` to `b`.
43
+ */
44
+ export function lerpPoint(a: Point, b: Point, t: number): Point {
45
+ return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
46
+ }
47
+
48
+ /*
49
+ * SVG path of one string through several points: a quadratic piece from each point to the
50
+ * next through the bend between them (`bends[i]` joins `points[i]` to `points[i + 1]`), as one
51
+ * continuous stroke. Rounded to a tenth of a unit, so a still string produces the same text
52
+ * each frame.
53
+ */
54
+ export function buildStringPath(points: Point[], bends: Point[]) {
55
+ if (points.length < 2) return '';
56
+ let d = `M${round(points[0].x)} ${round(points[0].y)}`;
57
+ for (let i = 1; i < points.length; i++) {
58
+ const bend = bends[i - 1];
59
+ d += `Q${round(bend.x)} ${round(bend.y)} ${round(points[i].x)} ${round(points[i].y)}`;
60
+ }
61
+ return d;
62
+ }
63
+
64
+ /*
65
+ * Where a straight move from `from` to `to` (a pointer between two events) crosses the
66
+ * quadratic curve from `a` to `b` through control `c`: the curve's `t` (0..1) at the crossing,
67
+ * or null when the move does not cross it. The curve is walked as `segments` straight pieces.
68
+ */
69
+ export function getStringCrossing(a: Point, c: Point, b: Point, from: Point, to: Point, segments = 12): number | null {
70
+ let prev = a;
71
+ for (let i = 1; i <= segments; i++) {
72
+ const t = i / segments;
73
+ const next = getQuadraticPoint(a, c, b, t);
74
+ const hit = getSegmentIntersection(prev, next, from, to);
75
+ if (hit !== null) return (i - 1 + hit) / segments;
76
+ prev = next;
77
+ }
78
+ return null;
79
+ }
80
+
81
+ /*
82
+ * The point of the quadratic curve from `a` to `b` through control `c` nearest to `point`, as
83
+ * the curve's `t` (0..1) and the distance to it, found among `segments + 1` samples.
84
+ */
85
+ export function getNearestStringPoint(a: Point, c: Point, b: Point, point: Point, segments = 24) {
86
+ let best = { t: 0, distance: Number.POSITIVE_INFINITY };
87
+ for (let i = 0; i <= segments; i++) {
88
+ const t = i / segments;
89
+ const at = getQuadraticPoint(a, c, b, t);
90
+ const distance = Math.hypot(at.x - point.x, at.y - point.y);
91
+ if (distance < best.distance) best = { t, distance };
92
+ }
93
+ return best;
94
+ }
95
+
96
+ /*
97
+ * Where segment `p1`→`p2` meets segment `q1`→`q2`, as the fraction along the first (0..1), or
98
+ * null when they do not meet (parallel ones never do).
99
+ */
100
+ export function getSegmentIntersection(p1: Point, p2: Point, q1: Point, q2: Point): number | null {
101
+ const rx = p2.x - p1.x;
102
+ const ry = p2.y - p1.y;
103
+ const sx = q2.x - q1.x;
104
+ const sy = q2.y - q1.y;
105
+ const denominator = rx * sy - ry * sx;
106
+ if (denominator === 0) return null;
107
+ const dx = q1.x - p1.x;
108
+ const dy = q1.y - p1.y;
109
+ const t = (dx * sy - dy * sx) / denominator;
110
+ const u = (dx * ry - dy * rx) / denominator;
111
+ return t >= 0 && t <= 1 && u >= 0 && u <= 1 ? t : null;
112
+ }
113
+
114
+ function round(value: number) {
115
+ return Math.round(value * 10) / 10;
116
+ }
@@ -1,120 +0,0 @@
1
- import { cn } from '@jsb188/app/utils/string.ts';
2
- import { memo, useLayoutEffect, useMemo, useRef, useState } from 'react';
3
- import type { CSSProperties, ReactNode } from 'react';
4
- import type { DotsAreaSceneParams } from '../scenes/DotsArea.ts';
5
- import { makeDotGridTileUrl, resolveDotGridOptions } from '../utils/dotGridSpecs.ts';
6
- import type { DotGridOptions } from '../utils/dotGridSpecs.ts';
7
- import { GfxCanvas, PlaceholderImage } from './GfxCanvas.tsx';
8
- import { useContentMemo } from './GfxCanvas.hooks.ts';
9
-
10
- /*
11
- * Load OGL and the scene only when the area mounts, never with the route bundle.
12
- */
13
- const loadDotsAreaScene = () => import('../scenes/DotsArea.ts');
14
-
15
- /*
16
- * Area covered by a dot pattern (the `.pattern_speckle` tile by default). A moving cursor
17
- * throws the dots aside (`kick`); they scatter away from its path and spring back home with a
18
- * bounce, wearing the `thrown` look while they are away. A still cursor does nothing. With
19
- * `imageUrl` each dot is a window onto that image instead of a flat color. With `reveal` the
20
- * dots open large enough to fill the area (a solid image or color, optionally zooming to 100%
21
- * from mount), then shrink to size once the delay is up, before the kick becomes active. With
22
- * `pulse` a random few dots swell and shrink back every cycle. With `fadeBottom` the dots (thrown
23
- * or pulsing too) fade to transparent over that many pixels along the bottom edge; the filled
24
- * reveal state is never faded, the fade arrives as it shrinks into dots. The canvas is
25
- * transparent, so the pattern sits over whatever background the area is given (`className` /
26
- * `style`); children render above it. Until the canvas is ready (and if WebGL is unavailable) static CSS stands in:
27
- * the dot tile, or the solid image or color while a reveal is pending, so the first frame matches.
28
- */
29
- export const DotsArea = memo(
30
- (
31
- p: DotGridOptions & {
32
- children?: ReactNode;
33
- /** Image shown through the dots instead of `dots.color`, cover-fit to the area; bigger dots reveal more of it. */
34
- imageUrl?: string;
35
- /** Height in CSS pixels of a fade to transparent along the bottom edge, applied to the dots but not to a reveal's filled state. */
36
- fadeBottom?: number;
37
- className?: string;
38
- style?: CSSProperties;
39
- },
40
- ) => {
41
- const { children, imageUrl, fadeBottom, className, style, dots, kick, thrown, reveal, pulse } = p;
42
- // The reveal timeline is anchored to when the placeholder's CSS zoom began. That animation
43
- // is in the server-rendered markup, so it starts as soon as the page paints, before React
44
- // has hydrated; at hydration the scene picks up its actual start so the canvas, whenever it
45
- // is ready, fades in exactly in step.
46
- const placeholderRef = useRef<HTMLImageElement>(null);
47
- const [revealBegin, setRevealBegin] = useState(() => performance.now());
48
- useLayoutEffect(() => {
49
- const animation = placeholderRef.current?.getAnimations()[0];
50
- if (typeof animation?.currentTime === 'number') setRevealBegin(performance.now() - animation.currentTime);
51
- }, []);
52
- const params = useContentMemo<DotsAreaSceneParams>(
53
- () => ({
54
- imageUrl: imageUrl ?? null,
55
- revealBegin,
56
- fadeBottom: Math.max(0, fadeBottom ?? 0),
57
- ...resolveDotGridOptions({ dots, kick, thrown, reveal, pulse }),
58
- }),
59
- [imageUrl, revealBegin, fadeBottom, dots, kick, thrown, reveal, pulse],
60
- );
61
- const tileUrl = useMemo(() => makeDotGridTileUrl(params.dots), [params.dots]);
62
- // The placeholder's zoom, as a keyframe animation so it needs no JavaScript to start.
63
- const zoom = params.reveal?.zoomOnMount ?? 1;
64
- const zoomKeyframes = zoom !== 1 ? `dots-area-zoom-${String(zoom).replace(/[^0-9]/g, '_')}` : null;
65
- // The static stand-in: the dot tile at rest (with the bottom fade as a second mask layer, so
66
- // it matches the canvas), or the solid fill a pending reveal opens with.
67
- const fadeMask =
68
- params.fadeBottom > 0 ? `, linear-gradient(to bottom, #000 calc(100% - ${params.fadeBottom}px), transparent)` : '';
69
- const placeholderStyle: CSSProperties = params.reveal
70
- ? { opacity: params.dots.opacity }
71
- : {
72
- opacity: params.dots.opacity,
73
- maskImage: `url("${tileUrl}")${fadeMask}`,
74
- maskRepeat: fadeMask ? 'repeat, no-repeat' : 'repeat',
75
- maskSize: `${params.dots.spacing}px ${params.dots.spacing}px${fadeMask ? ', 100% 100%' : ''}`,
76
- maskPosition: 'center',
77
- maskComposite: fadeMask ? 'intersect' : undefined,
78
- };
79
-
80
- return (
81
- <div
82
- className={cn('rel of', className)}
83
- style={style}
84
- >
85
- {zoomKeyframes && (
86
- <style>{`@keyframes ${zoomKeyframes} { from { transform: scale(${zoom}); } to { transform: scale(1); } }`}</style>
87
- )}
88
- {/* The first frame reproduces the placeholder exactly, so no cross-fade is needed; a fade would let the solid placeholder show through the dots' gaps and hide the reveal's start. */}
89
- <GfxCanvas
90
- load={loadDotsAreaScene}
91
- params={params}
92
- fill
93
- transparent
94
- fadeIn={0}
95
- placeholder={
96
- imageUrl && params.reveal ? (
97
- <PlaceholderImage
98
- ref={placeholderRef}
99
- src={imageUrl}
100
- style={{
101
- ...placeholderStyle,
102
- // The same zoom the scene runs (ease-out cubic), started by the browser on first paint.
103
- animation: zoomKeyframes
104
- ? `${zoomKeyframes} ${params.reveal.zoomDuration}ms cubic-bezier(0.33, 1, 0.68, 1) forwards`
105
- : undefined,
106
- }}
107
- />
108
- ) : (
109
- <div
110
- className="abs_full"
111
- style={{ ...placeholderStyle, backgroundColor: params.dots.color }}
112
- />
113
- )
114
- }
115
- />
116
- <div className="rel">{children}</div>
117
- </div>
118
- );
119
- },
120
- );