@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.
- package/README.md +54 -248
- package/libs/dotGrid.ts +77 -480
- package/libs/effects/effectQuad.ts +2 -8
- package/libs/pointer.ts +0 -6
- package/libs/runtime.ts +4 -6
- package/libs/types.ts +1 -8
- package/main.ts +0 -2
- package/modules/Carousel.helpers.ts +0 -234
- package/modules/Carousel.tsx +29 -135
- package/modules/DotsArea.tsx +120 -0
- package/modules/GfxCanvas.tsx +2 -10
- package/modules/ScrollSteps.tsx +52 -7
- package/modules/VisionBoard.helpers.ts +561 -0
- package/modules/VisionBoard.tsx +358 -0
- package/modules/VisionBoard.types.ts +88 -0
- package/package.json +5 -15
- package/scenes/Carousel.ts +18 -99
- package/scenes/DotsArea.ts +208 -0
- package/tsconfig.json +1 -1
- package/utils/color.ts +2 -64
- package/utils/dotGridSpecs.ts +46 -304
- package/elements/Shapes.helpers.ts +0 -204
- package/elements/Shapes.hooks.ts +0 -171
- package/elements/Shapes.tsx +0 -505
- package/elements/Shapes.types.ts +0 -81
- package/elements/Stack.helpers.ts +0 -42
- package/elements/Stack.tsx +0 -143
- package/elements/Text.helpers.ts +0 -45
- package/elements/Text.hooks.ts +0 -197
- package/elements/Text.tsx +0 -98
- package/libs/dotGrid.helpers.ts +0 -53
- package/libs/effects/dotEffects.ts +0 -136
- package/modules/Carousel.hooks.ts +0 -520
- package/modules/Dots.tsx +0 -269
- package/modules/DotsBoard.helpers.ts +0 -388
- package/modules/DotsBoard.hooks.ts +0 -144
- package/modules/DotsBoard.strings.ts +0 -649
- package/modules/DotsBoard.types.ts +0 -240
- package/modules/DotsBoardCanvas.hooks.ts +0 -49
- package/modules/DotsBoardCanvas.tsx +0 -190
- package/modules/DotsBoardElements.tsx +0 -330
- package/modules/Gallery.helpers.ts +0 -32
- package/modules/Gallery.hooks.ts +0 -149
- package/modules/Gallery.tsx +0 -194
- package/modules/KineticHeader.helpers.ts +0 -44
- package/modules/KineticHeader.hooks.ts +0 -206
- package/modules/KineticHeader.tsx +0 -305
- package/modules/KineticHeader.types.ts +0 -56
- package/modules/ScrollSteps.hooks.ts +0 -138
- package/scenes/DotsBoard.helpers.ts +0 -12
- package/scenes/DotsBoard.ts +0 -245
- package/utils/align.ts +0 -18
- package/utils/dotEffectSpecs.ts +0 -79
- package/utils/motion.ts +0 -88
- package/utils/random.ts +0 -22
- package/utils/strings.ts +0 -116
|
@@ -35,8 +35,6 @@ export interface EffectQuadDraw {
|
|
|
35
35
|
radius: number;
|
|
36
36
|
/** 0..1 opacity of the drawn image over `backdrop`; the rectangle itself stays opaque. */
|
|
37
37
|
opacity: number;
|
|
38
|
-
/** Overall alpha of the image and its backdrop; defaults to 1. */
|
|
39
|
-
alpha?: number;
|
|
40
38
|
/** Color seen through a translucent image, as if the rectangle had that background. */
|
|
41
39
|
backdrop: RGB;
|
|
42
40
|
}
|
|
@@ -77,7 +75,6 @@ export function createEffectQuad(gl: OGLRenderingContext, effects: ImageEffectIn
|
|
|
77
75
|
`
|
|
78
76
|
uniform float uRadius;
|
|
79
77
|
uniform float uOpacity;
|
|
80
|
-
uniform float uAlpha;
|
|
81
78
|
uniform vec3 uBackdrop;
|
|
82
79
|
void main() {
|
|
83
80
|
${parts.body}
|
|
@@ -85,8 +82,7 @@ void main() {
|
|
|
85
82
|
vec2 corner = abs(vUv - 0.5) * uResolution - (uResolution * 0.5 - uRadius);
|
|
86
83
|
float dist = length(max(corner, 0.0)) + min(max(corner.x, corner.y), 0.0) - uRadius;
|
|
87
84
|
float coverage = 1.0 - clamp(dist + 0.5, 0.0, 1.0);
|
|
88
|
-
//
|
|
89
|
-
coverage *= uAlpha;
|
|
85
|
+
// A translucent image sits on its own backdrop, so the page never shows through the rectangle.
|
|
90
86
|
fragColor = vec4(mix(uBackdrop, image, uOpacity) * coverage, coverage);
|
|
91
87
|
}
|
|
92
88
|
`,
|
|
@@ -101,7 +97,6 @@ void main() {
|
|
|
101
97
|
uRect: { value: [0, 0, 1, 1] },
|
|
102
98
|
uRadius: { value: 0 },
|
|
103
99
|
uOpacity: { value: 1 },
|
|
104
|
-
uAlpha: { value: 1 },
|
|
105
100
|
uBackdrop: { value: [1, 1, 1] },
|
|
106
101
|
},
|
|
107
102
|
transparent: true,
|
|
@@ -119,7 +114,7 @@ void main() {
|
|
|
119
114
|
|
|
120
115
|
return {
|
|
121
116
|
program,
|
|
122
|
-
draw({ image, imageSize, rect, canvas, amount, pointer, time, radius, opacity,
|
|
117
|
+
draw({ image, imageSize, rect, canvas, amount, pointer, time, radius, opacity, backdrop }) {
|
|
123
118
|
uniforms.tImage.value = image;
|
|
124
119
|
setVec2(uniforms.uImageSize, imageSize[0], imageSize[1]);
|
|
125
120
|
setVec2(uniforms.uResolution, rect.width * canvas.pixelRatio, rect.height * canvas.pixelRatio);
|
|
@@ -134,7 +129,6 @@ void main() {
|
|
|
134
129
|
uniforms.uTime.value = time;
|
|
135
130
|
uniforms.uRadius.value = radius * canvas.pixelRatio;
|
|
136
131
|
uniforms.uOpacity.value = opacity;
|
|
137
|
-
uniforms.uAlpha.value = alpha;
|
|
138
132
|
setVec3(uniforms.uBackdrop, backdrop);
|
|
139
133
|
gl.renderer.render({ scene: mesh, clear: false, sort: false, frustumCull: false, update: false });
|
|
140
134
|
},
|
package/libs/pointer.ts
CHANGED
|
@@ -79,10 +79,7 @@ export function createPointerTracker(canvas: HTMLCanvasElement, onMove: () => vo
|
|
|
79
79
|
y: 0.5,
|
|
80
80
|
targetX: 0.5,
|
|
81
81
|
targetY: 0.5,
|
|
82
|
-
clientX: 0,
|
|
83
|
-
clientY: 0,
|
|
84
82
|
active: false,
|
|
85
|
-
present: false,
|
|
86
83
|
hover: false,
|
|
87
84
|
hoverAmount: 0,
|
|
88
85
|
};
|
|
@@ -104,10 +101,7 @@ export function createPointerTracker(canvas: HTMLCanvasElement, onMove: () => vo
|
|
|
104
101
|
const y = 1 - (page.clientY - rect.top) / rect.height;
|
|
105
102
|
pointer.targetX = x;
|
|
106
103
|
pointer.targetY = y;
|
|
107
|
-
pointer.clientX = page.clientX;
|
|
108
|
-
pointer.clientY = page.clientY;
|
|
109
104
|
pointer.active = true;
|
|
110
|
-
pointer.present = page.present;
|
|
111
105
|
pointer.hover = page.present && x >= 0 && x <= 1 && y >= 0 && y <= 1;
|
|
112
106
|
}
|
|
113
107
|
|
package/libs/runtime.ts
CHANGED
|
@@ -215,7 +215,7 @@ export function createGfxRuntime<P>({
|
|
|
215
215
|
ready = true;
|
|
216
216
|
onReady?.();
|
|
217
217
|
}
|
|
218
|
-
const continuous = scene.frameloop === 'always' &&
|
|
218
|
+
const continuous = scene.frameloop === 'always' && !reducedMotion;
|
|
219
219
|
if (continuous || !settled) scheduleNext(time);
|
|
220
220
|
// A demand scene that asked for another frame (invalidate during update/render) keeps
|
|
221
221
|
// its clock; resetting it here would hand that frame a zero delta and stall its animation.
|
|
@@ -246,11 +246,9 @@ export function createGfxRuntime<P>({
|
|
|
246
246
|
try {
|
|
247
247
|
canvas.width = Math.max(1, Math.round(width * pixelRatio));
|
|
248
248
|
canvas.height = Math.max(1, Math.round(height * pixelRatio));
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
renderer.
|
|
252
|
-
renderer.width = canvas.width;
|
|
253
|
-
renderer.height = canvas.height;
|
|
249
|
+
renderer.dpr = pixelRatio;
|
|
250
|
+
renderer.width = canvas.width / pixelRatio;
|
|
251
|
+
renderer.height = canvas.height / pixelRatio;
|
|
254
252
|
scene.resize({ width, height, pixelRatio });
|
|
255
253
|
activity();
|
|
256
254
|
} catch (error) {
|
package/libs/types.ts
CHANGED
|
@@ -16,13 +16,8 @@ export interface ScenePointer {
|
|
|
16
16
|
/** Raw position the smoothed value eases toward. */
|
|
17
17
|
targetX: number;
|
|
18
18
|
targetY: number;
|
|
19
|
-
/** Raw position in viewport CSS pixels; unlike the UVs it stays put when the canvas scrolls under a still pointer. */
|
|
20
|
-
clientX: number;
|
|
21
|
-
clientY: number;
|
|
22
19
|
/** False until the first pointer event; the position rests at the center before then. */
|
|
23
20
|
active: boolean;
|
|
24
|
-
/** True while the last mouse or pen position is present in the window. */
|
|
25
|
-
present: boolean;
|
|
26
21
|
/** True while the pointer is inside the canvas rect (over children included). */
|
|
27
22
|
hover: boolean;
|
|
28
23
|
/** Eased 0..1 version of `hover`; drive hover effects from this. */
|
|
@@ -51,10 +46,8 @@ export interface SceneContext {
|
|
|
51
46
|
}
|
|
52
47
|
|
|
53
48
|
export interface Scene<P = void> {
|
|
54
|
-
/** Defaults to demand. Always mode pauses when hidden
|
|
49
|
+
/** Defaults to demand. Always mode pauses when hidden or reduced motion is enabled. */
|
|
55
50
|
frameloop?: 'always' | 'demand';
|
|
56
|
-
/** False keeps an always-running scene drawing while the visitor prefers reduced motion, for frames that are content (video) rather than decoration; true by default. */
|
|
57
|
-
pauseForReducedMotion?: boolean;
|
|
58
51
|
/** False for a scene nothing in which reacts to the pointer: moves no longer wake the loop or ease the pointer. Read live, so it may change. */
|
|
59
52
|
followsPointer?: boolean;
|
|
60
53
|
/** Per-second easing rate for the pointer; 6 (the default) is roughly 10% per frame at 60fps. */
|
package/main.ts
CHANGED
|
@@ -80,237 +80,3 @@ export function getVirtualPositions(position: number, containerWidth: number, mi
|
|
|
80
80
|
for (let v = position - sideCount; v <= position + sideCount; v++) positions.push(v);
|
|
81
81
|
return positions;
|
|
82
82
|
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Blocks carousel
|
|
86
|
-
*/
|
|
87
|
-
|
|
88
|
-
/** Where a block rests when the track snaps to it. */
|
|
89
|
-
export type BlocksAlign = 'start' | 'center';
|
|
90
|
-
|
|
91
|
-
/** What a press on the blocks starts: a drag of the track, a text selection, or a form field's own handling. */
|
|
92
|
-
export type BlocksPressKind = 'drag' | 'text' | 'native';
|
|
93
|
-
|
|
94
|
-
/** `Document` with the caret API Safari and older Chromium have instead of `caretPositionFromPoint`. */
|
|
95
|
-
type DocumentWithCaretRange = Document & {
|
|
96
|
-
caretRangeFromPoint?: (x: number, y: number) => Range | null;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
/** One moment of a drag: when it was, and where the pointer had put the track (before any edge resistance). */
|
|
100
|
-
export interface DragSample {
|
|
101
|
-
time: number;
|
|
102
|
-
offset: number;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** The blocks track measured: the viewport it scrolls in, how far it can go, and where each block snaps. */
|
|
106
|
-
export interface BlocksTrackMeasure {
|
|
107
|
-
/** Width of the container's content box, in CSS pixels. */
|
|
108
|
-
viewportWidth: number;
|
|
109
|
-
/** The track's resting offset at its start: 0, less the content offset. */
|
|
110
|
-
minOffset: number;
|
|
111
|
-
/** The furthest the track can move: its width past the viewport's (or 0 when it all fits), less the content offset. */
|
|
112
|
-
maxOffset: number;
|
|
113
|
-
/** Track offset that puts each block at its snap position, in block order. */
|
|
114
|
-
offsets: number[];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/** The iOS scroll view constant: how quickly a drag past an edge stiffens. */
|
|
118
|
-
const RUBBER_BAND_COEFFICIENT = 0.55;
|
|
119
|
-
|
|
120
|
-
/** Pixels per line for a wheel that reports lines instead of pixels (Firefox with a mouse wheel). */
|
|
121
|
-
const WHEEL_LINE_PX = 16;
|
|
122
|
-
|
|
123
|
-
/*
|
|
124
|
-
* Track offset that brings each block to rest: its left edge at the viewport's left edge
|
|
125
|
-
* (`start`), or its middle at the viewport's middle (`center`), never past either end of the
|
|
126
|
-
* track. Blocks that would need more than the track can give all share `maxOffset`, so the last
|
|
127
|
-
* blocks are reached at one and the same position.
|
|
128
|
-
*/
|
|
129
|
-
export function getBlockSnapOffsets(
|
|
130
|
-
blocks: { left: number; width: number }[],
|
|
131
|
-
viewportWidth: number,
|
|
132
|
-
maxOffset: number,
|
|
133
|
-
align: BlocksAlign,
|
|
134
|
-
) {
|
|
135
|
-
return blocks.map(({ left, width }) => {
|
|
136
|
-
const wanted = align === 'center' ? left + width / 2 - viewportWidth / 2 : left;
|
|
137
|
-
return Math.min(maxOffset, Math.max(0, wanted));
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/*
|
|
142
|
-
* Index of the snap offset nearest to `offset`; the lower index on a tie, so of the blocks sharing
|
|
143
|
-
* the end offset the first is the one reported.
|
|
144
|
-
*/
|
|
145
|
-
export function getNearestBlockIndex(offsets: number[], offset: number) {
|
|
146
|
-
let nearest = 0;
|
|
147
|
-
for (let i = 1; i < offsets.length; i++) {
|
|
148
|
-
if (Math.abs(offsets[i] - offset) < Math.abs(offsets[nearest] - offset)) nearest = i;
|
|
149
|
-
}
|
|
150
|
-
return nearest;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/*
|
|
154
|
-
* The block a finished gesture lands on: the one whose snap offset is nearest to where the track
|
|
155
|
-
* would coast (`offset` plus `projection`), so a flick carries on a little. A gesture that clearly
|
|
156
|
-
* meant to move on (`intent` is its direction: a flick's, or a wheel's after enough travel) but
|
|
157
|
-
* rounds back onto the block it started from, having moved past that block's offset in its
|
|
158
|
-
* direction, still advances to the next block with a different offset; so a mouse wheel's notch
|
|
159
|
-
* or a short flick never leaves the carousel where it was. Going back and then flicking forward
|
|
160
|
-
* is not such a case (the track is behind its start), and returns to the start block.
|
|
161
|
-
*/
|
|
162
|
-
export function getSettleIndex(offsets: number[], offset: number, projection: number, startIndex: number, intent: -1 | 0 | 1) {
|
|
163
|
-
const nearest = getNearestBlockIndex(offsets, offset + projection);
|
|
164
|
-
if (nearest !== startIndex || intent === 0 || (offset - offsets[startIndex]) * intent <= 0) return nearest;
|
|
165
|
-
let next = startIndex;
|
|
166
|
-
// Skip blocks that rest at the same offset as the start block (those sharing the track's end).
|
|
167
|
-
while (next + intent >= 0 && next + intent < offsets.length) {
|
|
168
|
-
next += intent;
|
|
169
|
-
if (offsets[next] !== offsets[startIndex]) return next;
|
|
170
|
-
}
|
|
171
|
-
return startIndex;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/*
|
|
175
|
-
* How fast the pointer was moving the track at release, in CSS pixels per second, from the samples
|
|
176
|
-
* of the last `windowMs`; 0 when it held still that long (a slow release is not a flick).
|
|
177
|
-
*/
|
|
178
|
-
export function getDragVelocity(samples: DragSample[], now: number, windowMs: number) {
|
|
179
|
-
const recent = samples.filter((sample) => now - sample.time <= windowMs);
|
|
180
|
-
if (recent.length < 2) return 0;
|
|
181
|
-
const first = recent[0];
|
|
182
|
-
const last = recent[recent.length - 1];
|
|
183
|
-
const dt = last.time - first.time;
|
|
184
|
-
return dt > 0 ? ((last.offset - first.offset) / dt) * 1000 : 0;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/*
|
|
188
|
-
* Soften a track offset dragged past `min` or `max`: the excess is compressed the way iOS scroll
|
|
189
|
-
* views do it, so the track follows the pointer less and less and never gets as far as `size` (the
|
|
190
|
-
* viewport width) from its edge. Inside the range the offset is returned as is.
|
|
191
|
-
*/
|
|
192
|
-
export function getRubberBandOffset(offset: number, min: number, max: number, size: number) {
|
|
193
|
-
if (offset >= min && offset <= max) return offset;
|
|
194
|
-
const edge = offset < min ? min : max;
|
|
195
|
-
if (size <= 0) return edge;
|
|
196
|
-
const excess = Math.abs(offset - edge);
|
|
197
|
-
const soft = (1 - 1 / ((excess * RUBBER_BAND_COEFFICIENT) / size + 1)) * size;
|
|
198
|
-
return edge + Math.sign(offset - edge) * soft;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/*
|
|
202
|
-
* Sideways wheel movement in CSS pixels, whatever unit the event reports in: lines are taken as
|
|
203
|
-
* `WHEEL_LINE_PX` each, pages as the viewport width.
|
|
204
|
-
*/
|
|
205
|
-
export function getWheelDeltaX(event: WheelEvent, viewportWidth: number) {
|
|
206
|
-
if (event.deltaMode === event.DOM_DELTA_LINE) return event.deltaX * WHEEL_LINE_PX;
|
|
207
|
-
if (event.deltaMode === event.DOM_DELTA_PAGE) return event.deltaX * viewportWidth;
|
|
208
|
-
return event.deltaX;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/*
|
|
212
|
-
* Measure the blocks track in one pass of layout reads: the viewport is the container's content
|
|
213
|
-
* box (padding shows the neighbouring blocks under it), the blocks are the track's children,
|
|
214
|
-
* with edges taken from their rectangles so fractional widths and gaps stay exact. Only the
|
|
215
|
-
* track's translation moves the rectangles, and every one by the same amount, so their
|
|
216
|
-
* differences are unaffected by it. `contentOffset` shifts the whole range the track rests in
|
|
217
|
-
* (its ends and every snap position) by that many CSS pixels: -20 shows the content 20px
|
|
218
|
-
* further left than the container's content box, at the start and everywhere after.
|
|
219
|
-
*/
|
|
220
|
-
export function measureBlocksTrack(
|
|
221
|
-
container: HTMLElement,
|
|
222
|
-
track: HTMLElement,
|
|
223
|
-
align: BlocksAlign,
|
|
224
|
-
contentOffset: number,
|
|
225
|
-
): BlocksTrackMeasure {
|
|
226
|
-
const style = getComputedStyle(container);
|
|
227
|
-
const viewportWidth = container.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight);
|
|
228
|
-
const trackRect = track.getBoundingClientRect();
|
|
229
|
-
const blocks = Array.from(track.children, (child) => {
|
|
230
|
-
const rect = child.getBoundingClientRect();
|
|
231
|
-
return { left: rect.left - trackRect.left, width: rect.width };
|
|
232
|
-
});
|
|
233
|
-
const maxOffset = Math.max(0, trackRect.width - viewportWidth);
|
|
234
|
-
// The track's offset is how far it has moved left, so content shifted left by 20 is an offset of +20.
|
|
235
|
-
const shift = -contentOffset;
|
|
236
|
-
return {
|
|
237
|
-
viewportWidth,
|
|
238
|
-
minOffset: shift,
|
|
239
|
-
maxOffset: maxOffset + shift,
|
|
240
|
-
offsets: getBlockSnapOffsets(blocks, viewportWidth, maxOffset, align).map((offset) => offset + shift),
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/*
|
|
245
|
-
* The element nearest `target` (itself included) that matches `selector`, only if it is inside
|
|
246
|
-
* `container`, so a carousel placed inside a link or an editor is not mistaken for one.
|
|
247
|
-
*/
|
|
248
|
-
function closestWithin(target: Element, container: Element, selector: string) {
|
|
249
|
-
const match = target.closest(selector);
|
|
250
|
-
return match && container.contains(match) ? match : null;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/*
|
|
254
|
-
* The text node the caret would land in at a viewport point, through whichever caret API the
|
|
255
|
-
* browser has; null when the point is nearest to something other than text.
|
|
256
|
-
*/
|
|
257
|
-
function getTextNodeAtPoint(x: number, y: number) {
|
|
258
|
-
const doc = document as DocumentWithCaretRange;
|
|
259
|
-
const node =
|
|
260
|
-
typeof doc.caretPositionFromPoint === 'function'
|
|
261
|
-
? doc.caretPositionFromPoint(x, y)?.offsetNode
|
|
262
|
-
: doc.caretRangeFromPoint?.(x, y)?.startContainer;
|
|
263
|
-
return node?.nodeType === Node.TEXT_NODE ? (node as Text) : null;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/*
|
|
267
|
-
* Whether a viewport point lies on the glyphs of selectable text inside `container`. The caret
|
|
268
|
-
* APIs answer with the nearest text even from blank space (the empty right side of a paragraph,
|
|
269
|
-
* a block's padding), so the text node's own line boxes decide; text under `user-select: none`
|
|
270
|
-
* (on its element or any ancestor up to the container) does not count.
|
|
271
|
-
*/
|
|
272
|
-
function isSelectableTextAtPoint(container: Element, x: number, y: number) {
|
|
273
|
-
const node = getTextNodeAtPoint(x, y);
|
|
274
|
-
if (!node?.data.trim() || !container.contains(node)) return false;
|
|
275
|
-
const range = document.createRange();
|
|
276
|
-
range.selectNodeContents(node);
|
|
277
|
-
const onGlyphs = Array.from(range.getClientRects()).some(
|
|
278
|
-
(rect) => x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom,
|
|
279
|
-
);
|
|
280
|
-
if (!onGlyphs) return false;
|
|
281
|
-
for (let el = node.parentElement; el; el = el === container ? null : el.parentElement) {
|
|
282
|
-
const style = getComputedStyle(el);
|
|
283
|
-
if ((style.userSelect || style.webkitUserSelect) === 'none') return false;
|
|
284
|
-
}
|
|
285
|
-
return true;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/*
|
|
289
|
-
* What a press at a viewport point on `target` starts. A form field keeps its press (typing, its
|
|
290
|
-
* own selection, a slider's thumb). Links, buttons, draggables, and blank space drag the track;
|
|
291
|
-
* a press there that does not move is still their click. Text is selected, as anywhere else on
|
|
292
|
-
* the page, when `selectText` allows it (the caller turns it off for touch, where a long press
|
|
293
|
-
* is what selects).
|
|
294
|
-
*/
|
|
295
|
-
export function getBlocksPressKind(target: Element, container: Element, x: number, y: number, selectText: boolean): BlocksPressKind {
|
|
296
|
-
if (closestWithin(target, container, 'input, textarea, select, [contenteditable]:not([contenteditable="false"])')) {
|
|
297
|
-
return 'native';
|
|
298
|
-
}
|
|
299
|
-
if (!selectText || closestWithin(target, container, 'a[href], button, [role="button"], [draggable="true"]')) return 'drag';
|
|
300
|
-
return isSelectableTextAtPoint(container, x, y) ? 'text' : 'drag';
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/*
|
|
304
|
-
* Whether an element from `target` up to (not including) `container` can itself scroll further
|
|
305
|
-
* sideways in the direction of `deltaX` (a wide table or code block inside a block), so the wheel
|
|
306
|
-
* is that element's and not the carousel's.
|
|
307
|
-
*/
|
|
308
|
-
export function canScrollSideways(target: Element, container: Element, deltaX: number) {
|
|
309
|
-
for (let el: Element | null = target; el && el !== container; el = el.parentElement) {
|
|
310
|
-
if (el.scrollWidth <= el.clientWidth) continue;
|
|
311
|
-
const { overflowX } = getComputedStyle(el);
|
|
312
|
-
if (overflowX !== 'auto' && overflowX !== 'scroll') continue;
|
|
313
|
-
if (deltaX < 0 ? el.scrollLeft > 0 : Math.ceil(el.scrollLeft + el.clientWidth) < el.scrollWidth) return true;
|
|
314
|
-
}
|
|
315
|
-
return false;
|
|
316
|
-
}
|
package/modules/Carousel.tsx
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { cn } from '@jsb188/app/utils/string.ts';
|
|
2
2
|
import { Icon } from '@jsb188/react-web/svgs/Icon';
|
|
3
|
-
import {
|
|
4
|
-
import type { CSSProperties, KeyboardEvent
|
|
3
|
+
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import type { CSSProperties, KeyboardEvent } from 'react';
|
|
5
5
|
import type { CarouselSceneParams } from '../scenes/Carousel.ts';
|
|
6
6
|
import { normalizeImageEffectSpecs } from '../utils/imageEffectSpecs.ts';
|
|
7
7
|
import type { ImageEffectName, ImageEffectSpec } from '../utils/imageEffectSpecs.ts';
|
|
8
8
|
import { getItemIndex, getVirtualLeft, getVirtualPositions, playVideo, whenVideosPlaying } from './Carousel.helpers.ts';
|
|
9
|
-
import type { BlocksAlign } from './Carousel.helpers.ts';
|
|
10
|
-
import { useBlocksCarousel } from './Carousel.hooks.ts';
|
|
11
9
|
import { GfxCanvas } from './GfxCanvas.tsx';
|
|
12
10
|
import { useContentMemo } from './GfxCanvas.hooks.ts';
|
|
13
11
|
|
|
@@ -19,7 +17,7 @@ export interface VideoCarouselItem {
|
|
|
19
17
|
videoUrl: string;
|
|
20
18
|
/** Shown until the video has a frame, and in place of the video when it cannot play. */
|
|
21
19
|
posterUrl?: string;
|
|
22
|
-
/** Display size of the video in CSS pixels; each item may differ
|
|
20
|
+
/** Display size of the video in CSS pixels; each item may differ. */
|
|
23
21
|
width: number;
|
|
24
22
|
height: number;
|
|
25
23
|
/** Already translated; shown above the video only while this item is focused. */
|
|
@@ -106,16 +104,16 @@ const CarouselCaption = memo(
|
|
|
106
104
|
aria-hidden={phase === 'out'}
|
|
107
105
|
>
|
|
108
106
|
<div
|
|
109
|
-
className="v_bottom a_c pb_md h_f
|
|
107
|
+
className="v_bottom a_c pb_md h_f max_w_f"
|
|
110
108
|
style={{ width }}
|
|
111
109
|
>
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
110
|
+
{item.iconName && (
|
|
111
|
+
<div className="h_center mb_sm">
|
|
112
|
+
<Icon name={item.iconName} />
|
|
113
|
+
</div>
|
|
114
|
+
)}
|
|
115
|
+
<div className="ft_semibold ft_lg">{item.title}</div>
|
|
116
|
+
{item.description && <div className="op_70 mt_xs">{item.description}</div>}
|
|
119
117
|
</div>
|
|
120
118
|
</div>
|
|
121
119
|
);
|
|
@@ -190,11 +188,10 @@ const CarouselSlide = memo(
|
|
|
190
188
|
: { className: 'abs_tl h_f v_top' })}
|
|
191
189
|
style={{ left, width, transition: transition && `left ${transition}, width ${transition}` }}
|
|
192
190
|
>
|
|
193
|
-
|
|
194
|
-
<div className="v_top w_f">
|
|
191
|
+
<div className="h_center f w_f">
|
|
195
192
|
{/* The video's own backdrop: a dimmed video fades toward it instead of revealing the page; the scene reads this background for the drawn copy. */}
|
|
196
193
|
<div
|
|
197
|
-
className="
|
|
194
|
+
className="bg_active of max_w_f"
|
|
198
195
|
style={{ width: item.width, borderRadius }}
|
|
199
196
|
>
|
|
200
197
|
<video
|
|
@@ -224,12 +221,11 @@ const CarouselSlide = memo(
|
|
|
224
221
|
|
|
225
222
|
/*
|
|
226
223
|
* Horizontal carousel of videos laid out edge to edge across the full width of its container;
|
|
227
|
-
* whatever falls past the left or right edge is clipped (never widening the page).
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
* videos), which never moves with them, shows the focused item's icon, title, and
|
|
224
|
+
* whatever falls past the left or right edge is clipped (never widening the page). The focused
|
|
225
|
+
* slide sits centered in a box `focusedContentWidth` wide; every other slide shows its video at
|
|
226
|
+
* its own size, dimmed to `unfocusedOpacity` over a `bg_active` backdrop (so the page never
|
|
227
|
+
* shows through it), through `effect` (pixelated by default). Above the track a text area of
|
|
228
|
+
* fixed height, which never moves with the videos, shows the focused item's icon, title, and
|
|
233
229
|
* description; when focus moves, that caption fades out sliding a little in the direction the
|
|
234
230
|
* videos travel while the next one fades in sliding into place from the other side. The list is
|
|
235
231
|
* endless: slides are laid out along an unbounded track and the items repeat, so focus only ever
|
|
@@ -246,8 +242,8 @@ const CarouselSlide = memo(
|
|
|
246
242
|
export const VideoCarousel = memo(
|
|
247
243
|
(p: {
|
|
248
244
|
items: VideoCarouselItem[];
|
|
249
|
-
/** Width of the
|
|
250
|
-
|
|
245
|
+
/** Width of the focused slide's box (texts and video); it never changes with the item. */
|
|
246
|
+
focusedContentWidth: number;
|
|
251
247
|
/** Controlled focused item; a change moves forward around the loop to that item. Omit to let clicks and autoplay move focus on their own. */
|
|
252
248
|
focusedIndex?: number;
|
|
253
249
|
defaultFocusedIndex?: number;
|
|
@@ -277,7 +273,7 @@ export const VideoCarousel = memo(
|
|
|
277
273
|
}) => {
|
|
278
274
|
const {
|
|
279
275
|
items,
|
|
280
|
-
|
|
276
|
+
focusedContentWidth,
|
|
281
277
|
focusedIndex: controlledIndex,
|
|
282
278
|
defaultFocusedIndex = 0,
|
|
283
279
|
onFocusedIndexChange,
|
|
@@ -445,11 +441,12 @@ export const VideoCarousel = memo(
|
|
|
445
441
|
[slides, effect, transitionMs, borderRadius, unfocusedOpacity, backdropColor],
|
|
446
442
|
);
|
|
447
443
|
|
|
448
|
-
// Slides sit at their natural-width positions along the endless track,
|
|
449
|
-
//
|
|
450
|
-
// leaving move nothing else.
|
|
444
|
+
// Slides sit at their natural-width positions along the endless track, with everything right of
|
|
445
|
+
// the focused slide pushed by its widening to the focused box; the track is shifted so that box
|
|
446
|
+
// lands centered. All coordinates are absolute, so slides entering or leaving move nothing else.
|
|
451
447
|
const focusedLeft = getVirtualLeft(position, widths, gap);
|
|
452
|
-
const
|
|
448
|
+
const focusedShift = focusedContentWidth - widths[focusedItemIndex];
|
|
449
|
+
const trackOffset = containerWidth / 2 - focusedContentWidth / 2 - focusedLeft;
|
|
453
450
|
const trackHeight = Math.max(...items.map((item) => item.height));
|
|
454
451
|
const transition = containerWidth ? `${transitionMs}ms ${CAROUSEL.easing}` : undefined;
|
|
455
452
|
const videosHidden = canvasStatus === 'ready';
|
|
@@ -475,7 +472,7 @@ export const VideoCarousel = memo(
|
|
|
475
472
|
item={items[getItemIndex(caption.position, count)]}
|
|
476
473
|
phase={caption.phase}
|
|
477
474
|
direction={caption.direction}
|
|
478
|
-
width={
|
|
475
|
+
width={focusedContentWidth}
|
|
479
476
|
durationMs={transitionMs}
|
|
480
477
|
/>
|
|
481
478
|
))}
|
|
@@ -499,8 +496,8 @@ export const VideoCarousel = memo(
|
|
|
499
496
|
item={items[getItemIndex(v, count)]}
|
|
500
497
|
focused={focused}
|
|
501
498
|
interactive={!focused && !paused}
|
|
502
|
-
left={getVirtualLeft(v, widths, gap)}
|
|
503
|
-
width={widths[getItemIndex(v, count)]}
|
|
499
|
+
left={getVirtualLeft(v, widths, gap) + (v > position ? focusedShift : 0)}
|
|
500
|
+
width={focused ? focusedContentWidth : widths[getItemIndex(v, count)]}
|
|
504
501
|
borderRadius={borderRadius}
|
|
505
502
|
transition={transition}
|
|
506
503
|
videoOpacity={videosHidden ? 0 : focused ? 1 : unfocusedOpacity}
|
|
@@ -526,106 +523,3 @@ export const VideoCarousel = memo(
|
|
|
526
523
|
);
|
|
527
524
|
},
|
|
528
525
|
);
|
|
529
|
-
|
|
530
|
-
/*
|
|
531
|
-
* A row of blocks (its children, each any width, all stretched to the tallest) that the visitor
|
|
532
|
-
* drags with any pointer or scrolls sideways with a wheel or trackpad; whatever falls past the
|
|
533
|
-
* container's edges is clipped. When the drag or the scroll ends, the track glides to the
|
|
534
|
-
* nearest snap position: a block's left edge at the container's left edge (`align='start'`), or
|
|
535
|
-
* its middle at the container's middle. The track stops at its ends (the last blocks share the
|
|
536
|
-
* end position), so nothing loops, and nothing moves on its own. A drag past an end gives with
|
|
537
|
-
* rubber-band resistance and springs back; a flick always leaves the current block, and a wheel
|
|
538
|
-
* notch too short to reach the next block still advances one. A click on a block stays a click
|
|
539
|
-
* unless the pointer moved first, so blocks can hold buttons and links. With `selectableText` (the
|
|
540
|
-
* default) a mouse or pen press on text selects it the way it would anywhere on the page, and
|
|
541
|
-
* the drag starts from everything else (blank space, images, links, buttons); the cursor shows
|
|
542
|
-
* which one a press would do. On a touch screen a sideways swipe drags and a long press selects.
|
|
543
|
-
* Form fields inside the blocks are never dragged from. `index` / `defaultIndex`
|
|
544
|
-
* / `onIndexChange` name the block at the snap position; changing `index` glides there. Keyboard
|
|
545
|
-
* focus landing in a clipped block brings it into view. Padding on the container (through
|
|
546
|
-
* `className` or `style`) shows the neighbouring blocks under it. `lock={false}` turns the
|
|
547
|
-
* snapping off: a gesture leaves the track wherever it put it (a flick coasts a little further,
|
|
548
|
-
* and a drag past an end still springs back), while `index` and keyboard focus still glide to
|
|
549
|
-
* a block's snap position. `contentOffset` shifts where the content sits, at the start and
|
|
550
|
-
* throughout: -20 shows it 20px further left than the container's content box.
|
|
551
|
-
*/
|
|
552
|
-
export const BlocksCarousel = memo(
|
|
553
|
-
(p: {
|
|
554
|
-
children: ReactNode;
|
|
555
|
-
/** Space between blocks in CSS pixels. */
|
|
556
|
-
gap?: number;
|
|
557
|
-
/** Where the current block rests: its left edge at the container's left edge, or centered in it. */
|
|
558
|
-
align?: BlocksAlign;
|
|
559
|
-
/** Controlled current block; a change glides the track to it. Omit to let gestures move it on their own. */
|
|
560
|
-
index?: number;
|
|
561
|
-
defaultIndex?: number;
|
|
562
|
-
/** Called with the block a gesture picked, as soon as it is picked (the glide there is still under way). */
|
|
563
|
-
onIndexChange?: (index: number) => void;
|
|
564
|
-
/** Duration of the glide to a snap position, in ms. */
|
|
565
|
-
transitionMs?: number;
|
|
566
|
-
/** Whether a finished drag or scroll locks the track onto the nearest block; `false` leaves it where the gesture ended. */
|
|
567
|
-
lock?: boolean;
|
|
568
|
-
/** Shift of the content in CSS pixels, at the start and throughout: -20 shows it 20px further left. */
|
|
569
|
-
contentOffset?: number;
|
|
570
|
-
/** Whether a mouse or pen press on text selects it instead of dragging the track; `false` drags from anywhere. */
|
|
571
|
-
selectableText?: boolean;
|
|
572
|
-
className?: string;
|
|
573
|
-
style?: CSSProperties;
|
|
574
|
-
}) => {
|
|
575
|
-
const {
|
|
576
|
-
children,
|
|
577
|
-
gap = 16,
|
|
578
|
-
align = 'start',
|
|
579
|
-
index,
|
|
580
|
-
defaultIndex = 0,
|
|
581
|
-
onIndexChange,
|
|
582
|
-
transitionMs = 600,
|
|
583
|
-
lock = true,
|
|
584
|
-
contentOffset = 0,
|
|
585
|
-
selectableText = true,
|
|
586
|
-
className,
|
|
587
|
-
style,
|
|
588
|
-
} = p;
|
|
589
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
590
|
-
const trackRef = useRef<HTMLDivElement>(null);
|
|
591
|
-
const handlers = useBlocksCarousel(containerRef, trackRef, {
|
|
592
|
-
align,
|
|
593
|
-
index,
|
|
594
|
-
defaultIndex,
|
|
595
|
-
onIndexChange,
|
|
596
|
-
transitionMs,
|
|
597
|
-
lock,
|
|
598
|
-
contentOffset,
|
|
599
|
-
selectableText,
|
|
600
|
-
});
|
|
601
|
-
|
|
602
|
-
return (
|
|
603
|
-
<div
|
|
604
|
-
ref={containerRef}
|
|
605
|
-
// Vertical pans and pinches stay the browser's, so a finger can still scroll the page across the carousel; sideways ones are the drag.
|
|
606
|
-
// `cs_grab` is the cursor until the pointer first moves over the blocks; the hook writes it from then on.
|
|
607
|
-
className={cn('rel of cs_grab', className)}
|
|
608
|
-
style={{ ...style, touchAction: 'pan-y pinch-zoom' }}
|
|
609
|
-
{...handlers}
|
|
610
|
-
>
|
|
611
|
-
<div
|
|
612
|
-
ref={trackRef}
|
|
613
|
-
// As wide as its blocks (never shrunk to the container), moved by its transform alone. The first block's start
|
|
614
|
-
// position is rendered already, so server-rendered HTML does not jump by `contentOffset` when the hook takes over.
|
|
615
|
-
className="rel h_stretch"
|
|
616
|
-
style={{ gap, width: 'max-content', willChange: 'transform', transform: `translate3d(${contentOffset}px, 0, 0)` }}
|
|
617
|
-
>
|
|
618
|
-
{/* Each block in its own wrapper, so blocks are measured without needing refs of their own, and stretched to the row's height. */}
|
|
619
|
-
{Children.toArray(children).map((child, i) => (
|
|
620
|
-
<div
|
|
621
|
-
key={isValidElement(child) ? (child.key ?? i) : i}
|
|
622
|
-
className="no_shrink h_stretch"
|
|
623
|
-
>
|
|
624
|
-
{child}
|
|
625
|
-
</div>
|
|
626
|
-
))}
|
|
627
|
-
</div>
|
|
628
|
-
</div>
|
|
629
|
-
);
|
|
630
|
-
},
|
|
631
|
-
);
|