@jsb188/gfx 1.1.2

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 (65) hide show
  1. package/README.md +522 -0
  2. package/elements/Shapes.helpers.ts +204 -0
  3. package/elements/Shapes.hooks.ts +171 -0
  4. package/elements/Shapes.tsx +505 -0
  5. package/elements/Shapes.types.ts +81 -0
  6. package/elements/Stack.helpers.ts +42 -0
  7. package/elements/Stack.tsx +143 -0
  8. package/elements/Text.helpers.ts +45 -0
  9. package/elements/Text.hooks.ts +197 -0
  10. package/elements/Text.tsx +98 -0
  11. package/libs/chain.ts +174 -0
  12. package/libs/dotGrid.helpers.ts +53 -0
  13. package/libs/dotGrid.ts +954 -0
  14. package/libs/effects/dotEffects.ts +136 -0
  15. package/libs/effects/effectQuad.ts +146 -0
  16. package/libs/effects/effectRegion.ts +299 -0
  17. package/libs/effects/imageEffects.ts +599 -0
  18. package/libs/effects/index.ts +8 -0
  19. package/libs/effects/ripple.ts +160 -0
  20. package/libs/glsl.ts +134 -0
  21. package/libs/passes.ts +374 -0
  22. package/libs/pointer.ts +151 -0
  23. package/libs/runtime.ts +329 -0
  24. package/libs/texture.ts +160 -0
  25. package/libs/types.ts +94 -0
  26. package/libs/uniforms.ts +32 -0
  27. package/main.ts +4 -0
  28. package/modules/Carousel.helpers.ts +316 -0
  29. package/modules/Carousel.hooks.ts +520 -0
  30. package/modules/Carousel.tsx +631 -0
  31. package/modules/Dots.tsx +269 -0
  32. package/modules/DotsBoard.helpers.ts +388 -0
  33. package/modules/DotsBoard.hooks.ts +144 -0
  34. package/modules/DotsBoard.strings.ts +649 -0
  35. package/modules/DotsBoard.types.ts +240 -0
  36. package/modules/DotsBoardCanvas.hooks.ts +49 -0
  37. package/modules/DotsBoardCanvas.tsx +190 -0
  38. package/modules/DotsBoardElements.tsx +330 -0
  39. package/modules/Gallery.helpers.ts +32 -0
  40. package/modules/Gallery.hooks.ts +149 -0
  41. package/modules/Gallery.tsx +194 -0
  42. package/modules/GfxCanvas.hooks.ts +12 -0
  43. package/modules/GfxCanvas.tsx +189 -0
  44. package/modules/Hero.tsx +90 -0
  45. package/modules/KineticHeader.helpers.ts +44 -0
  46. package/modules/KineticHeader.hooks.ts +206 -0
  47. package/modules/KineticHeader.tsx +305 -0
  48. package/modules/KineticHeader.types.ts +56 -0
  49. package/modules/ScrollSteps.hooks.ts +138 -0
  50. package/modules/ScrollSteps.tsx +98 -0
  51. package/package.json +52 -0
  52. package/scenes/Carousel.ts +277 -0
  53. package/scenes/DotsBoard.helpers.ts +12 -0
  54. package/scenes/DotsBoard.ts +245 -0
  55. package/scenes/Hero.ts +280 -0
  56. package/tsconfig.json +13 -0
  57. package/utils/align.ts +18 -0
  58. package/utils/color.ts +112 -0
  59. package/utils/dotEffectSpecs.ts +79 -0
  60. package/utils/dotGridSpecs.ts +469 -0
  61. package/utils/easing.ts +18 -0
  62. package/utils/imageEffectSpecs.ts +89 -0
  63. package/utils/motion.ts +88 -0
  64. package/utils/random.ts +22 -0
  65. package/utils/strings.ts +116 -0
@@ -0,0 +1,144 @@
1
+ import { createContext, useCallback, useContext, useLayoutEffect, useMemo, useState } from 'react';
2
+ import type { RefObject } from 'react';
3
+ import { getDotsBoardHolesClipPath } from './DotsBoard.helpers.ts';
4
+ import type {
5
+ DotsBoardContextValue,
6
+ DotsBoardHole,
7
+ DotsLayerContextValue,
8
+ DotsStageContextValue,
9
+ ElementSize,
10
+ Offset,
11
+ } from './DotsBoard.types.ts';
12
+
13
+ /**
14
+ * Contexts
15
+ */
16
+
17
+ export const DotsBoardContext = createContext<DotsBoardContextValue | null>(null);
18
+ export const DotsLayerContext = createContext<DotsLayerContextValue | null>(null);
19
+ export const DotsStageContext = createContext<DotsStageContextValue | null>(null);
20
+
21
+ /*
22
+ * The enclosing board's grid, size and strings; throws outside a `<DotsBoard>`.
23
+ */
24
+ export function useDotsBoard() {
25
+ const board = useContext(DotsBoardContext);
26
+ if (!board) throw new Error('useDotsBoard must be used inside <DotsBoard>');
27
+ return board;
28
+ }
29
+
30
+ /*
31
+ * The enclosing layer (the board itself, or a `<DotsBoard.Sticky>`); throws outside a `<DotsBoard>`.
32
+ */
33
+ export function useDotsLayer() {
34
+ const layer = useContext(DotsLayerContext);
35
+ if (!layer) throw new Error('useDotsLayer must be used inside <DotsBoard>');
36
+ return layer;
37
+ }
38
+
39
+ /*
40
+ * The enclosing stage's rectangle; throws outside a `<DotsBoard.Stage>`.
41
+ */
42
+ export function useDotsStage() {
43
+ const stage = useContext(DotsStageContext);
44
+ if (!stage) throw new Error('useDotsStage must be used inside <DotsBoard.Stage>');
45
+ return stage;
46
+ }
47
+
48
+ /**
49
+ * Measuring
50
+ */
51
+
52
+ /*
53
+ * Track an element's size: measured synchronously on mount (so the first paint is already
54
+ * laid out) and again whenever a `ResizeObserver` sees it change. `version` is 0 until the
55
+ * first measurement and ticks on every observed change after. Select one axis to avoid updates
56
+ * from unrelated layout changes; the unobserved dimension stays zero.
57
+ */
58
+ export function useElementSize(ref: RefObject<HTMLElement | null>, axis: 'both' | 'width' | 'height' = 'both'): ElementSize {
59
+ const [size, setSize] = useState<ElementSize>({ width: 0, height: 0, version: 0 });
60
+ useLayoutEffect(() => {
61
+ const element = ref.current;
62
+ if (!element) return;
63
+ const measure = () => {
64
+ const rect = element.getBoundingClientRect();
65
+ const width = axis === 'height' ? 0 : rect.width;
66
+ const height = axis === 'width' ? 0 : rect.height;
67
+ setSize((prev) =>
68
+ prev.version > 0 && prev.width === width && prev.height === height ? prev : { width, height, version: prev.version + 1 },
69
+ );
70
+ };
71
+ measure();
72
+ // The border box, to match `getBoundingClientRect` and so a padding change counts too.
73
+ const observer = new ResizeObserver(measure);
74
+ observer.observe(element, { box: 'border-box' });
75
+ return () => observer.disconnect();
76
+ }, [ref, axis]);
77
+ return size;
78
+ }
79
+
80
+ /*
81
+ * The viewport's height in CSS pixels, following window resizes; 0 on the server and until the
82
+ * first measurement, so a layout sized to it can fall back to a default until then.
83
+ */
84
+ export function useViewportHeight() {
85
+ const [height, setHeight] = useState(0);
86
+ useLayoutEffect(() => {
87
+ const measure = () => setHeight(window.innerHeight);
88
+ measure();
89
+ window.addEventListener('resize', measure);
90
+ return () => window.removeEventListener('resize', measure);
91
+ }, []);
92
+ return height;
93
+ }
94
+
95
+ /*
96
+ * Where an element's top-left corner sits inside another, in CSS pixels, re-measured whenever
97
+ * `deps` change (the sizes of either, typically). Both rectangles are read in one layout pass,
98
+ * and the offset is scroll-independent, so it holds for an element pinned by `position: sticky`
99
+ * as long as `within` moves with it.
100
+ */
101
+ export function useElementOffset(
102
+ ref: RefObject<HTMLElement | null>,
103
+ within: RefObject<HTMLElement | null>,
104
+ deps: readonly unknown[],
105
+ ): Offset {
106
+ const [offset, setOffset] = useState<Offset>({ left: 0, top: 0 });
107
+ useLayoutEffect(() => {
108
+ const element = ref.current;
109
+ const container = within.current;
110
+ if (!element || !container) return;
111
+ const containerRect = container.getBoundingClientRect();
112
+ const rect = element.getBoundingClientRect();
113
+ const left = rect.left - containerRect.left;
114
+ const top = rect.top - containerRect.top;
115
+ setOffset((prev) => (prev.left === left && prev.top === top ? prev : { left, top }));
116
+ }, deps);
117
+ return offset;
118
+ }
119
+
120
+ /**
121
+ * Holes
122
+ */
123
+
124
+ /*
125
+ * The holes a layer's sections leave in its dots: `setHole` (stable, for the layer's context)
126
+ * adds, replaces or takes away a section's hole by key, and `clipPath` is the CSS that leaves
127
+ * them all out of the layer's dots and hover glow, undefined without any. Sections set their
128
+ * holes in layout effects, so the layer re-renders with them before the paint; nothing inside
129
+ * it re-renders, since its children and context values stay the same.
130
+ */
131
+ export function useDotsHoles() {
132
+ const [holes, setHoles] = useState<ReadonlyMap<string, DotsBoardHole>>(() => new Map());
133
+ const setHole = useCallback((key: string, hole: DotsBoardHole | null) => {
134
+ setHoles((prev) => {
135
+ if ((prev.get(key) ?? null) === hole) return prev;
136
+ const next = new Map(prev);
137
+ if (hole) next.set(key, hole);
138
+ else next.delete(key);
139
+ return next;
140
+ });
141
+ }, []);
142
+ const clipPath = useMemo(() => getDotsBoardHolesClipPath(holes.values()), [holes]);
143
+ return { clipPath, setHole };
144
+ }