@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,138 @@
|
|
|
1
|
+
import type { RefObject } from 'react';
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Whether an element is in the viewport (any pixel of it, or `threshold` of it, within
|
|
6
|
+
* `rootMargin` of the edges). With `once` it stays true after the first sighting, for a
|
|
7
|
+
* reveal that should not undo itself when the visitor scrolls back. False until the first
|
|
8
|
+
* observation, so on the server and before hydration nothing counts as seen; where the
|
|
9
|
+
* browser has no `IntersectionObserver` everything counts as seen at once. `disabled` keeps
|
|
10
|
+
* the element unobserved (and the answer false), for a caller that only sometimes needs it.
|
|
11
|
+
*/
|
|
12
|
+
export function useInView(
|
|
13
|
+
ref: RefObject<Element | null>,
|
|
14
|
+
{
|
|
15
|
+
once = false,
|
|
16
|
+
rootMargin,
|
|
17
|
+
threshold = 0,
|
|
18
|
+
disabled = false,
|
|
19
|
+
}: { once?: boolean; rootMargin?: string; threshold?: number; disabled?: boolean } = {},
|
|
20
|
+
) {
|
|
21
|
+
const [inView, setInView] = useState(false);
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const element = ref.current;
|
|
25
|
+
if (!element || disabled) return;
|
|
26
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
27
|
+
setInView(true);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const observer = new IntersectionObserver(
|
|
31
|
+
([entry]) => {
|
|
32
|
+
// Initial observations can intersect without meeting the requested visible fraction.
|
|
33
|
+
if (entry.isIntersecting && entry.intersectionRatio >= threshold) {
|
|
34
|
+
setInView(true);
|
|
35
|
+
if (once) observer.disconnect();
|
|
36
|
+
} else if (!once) setInView(false);
|
|
37
|
+
},
|
|
38
|
+
{ rootMargin, threshold },
|
|
39
|
+
);
|
|
40
|
+
observer.observe(element);
|
|
41
|
+
return () => observer.disconnect();
|
|
42
|
+
}, [ref, once, rootMargin, threshold, disabled]);
|
|
43
|
+
|
|
44
|
+
return inView;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Constants
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/** Start listening to scroll a little before the track enters the viewport, and keep listening a little after. */
|
|
52
|
+
const SCROLL_ROOT_MARGIN = '25% 0px';
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
* The step a scrolled element is at: how far its top has scrolled past the `top` line, as a
|
|
56
|
+
* share of `count × stepHeight` pixels, floored into `0..count - 1`; or, with `stepHeights`,
|
|
57
|
+
* each step its own number of pixels long, in order, `count` and `stepHeight` then being
|
|
58
|
+
* ignored. `onProgress` gets the continuous 0..1 position on every scroll frame (without a
|
|
59
|
+
* re-render) for anything that should move with the page. The scroll listener is only
|
|
60
|
+
* attached while the element is near the viewport, and reads one layout rectangle per frame.
|
|
61
|
+
*/
|
|
62
|
+
export function useScrollStep(
|
|
63
|
+
ref: RefObject<HTMLElement | null>,
|
|
64
|
+
{
|
|
65
|
+
count,
|
|
66
|
+
stepHeight = 500,
|
|
67
|
+
stepHeights,
|
|
68
|
+
top = 0,
|
|
69
|
+
onProgress,
|
|
70
|
+
}: { count: number; stepHeight?: number; stepHeights?: number[]; top?: number; onProgress?: (progress: number) => void },
|
|
71
|
+
) {
|
|
72
|
+
const onProgressRef = useRef(onProgress);
|
|
73
|
+
const [step, setStep] = useState(0);
|
|
74
|
+
// As one string, so a fresh array of the same heights does not restart the listener.
|
|
75
|
+
const heightsKey = stepHeights?.join(',');
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
onProgressRef.current = onProgress;
|
|
79
|
+
}, [onProgress]);
|
|
80
|
+
|
|
81
|
+
// `stepHeights` is read through `heightsKey`, so a fresh array of the same values changes nothing.
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
const element = ref.current;
|
|
84
|
+
if (!element) return;
|
|
85
|
+
// Where each step ends, in pixels from the start of the track.
|
|
86
|
+
const ends = getScrollStepEnds(heightsKey ? heightsKey.split(',').map(Number) : Array.from({ length: count }, () => stepHeight));
|
|
87
|
+
if (ends.length < 1) return;
|
|
88
|
+
let rafId = 0;
|
|
89
|
+
let listening = false;
|
|
90
|
+
const distance = ends[ends.length - 1];
|
|
91
|
+
|
|
92
|
+
const measure = () => {
|
|
93
|
+
rafId = 0;
|
|
94
|
+
const scrolled = top - element.getBoundingClientRect().top;
|
|
95
|
+
// Steps with no length yet (a layer not measured) are done as soon as the line is reached.
|
|
96
|
+
const progress = distance ? Math.min(1, Math.max(0, scrolled / distance)) : Number(scrolled >= 0);
|
|
97
|
+
// The first step not yet scrolled past; the last one once all are.
|
|
98
|
+
const at = ends.findIndex((end) => scrolled < end);
|
|
99
|
+
setStep(at === -1 ? ends.length - 1 : at);
|
|
100
|
+
onProgressRef.current?.(progress);
|
|
101
|
+
};
|
|
102
|
+
const schedule = () => {
|
|
103
|
+
if (!rafId) rafId = requestAnimationFrame(measure);
|
|
104
|
+
};
|
|
105
|
+
const listen = (on: boolean) => {
|
|
106
|
+
if (on === listening) return;
|
|
107
|
+
listening = on;
|
|
108
|
+
if (on) {
|
|
109
|
+
window.addEventListener('scroll', schedule, { passive: true });
|
|
110
|
+
window.addEventListener('resize', schedule);
|
|
111
|
+
schedule();
|
|
112
|
+
} else {
|
|
113
|
+
window.removeEventListener('scroll', schedule);
|
|
114
|
+
window.removeEventListener('resize', schedule);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const observer = new IntersectionObserver(([entry]) => listen(entry.isIntersecting), { rootMargin: SCROLL_ROOT_MARGIN });
|
|
118
|
+
observer.observe(element);
|
|
119
|
+
return () => {
|
|
120
|
+
observer.disconnect();
|
|
121
|
+
listen(false);
|
|
122
|
+
if (rafId) cancelAnimationFrame(rafId);
|
|
123
|
+
};
|
|
124
|
+
}, [ref, count, stepHeight, heightsKey, top]);
|
|
125
|
+
|
|
126
|
+
return step;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/*
|
|
130
|
+
* The running total of step heights: where each step ends along the track.
|
|
131
|
+
*/
|
|
132
|
+
function getScrollStepEnds(heights: number[]) {
|
|
133
|
+
let total = 0;
|
|
134
|
+
return heights.map((height) => {
|
|
135
|
+
total += Math.max(0, height);
|
|
136
|
+
return total;
|
|
137
|
+
});
|
|
138
|
+
}
|
package/modules/ScrollSteps.tsx
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { memo, useEffect, useRef
|
|
1
|
+
import { memo, useEffect, useRef } 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';
|
|
3
6
|
|
|
4
7
|
/**
|
|
5
8
|
* Constants
|
|
6
9
|
*/
|
|
7
10
|
|
|
8
11
|
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',
|
|
11
12
|
fadeMs: 450,
|
|
12
13
|
fadeEasing: 'cubic-bezier(0.22, 1, 0.36, 1)',
|
|
13
14
|
fadeShiftPx: 14,
|
|
@@ -18,8 +19,8 @@ const SCROLL_STEPS = {
|
|
|
18
19
|
* on screen and the active step advances every `stepHeight` pixels, from 0 to `count - 1`, then
|
|
19
20
|
* the stage scrolls away with the page. `children` renders the stage for the current step and
|
|
20
21
|
* re-renders only when the step changes; `onProgress` receives the continuous 0..1 position on
|
|
21
|
-
* every scroll frame for anything that should move with the page
|
|
22
|
-
*
|
|
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).
|
|
23
24
|
*/
|
|
24
25
|
export const ScrollSteps = memo(
|
|
25
26
|
(p: {
|
|
@@ -39,55 +40,9 @@ export const ScrollSteps = memo(
|
|
|
39
40
|
}) => {
|
|
40
41
|
const { count, stepHeight = 500, stageHeight = '100vh', top = 0, className, style, stageClassName, onProgress, children } = p;
|
|
41
42
|
const trackRef = useRef<HTMLDivElement>(null);
|
|
42
|
-
const
|
|
43
|
-
const [step, setStep] = useState(0);
|
|
43
|
+
const step = useScrollStep(trackRef, { count, stepHeight, top, onProgress });
|
|
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
|
-
|
|
91
46
|
return (
|
|
92
47
|
<div
|
|
93
48
|
ref={trackRef}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsb188/gfx",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.12",
|
|
4
4
|
"description": "Shared OGL/WebGL2 runtime, fullscreen pass chain, shader passes, and hero/section components for web graphics.",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "jsb188",
|
|
@@ -10,10 +10,14 @@
|
|
|
10
10
|
".": "./main.ts",
|
|
11
11
|
"./modules/GfxCanvas": "./modules/GfxCanvas.tsx",
|
|
12
12
|
"./modules/Hero": "./modules/Hero.tsx",
|
|
13
|
-
"./modules/
|
|
13
|
+
"./modules/KineticHeader": "./modules/KineticHeader.tsx",
|
|
14
|
+
"./modules/Dots": "./modules/Dots.tsx",
|
|
14
15
|
"./modules/Carousel": "./modules/Carousel.tsx",
|
|
15
|
-
"./modules/
|
|
16
|
+
"./modules/Gallery": "./modules/Gallery.tsx",
|
|
16
17
|
"./modules/ScrollSteps": "./modules/ScrollSteps.tsx",
|
|
18
|
+
"./elements/Shapes": "./elements/Shapes.tsx",
|
|
19
|
+
"./elements/Stack": "./elements/Stack.tsx",
|
|
20
|
+
"./elements/Text": "./elements/Text.tsx",
|
|
17
21
|
"./libs/runtime": "./libs/runtime.ts",
|
|
18
22
|
"./libs/types": "./libs/types.ts",
|
|
19
23
|
"./libs/uniforms": "./libs/uniforms.ts",
|
|
@@ -27,14 +31,20 @@
|
|
|
27
31
|
"./libs/effects/effectRegion": "./libs/effects/effectRegion.ts",
|
|
28
32
|
"./libs/effects/effectQuad": "./libs/effects/effectQuad.ts",
|
|
29
33
|
"./libs/effects/ripple": "./libs/effects/ripple.ts",
|
|
34
|
+
"./libs/effects/dotEffects": "./libs/effects/dotEffects.ts",
|
|
30
35
|
"./utils/color": "./utils/color.ts",
|
|
31
36
|
"./utils/easing": "./utils/easing.ts",
|
|
37
|
+
"./utils/motion": "./utils/motion.ts",
|
|
38
|
+
"./utils/align": "./utils/align.ts",
|
|
32
39
|
"./utils/imageEffectSpecs": "./utils/imageEffectSpecs.ts",
|
|
33
|
-
"./utils/dotGridSpecs": "./utils/dotGridSpecs.ts"
|
|
40
|
+
"./utils/dotGridSpecs": "./utils/dotGridSpecs.ts",
|
|
41
|
+
"./utils/dotEffectSpecs": "./utils/dotEffectSpecs.ts",
|
|
42
|
+
"./utils/strings": "./utils/strings.ts",
|
|
43
|
+
"./utils/random": "./utils/random.ts"
|
|
34
44
|
},
|
|
35
45
|
"peerDependencies": {
|
|
36
46
|
"ogl": "^1.0.11",
|
|
37
|
-
"react": "^19.
|
|
47
|
+
"react": "^19.3.0"
|
|
38
48
|
},
|
|
39
49
|
"scripts": {
|
|
40
50
|
"build": "tsc"
|
package/scenes/Carousel.ts
CHANGED
|
@@ -17,12 +17,18 @@ export interface CarouselSceneSlide {
|
|
|
17
17
|
key: string;
|
|
18
18
|
/** The focused slide is drawn with the effect fully off; every other slide has it fully on. */
|
|
19
19
|
focused: boolean;
|
|
20
|
+
/** Overall thumbnail opacity, animated independently of focus; defaults to 1. */
|
|
21
|
+
opacity?: number;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
export interface CarouselSceneParams {
|
|
23
25
|
slides: CarouselSceneSlide[];
|
|
24
26
|
/** Live lookup of each slide's <video> by key. The component fills it from ref callbacks, so the scene reads it every frame. */
|
|
25
27
|
videos: ReadonlyMap<string, HTMLVideoElement>;
|
|
28
|
+
/** Default true; disable for galleries whose thumbnails load independently as they enter view. */
|
|
29
|
+
waitForAllVideos?: boolean;
|
|
30
|
+
/** Draw paused thumbnail frames at their wrappers only when they change; native overlays own active playback. */
|
|
31
|
+
pausedThumbnails?: boolean;
|
|
26
32
|
/** Effects applied to the unfocused slides, in order. */
|
|
27
33
|
effects: ImageEffectSpec[];
|
|
28
34
|
/** How long the effect takes to fade in or out when focus moves (ms); match the slide transition. */
|
|
@@ -38,12 +44,16 @@ export interface CarouselSceneParams {
|
|
|
38
44
|
interface CarouselSlideState {
|
|
39
45
|
video: HTMLVideoElement | null;
|
|
40
46
|
texture: VideoTexture | null;
|
|
47
|
+
unsubscribeVideo: (() => void) | null;
|
|
41
48
|
/** Background of the video's wrapper, seen through the dimmed video in place of the page. */
|
|
42
49
|
backdrop: RGB;
|
|
43
50
|
/** Current effect strength, eased between `from` and `to` on the wall clock. */
|
|
44
51
|
amount: number;
|
|
45
52
|
from: number;
|
|
46
53
|
to: number;
|
|
54
|
+
opacity: number;
|
|
55
|
+
opacityFrom: number;
|
|
56
|
+
opacityTo: number;
|
|
47
57
|
startedAt: number;
|
|
48
58
|
}
|
|
49
59
|
|
|
@@ -52,11 +62,16 @@ interface CarouselSlideState {
|
|
|
52
62
|
* slide's video is redrawn each frame at the exact place its <video> element occupies (read from
|
|
53
63
|
* the DOM, so CSS slide transitions carry the drawing along), through the effect list at a
|
|
54
64
|
* per-slide strength. The focused slide eases to 0 (an exact no-op, so it looks like the plain
|
|
55
|
-
* video) and the others ease to 1, fading to `unfocusedOpacity` on the same curve.
|
|
56
|
-
*
|
|
57
|
-
*
|
|
65
|
+
* video) and the others ease to 1, fading to `unfocusedOpacity` on the same curve. Each slide's
|
|
66
|
+
* overall opacity can also animate independently without changing its effect. Videos that lie
|
|
67
|
+
* entirely outside the canvas are skipped. By default the scene waits for every registered video
|
|
68
|
+
* to have a frame; galleries can opt out so thumbnails appear independently as their videos load.
|
|
69
|
+
* Paused thumbnails render on demand, waking only for frame readiness, opacity transitions, or layout changes.
|
|
58
70
|
*/
|
|
59
|
-
export default function createCarouselScene(
|
|
71
|
+
export default function createCarouselScene(
|
|
72
|
+
{ gl, invalidate, onDispose }: SceneContext,
|
|
73
|
+
params: CarouselSceneParams,
|
|
74
|
+
): Scene<CarouselSceneParams> {
|
|
60
75
|
const canvas = gl.canvas as HTMLCanvasElement;
|
|
61
76
|
const states = new Map<string, CarouselSlideState>();
|
|
62
77
|
let quad: EffectQuad | null = null;
|
|
@@ -69,7 +84,7 @@ export default function createCarouselScene({ gl, onDispose }: SceneContext, par
|
|
|
69
84
|
const rect = { x: 0, y: 0, width: 0, height: 0 };
|
|
70
85
|
const pointerUv: [number, number] = [0.5, 0.5];
|
|
71
86
|
onDispose(() => {
|
|
72
|
-
for (const state of states.values()) state
|
|
87
|
+
for (const state of states.values()) disposeVideo(state);
|
|
73
88
|
states.clear();
|
|
74
89
|
quad?.dispose();
|
|
75
90
|
disposeImageEffects(effects);
|
|
@@ -97,32 +112,65 @@ export default function createCarouselScene({ gl, onDispose }: SceneContext, par
|
|
|
97
112
|
for (const slide of next.slides) {
|
|
98
113
|
keep.add(slide.key);
|
|
99
114
|
const target = slide.focused ? 0 : 1;
|
|
115
|
+
const opacity = Math.max(0, Math.min(1, slide.opacity ?? 1));
|
|
100
116
|
let state = states.get(slide.key);
|
|
101
117
|
if (!state) {
|
|
102
118
|
// A slide that appears already in its resting state does not animate in.
|
|
103
|
-
state = {
|
|
119
|
+
state = {
|
|
120
|
+
video: null,
|
|
121
|
+
texture: null,
|
|
122
|
+
unsubscribeVideo: null,
|
|
123
|
+
backdrop: fallbackBackdrop,
|
|
124
|
+
amount: target,
|
|
125
|
+
from: target,
|
|
126
|
+
to: target,
|
|
127
|
+
opacity,
|
|
128
|
+
opacityFrom: opacity,
|
|
129
|
+
opacityTo: opacity,
|
|
130
|
+
startedAt: now,
|
|
131
|
+
};
|
|
104
132
|
states.set(slide.key, state);
|
|
105
|
-
} else if (state.to !== target) {
|
|
133
|
+
} else if (state.to !== target || state.opacityTo !== opacity) {
|
|
106
134
|
state.from = state.amount;
|
|
107
135
|
state.to = target;
|
|
136
|
+
state.opacityFrom = state.opacity;
|
|
137
|
+
state.opacityTo = opacity;
|
|
108
138
|
state.startedAt = now;
|
|
109
139
|
}
|
|
110
140
|
}
|
|
111
141
|
for (const [key, state] of states) {
|
|
112
142
|
if (keep.has(key)) continue;
|
|
113
|
-
state
|
|
143
|
+
disposeVideo(state);
|
|
114
144
|
states.delete(key);
|
|
115
145
|
}
|
|
116
146
|
}
|
|
117
147
|
|
|
148
|
+
/*
|
|
149
|
+
* Release a slide's texture and readiness listeners when its video changes or the slide leaves.
|
|
150
|
+
*/
|
|
151
|
+
function disposeVideo(state: CarouselSlideState) {
|
|
152
|
+
state.unsubscribeVideo?.();
|
|
153
|
+
state.unsubscribeVideo = null;
|
|
154
|
+
state.texture?.dispose();
|
|
155
|
+
state.texture = null;
|
|
156
|
+
state.video = null;
|
|
157
|
+
}
|
|
158
|
+
|
|
118
159
|
/*
|
|
119
160
|
* Bind the slide to whatever <video> is registered for it right now, swapping textures if the element changed.
|
|
120
161
|
*/
|
|
121
162
|
function syncVideo(state: CarouselSlideState, video: HTMLVideoElement | null) {
|
|
122
163
|
if (state.video === video) return;
|
|
123
|
-
state
|
|
164
|
+
disposeVideo(state);
|
|
124
165
|
state.video = video;
|
|
125
166
|
state.texture = video ? createVideoTexture(gl, video) : null;
|
|
167
|
+
if (video) {
|
|
168
|
+
const events = ['loadeddata', 'canplay', 'seeked', 'pause', 'error'] as const;
|
|
169
|
+
for (const event of events) video.addEventListener(event, invalidate);
|
|
170
|
+
state.unsubscribeVideo = () => {
|
|
171
|
+
for (const event of events) video.removeEventListener(event, invalidate);
|
|
172
|
+
};
|
|
173
|
+
}
|
|
126
174
|
// The wrapper's CSS background is the source of truth for what a dimmed video fades toward.
|
|
127
175
|
const wrapper = video?.parentElement;
|
|
128
176
|
state.backdrop = wrapper ? cssColorToRgb(getComputedStyle(wrapper).backgroundColor, fallbackBackdrop) : fallbackBackdrop;
|
|
@@ -132,19 +180,39 @@ export default function createCarouselScene({ gl, onDispose }: SceneContext, par
|
|
|
132
180
|
applySlides(params);
|
|
133
181
|
|
|
134
182
|
return {
|
|
135
|
-
|
|
136
|
-
|
|
183
|
+
/*
|
|
184
|
+
* Keep full-rate playback for carousels and stop between changes for paused galleries.
|
|
185
|
+
*/
|
|
186
|
+
get frameloop() {
|
|
187
|
+
return current.pausedThumbnails ? 'demand' : 'always';
|
|
188
|
+
},
|
|
189
|
+
/*
|
|
190
|
+
* Paused thumbnails have no pointer effect; their native previews handle interaction.
|
|
191
|
+
*/
|
|
192
|
+
get followsPointer() {
|
|
193
|
+
return !current.pausedThumbnails;
|
|
194
|
+
},
|
|
195
|
+
// The frames are the videos themselves (the plain ones hide under the canvas), so they keep
|
|
196
|
+
// playing at full rate: never at the idle rate, nor paused for reduced motion.
|
|
137
197
|
idleFrameRate: 0,
|
|
138
|
-
|
|
198
|
+
pauseForReducedMotion: false,
|
|
199
|
+
isReady: () =>
|
|
200
|
+
current.waitForAllVideos === false ||
|
|
201
|
+
current.slides.every(
|
|
202
|
+
(slide) => !current.videos.has(slide.key) || current.videos.get(slide.key)?.error || states.get(slide.key)?.texture?.loaded,
|
|
203
|
+
),
|
|
139
204
|
resize(nextSize) {
|
|
140
205
|
size = nextSize;
|
|
141
206
|
},
|
|
142
207
|
update() {
|
|
143
208
|
const now = performance.now();
|
|
144
209
|
for (const state of states.values()) {
|
|
145
|
-
if (state.amount === state.to) continue;
|
|
210
|
+
if (state.amount === state.to && state.opacity === state.opacityTo) continue;
|
|
146
211
|
const t = Math.min(1, (now - state.startedAt) / Math.max(1, current.transitionMs));
|
|
147
|
-
|
|
212
|
+
const eased = easeInOutCubic(t);
|
|
213
|
+
state.amount = state.from + (state.to - state.from) * eased;
|
|
214
|
+
state.opacity = state.opacityFrom + (state.opacityTo - state.opacityFrom) * eased;
|
|
215
|
+
if (t < 1) invalidate();
|
|
148
216
|
}
|
|
149
217
|
},
|
|
150
218
|
render({ elapsed, pointer }) {
|
|
@@ -161,15 +229,27 @@ export default function createCarouselScene({ gl, onDispose }: SceneContext, par
|
|
|
161
229
|
syncVideo(state, current.videos.get(slide.key) ?? null);
|
|
162
230
|
const { video, texture } = state;
|
|
163
231
|
if (!video || !texture) continue;
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
232
|
+
// A carousel's initial reveal still waits for registered videos outside the visible canvas.
|
|
233
|
+
if (current.waitForAllVideos !== false) texture.update();
|
|
234
|
+
if (state.opacity <= 0) continue;
|
|
235
|
+
// The enlarged native preview must not stretch the gallery's fixed thumbnail underneath it.
|
|
236
|
+
const videoRect = (current.pausedThumbnails ? (video.parentElement ?? video) : video).getBoundingClientRect();
|
|
167
237
|
rect.x = videoRect.left - canvasRect.left;
|
|
168
238
|
rect.y = videoRect.top - canvasRect.top;
|
|
169
239
|
rect.width = videoRect.width;
|
|
170
240
|
rect.height = videoRect.height;
|
|
171
241
|
// Skip what the carousel has clipped away entirely.
|
|
172
|
-
if (
|
|
242
|
+
if (
|
|
243
|
+
rect.width <= 0 ||
|
|
244
|
+
rect.height <= 0 ||
|
|
245
|
+
rect.x + rect.width <= 0 ||
|
|
246
|
+
rect.x >= size.width ||
|
|
247
|
+
rect.y + rect.height <= 0 ||
|
|
248
|
+
rect.y >= size.height
|
|
249
|
+
)
|
|
250
|
+
continue;
|
|
251
|
+
if (current.waitForAllVideos === false) texture.update();
|
|
252
|
+
if (!texture.loaded) continue;
|
|
173
253
|
pointerUv[0] = (pointerX - rect.x) / rect.width;
|
|
174
254
|
pointerUv[1] = 1 - (pointerY - rect.y) / rect.height;
|
|
175
255
|
quad.draw({
|
|
@@ -182,6 +262,7 @@ export default function createCarouselScene({ gl, onDispose }: SceneContext, par
|
|
|
182
262
|
time: elapsed,
|
|
183
263
|
radius: current.borderRadius,
|
|
184
264
|
opacity: 1 - state.amount * (1 - current.unfocusedOpacity),
|
|
265
|
+
alpha: state.opacity,
|
|
185
266
|
backdrop: state.backdrop,
|
|
186
267
|
});
|
|
187
268
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { easeInOutCubic, easeOutCubic } from '../utils/easing.ts';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Animate from the initial size to the resting size, or reach a larger peak halfway through the reveal
|
|
5
|
+
* and settle back. Both phases meet at rest so the change of direction stays smooth.
|
|
6
|
+
*/
|
|
7
|
+
export function getDotAppearSize(progress: number, size: number, appearSize: number, initialSize = 0) {
|
|
8
|
+
const amount = Math.min(1, Math.max(0, progress));
|
|
9
|
+
if (appearSize <= size) return initialSize + (size - initialSize) * easeOutCubic(amount);
|
|
10
|
+
if (amount < 0.5) return initialSize + (appearSize - initialSize) * easeOutCubic(amount * 2);
|
|
11
|
+
return appearSize + (size - appearSize) * easeInOutCubic((amount - 0.5) * 2);
|
|
12
|
+
}
|