@jsb188/gfx 1.1.2 → 1.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +54 -248
  2. package/libs/dotGrid.ts +77 -480
  3. package/libs/effects/effectQuad.ts +2 -8
  4. package/libs/pointer.ts +0 -6
  5. package/libs/runtime.ts +4 -6
  6. package/libs/types.ts +1 -8
  7. package/main.ts +0 -2
  8. package/modules/Carousel.helpers.ts +0 -234
  9. package/modules/Carousel.tsx +29 -135
  10. package/modules/DotsArea.tsx +120 -0
  11. package/modules/GfxCanvas.tsx +2 -10
  12. package/modules/ScrollSteps.tsx +52 -7
  13. package/modules/VisionBoard.helpers.ts +561 -0
  14. package/modules/VisionBoard.tsx +358 -0
  15. package/modules/VisionBoard.types.ts +88 -0
  16. package/package.json +5 -15
  17. package/scenes/Carousel.ts +18 -99
  18. package/scenes/DotsArea.ts +208 -0
  19. package/tsconfig.json +1 -1
  20. package/utils/color.ts +2 -64
  21. package/utils/dotGridSpecs.ts +46 -304
  22. package/elements/Shapes.helpers.ts +0 -204
  23. package/elements/Shapes.hooks.ts +0 -171
  24. package/elements/Shapes.tsx +0 -505
  25. package/elements/Shapes.types.ts +0 -81
  26. package/elements/Stack.helpers.ts +0 -42
  27. package/elements/Stack.tsx +0 -143
  28. package/elements/Text.helpers.ts +0 -45
  29. package/elements/Text.hooks.ts +0 -197
  30. package/elements/Text.tsx +0 -98
  31. package/libs/dotGrid.helpers.ts +0 -53
  32. package/libs/effects/dotEffects.ts +0 -136
  33. package/modules/Carousel.hooks.ts +0 -520
  34. package/modules/Dots.tsx +0 -269
  35. package/modules/DotsBoard.helpers.ts +0 -388
  36. package/modules/DotsBoard.hooks.ts +0 -144
  37. package/modules/DotsBoard.strings.ts +0 -649
  38. package/modules/DotsBoard.types.ts +0 -240
  39. package/modules/DotsBoardCanvas.hooks.ts +0 -49
  40. package/modules/DotsBoardCanvas.tsx +0 -190
  41. package/modules/DotsBoardElements.tsx +0 -330
  42. package/modules/Gallery.helpers.ts +0 -32
  43. package/modules/Gallery.hooks.ts +0 -149
  44. package/modules/Gallery.tsx +0 -194
  45. package/modules/KineticHeader.helpers.ts +0 -44
  46. package/modules/KineticHeader.hooks.ts +0 -206
  47. package/modules/KineticHeader.tsx +0 -305
  48. package/modules/KineticHeader.types.ts +0 -56
  49. package/modules/ScrollSteps.hooks.ts +0 -138
  50. package/scenes/DotsBoard.helpers.ts +0 -12
  51. package/scenes/DotsBoard.ts +0 -245
  52. package/utils/align.ts +0 -18
  53. package/utils/dotEffectSpecs.ts +0 -79
  54. package/utils/motion.ts +0 -88
  55. package/utils/random.ts +0 -22
  56. package/utils/strings.ts +0 -116
@@ -0,0 +1,120 @@
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
+ );
@@ -71,9 +71,6 @@ export const GfxCanvas = <P = void>({
71
71
  const onReadyRef = useRef(onReady);
72
72
  // ready: first frame drawn, fading in over the placeholder; shown: fade done, placeholder gone.
73
73
  const [status, setStatus] = useState<'loading' | 'ready' | 'shown' | 'error'>('loading');
74
- // A disposed runtime loses its WebGL context for good, and a canvas never gets another, so a
75
- // runtime replaced while the canvas stays mounted (new props, Fast Refresh) gets a new element.
76
- const [canvasKey, setCanvasKey] = useState(0);
77
74
 
78
75
  useEffect(() => {
79
76
  onErrorRef.current = onError;
@@ -159,14 +156,10 @@ export const GfxCanvas = <P = void>({
159
156
  observer?.disconnect();
160
157
  runtime?.dispose();
161
158
  if (runtimeRef.current === runtime) runtimeRef.current = null;
162
- if (runtime && canvas.isConnected) setCanvasKey((key) => key + 1);
163
159
  };
164
- }, [load, lazy, maxPixelRatio, transparent, canvasKey]);
160
+ }, [load, lazy, maxPixelRatio, transparent]);
165
161
 
166
162
  const shown = status === 'ready' || status === 'shown';
167
- // The placeholder stays under the canvas while it fades in. With no fade the swap is one
168
- // commit, so a translucent placeholder never stacks under the same translucent first frame.
169
- const placeholderShown = status === 'loading' || status === 'error' || (status === 'ready' && fadeIn > 0);
170
163
 
171
164
  return (
172
165
  <div
@@ -174,9 +167,8 @@ export const GfxCanvas = <P = void>({
174
167
  style={style}
175
168
  >
176
169
  {/* Rendered first so the canvas fades in on top of it. */}
177
- {placeholderShown && <div className="abs_full">{status === 'error' ? (errorFallback ?? placeholder) : placeholder}</div>}
170
+ {status !== 'shown' && <div className="abs_full">{status === 'error' ? (errorFallback ?? placeholder) : placeholder}</div>}
178
171
  <canvas
179
- key={canvasKey}
180
172
  ref={canvasRef}
181
173
  role={label ? 'img' : undefined}
182
174
  aria-label={label}
@@ -1,14 +1,13 @@
1
- import { memo, useEffect, useRef } from 'react';
1
+ import { memo, useEffect, useRef, useState } from 'react';
2
2
  import type { CSSProperties, ReactNode } from 'react';
3
- import { useScrollStep } from './ScrollSteps.hooks.ts';
4
-
5
- export { useInView, useScrollStep } from './ScrollSteps.hooks.ts';
6
3
 
7
4
  /**
8
5
  * Constants
9
6
  */
10
7
 
11
8
  const SCROLL_STEPS = {
9
+ /** Start listening to scroll a little before the track enters the viewport, and keep listening a little after. */
10
+ rootMargin: '25% 0px',
12
11
  fadeMs: 450,
13
12
  fadeEasing: 'cubic-bezier(0.22, 1, 0.36, 1)',
14
13
  fadeShiftPx: 14,
@@ -19,8 +18,8 @@ const SCROLL_STEPS = {
19
18
  * on screen and the active step advances every `stepHeight` pixels, from 0 to `count - 1`, then
20
19
  * the stage scrolls away with the page. `children` renders the stage for the current step and
21
20
  * re-renders only when the step changes; `onProgress` receives the continuous 0..1 position on
22
- * every scroll frame for anything that should move with the page (see `useScrollStep`, which
23
- * does the measuring and can drive any element the same way).
21
+ * every scroll frame for anything that should move with the page. The scroll listener is only
22
+ * attached while the track is near the viewport, and reads one layout rectangle per frame.
24
23
  */
25
24
  export const ScrollSteps = memo(
26
25
  (p: {
@@ -40,9 +39,55 @@ export const ScrollSteps = memo(
40
39
  }) => {
41
40
  const { count, stepHeight = 500, stageHeight = '100vh', top = 0, className, style, stageClassName, onProgress, children } = p;
42
41
  const trackRef = useRef<HTMLDivElement>(null);
43
- const step = useScrollStep(trackRef, { count, stepHeight, top, onProgress });
42
+ const onProgressRef = useRef(onProgress);
43
+ const [step, setStep] = useState(0);
44
44
  const stageHeightCss = typeof stageHeight === 'number' ? `${stageHeight}px` : stageHeight;
45
45
 
46
+ useEffect(() => {
47
+ onProgressRef.current = onProgress;
48
+ }, [onProgress]);
49
+
50
+ useEffect(() => {
51
+ const track = trackRef.current;
52
+ if (!track || count < 1) return;
53
+ let rafId = 0;
54
+ let listening = false;
55
+ const distance = count * stepHeight;
56
+
57
+ /*
58
+ * How far the track's top has scrolled past the sticky line, as a share of the distance the
59
+ * stage stays pinned.
60
+ */
61
+ const measure = () => {
62
+ rafId = 0;
63
+ const progress = Math.min(1, Math.max(0, (top - track.getBoundingClientRect().top) / distance));
64
+ setStep(Math.min(count - 1, Math.floor(progress * count)));
65
+ onProgressRef.current?.(progress);
66
+ };
67
+ const schedule = () => {
68
+ if (!rafId) rafId = requestAnimationFrame(measure);
69
+ };
70
+ const listen = (on: boolean) => {
71
+ if (on === listening) return;
72
+ listening = on;
73
+ if (on) {
74
+ window.addEventListener('scroll', schedule, { passive: true });
75
+ window.addEventListener('resize', schedule);
76
+ schedule();
77
+ } else {
78
+ window.removeEventListener('scroll', schedule);
79
+ window.removeEventListener('resize', schedule);
80
+ }
81
+ };
82
+ const observer = new IntersectionObserver(([entry]) => listen(entry.isIntersecting), { rootMargin: SCROLL_STEPS.rootMargin });
83
+ observer.observe(track);
84
+ return () => {
85
+ observer.disconnect();
86
+ listen(false);
87
+ if (rafId) cancelAnimationFrame(rafId);
88
+ };
89
+ }, [count, stepHeight, top]);
90
+
46
91
  return (
47
92
  <div
48
93
  ref={trackRef}