@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.
- package/README.md +522 -0
- 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/chain.ts +174 -0
- package/libs/dotGrid.helpers.ts +53 -0
- package/libs/dotGrid.ts +954 -0
- package/libs/effects/dotEffects.ts +136 -0
- package/libs/effects/effectQuad.ts +146 -0
- package/libs/effects/effectRegion.ts +299 -0
- package/libs/effects/imageEffects.ts +599 -0
- package/libs/effects/index.ts +8 -0
- package/libs/effects/ripple.ts +160 -0
- package/libs/glsl.ts +134 -0
- package/libs/passes.ts +374 -0
- package/libs/pointer.ts +151 -0
- package/libs/runtime.ts +329 -0
- package/libs/texture.ts +160 -0
- package/libs/types.ts +94 -0
- package/libs/uniforms.ts +32 -0
- package/main.ts +4 -0
- package/modules/Carousel.helpers.ts +316 -0
- package/modules/Carousel.hooks.ts +520 -0
- package/modules/Carousel.tsx +631 -0
- 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.hooks.ts +12 -0
- package/modules/GfxCanvas.tsx +189 -0
- package/modules/Hero.tsx +90 -0
- 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 +98 -0
- package/package.json +52 -0
- package/scenes/Carousel.ts +277 -0
- package/scenes/DotsBoard.helpers.ts +12 -0
- package/scenes/DotsBoard.ts +245 -0
- package/scenes/Hero.ts +280 -0
- package/tsconfig.json +13 -0
- package/utils/align.ts +18 -0
- package/utils/color.ts +112 -0
- package/utils/dotEffectSpecs.ts +79 -0
- package/utils/dotGridSpecs.ts +469 -0
- package/utils/easing.ts +18 -0
- package/utils/imageEffectSpecs.ts +89 -0
- package/utils/motion.ts +88 -0
- package/utils/random.ts +22 -0
- package/utils/strings.ts +116 -0
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dot grid options, their defaults, a matching static SVG tile, and the CSS that draws the grid
|
|
3
|
+
* with it. This module has no runtime dependency on OGL, so route bundles can import it to
|
|
4
|
+
* render a CSS fallback of the same grid before (or without) the engine.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { CSSProperties } from 'react';
|
|
8
|
+
import { ALIGN_X, ALIGN_Y } from './align.ts';
|
|
9
|
+
import type { AlignX, AlignY } from './align.ts';
|
|
10
|
+
import { DOT_EFFECT_DEFAULTS, DOT_SHAPES, resolveDotEffectOptions } from './dotEffectSpecs.ts';
|
|
11
|
+
import type { DotEffectOptions, DotShape } from './dotEffectSpecs.ts';
|
|
12
|
+
|
|
13
|
+
export { DOT_SHAPES, DOT_EFFECTS } from './dotEffectSpecs.ts';
|
|
14
|
+
export type { DotEffect, DotEffectSelection, DotEffectOptions, DotShape } from './dotEffectSpecs.ts';
|
|
15
|
+
|
|
16
|
+
/** Where the grid is anchored along each axis, like the keywords of CSS `mask-position`. */
|
|
17
|
+
export const DOT_ALIGN_X = ALIGN_X;
|
|
18
|
+
export const DOT_ALIGN_Y = ALIGN_Y;
|
|
19
|
+
export type DotAlignX = AlignX;
|
|
20
|
+
export type DotAlignY = AlignY;
|
|
21
|
+
/** A dot center a number of CSS pixels in from one edge; `{ left }` / `{ top }` are the same as a bare number. */
|
|
22
|
+
export type DotAlignXOffset = { left: number } | { right: number };
|
|
23
|
+
export type DotAlignYOffset = { top: number } | { bottom: number };
|
|
24
|
+
/** Any anchor along either axis, as the grid helpers accept it. */
|
|
25
|
+
export type DotAlign = DotAlignX | DotAlignY | number | DotAlignXOffset | DotAlignYOffset;
|
|
26
|
+
|
|
27
|
+
/** The grid at rest. */
|
|
28
|
+
export interface DotStyle {
|
|
29
|
+
/** Dot diameter in CSS pixels. */
|
|
30
|
+
size?: number;
|
|
31
|
+
/** Shape of every dot; thrown dots keep it. */
|
|
32
|
+
shape?: DotShape;
|
|
33
|
+
/** Distance between dot centers in CSS pixels. */
|
|
34
|
+
spacing?: number;
|
|
35
|
+
/** Hex dot color; ignored while an overlay image shows through the dots. */
|
|
36
|
+
color?: string;
|
|
37
|
+
/** 0..1 opacity of the dots. */
|
|
38
|
+
opacity?: number;
|
|
39
|
+
/** Horizontal anchor: `center` puts a column on the middle of the area; `left` hides the first column just past the left edge (its center one `size` out, so the first visible column is about a spacing in); `right` sets the last column flush against the right edge; a number (or `{ left }`) puts a column's center that many CSS pixels from the left edge, `{ right }` that many from the right edge. */
|
|
40
|
+
alignX?: DotAlignX | number | DotAlignXOffset;
|
|
41
|
+
/** Vertical anchor: `center` puts a row on the middle of the area; `top` hides the first row just past the top edge (its center one `size` up, so the first visible row is about a spacing down); `bottom` sets the last row flush against the bottom edge; a number (or `{ top }`) puts a row's center that many CSS pixels from the top edge, `{ bottom }` that many from the bottom edge. */
|
|
42
|
+
alignY?: DotAlignY | number | DotAlignYOffset;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** How a moving cursor throws the dots, and how they come back. */
|
|
46
|
+
export interface DotKick {
|
|
47
|
+
/** Radius in CSS pixels around the moving cursor within which dots are kicked. */
|
|
48
|
+
radius?: number;
|
|
49
|
+
/** A fast swipe throws dots about `radius × strength` pixels; negative pulls inward. */
|
|
50
|
+
strength?: number;
|
|
51
|
+
/** Extra per-dot variation in strength and direction, from 0 (default) to 1; stable across hover frames. */
|
|
52
|
+
randomness?: number;
|
|
53
|
+
/** Milliseconds for a thrown dot to settle back home. */
|
|
54
|
+
bounceDuration?: number;
|
|
55
|
+
/** 0..1 how springy the return is: 0 glides back with no overshoot, 1 rings for the whole duration. */
|
|
56
|
+
bounciness?: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The look of a dot while it is thrown from home, fading back as it settles. */
|
|
60
|
+
export interface ThrownDotStyle {
|
|
61
|
+
/** Hex color blended over the dot; omit to keep its color. */
|
|
62
|
+
color?: string | null;
|
|
63
|
+
/** 0..1 opacity; defaults to the resting opacity. */
|
|
64
|
+
opacity?: number;
|
|
65
|
+
/** Size multiplier: 4 draws a fully thrown dot four times as large. */
|
|
66
|
+
scale?: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A random handful of dots animate each cycle, once the reveal finishes. Defaults to a simple swell. */
|
|
70
|
+
export interface DotPulse extends DotEffectOptions {
|
|
71
|
+
/** About how many dots animate per cycle. */
|
|
72
|
+
count?: number;
|
|
73
|
+
/** Milliseconds per cycle; a new random set is picked each cycle. */
|
|
74
|
+
interval?: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A shared effect triggered at the click or tap position. */
|
|
78
|
+
export interface DotClick extends DotEffectOptions {
|
|
79
|
+
/** Ripple-click impulse strength, independent of cursor kicks; 0 (default) disables it, negative pulls inward. */
|
|
80
|
+
kickStrength?: number;
|
|
81
|
+
/** Radius in CSS pixels of the ripple-click impulse, independent of the visual ripple and cursor; defaults to 80. */
|
|
82
|
+
kickRadius?: number;
|
|
83
|
+
/** Extra per-click variation in each dot's impulse strength and direction, from 0 (default) to 1. */
|
|
84
|
+
kickRandomness?: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** An effect across the resting grid, repeated without pointer interaction after the reveal. */
|
|
88
|
+
export interface DotRest extends DotEffectOptions {
|
|
89
|
+
/** Milliseconds between cycles; duration is capped at this interval. */
|
|
90
|
+
interval?: number;
|
|
91
|
+
/** Center of the area, or a new random point inside it each cycle. */
|
|
92
|
+
origin?: 'center' | 'random';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A fade along the bottom edge, this many CSS pixels high: to transparent, or with `color` (hex) to that flat color. */
|
|
96
|
+
export type DotFade = number | { height: number; color?: string };
|
|
97
|
+
|
|
98
|
+
/** Intro: shrink a filled area into dots, or grow dots from an empty background. */
|
|
99
|
+
export interface DotReveal {
|
|
100
|
+
/** `fade_out` (default) shrinks the filled image or color into dots; `appear` grows dots from `initialSize`. */
|
|
101
|
+
effect?: 'fade_out' | 'appear';
|
|
102
|
+
/** Starting diameter in CSS pixels for `appear`. Defaults to 0 (an empty background). */
|
|
103
|
+
initialSize?: number;
|
|
104
|
+
/** Peak diameter in CSS pixels for `appear`, then shrink to `dots.size`. Defaults to `dots.size`; smaller values are clamped to it. */
|
|
105
|
+
appearSize?: number;
|
|
106
|
+
/** Milliseconds the initial state holds before the dots animate. */
|
|
107
|
+
delay?: number;
|
|
108
|
+
/** Milliseconds for the whole reveal, including growth and settling for `appear`. */
|
|
109
|
+
duration?: number;
|
|
110
|
+
/** Scale the overlay image starts at on mount (1.25 = 125%), easing to 1 over `zoomDuration`; 1 for no zoom. Runs independently of `delay`: the dots start shrinking when the delay is up whether or not the zoom has finished. Only with an image. */
|
|
111
|
+
zoomOnMount?: number;
|
|
112
|
+
/** Milliseconds the zoom to 100% takes. */
|
|
113
|
+
zoomDuration?: number;
|
|
114
|
+
/** A bottom fade for `fade_out`'s filled state; it holds through the shrink and then hands over to the dots' fade. `appear` uses the dots' fade throughout. */
|
|
115
|
+
fadeBottom?: DotFade;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Resolved options are themselves valid options, so a scene can hand them straight to the grid. */
|
|
119
|
+
export interface DotGridOptions {
|
|
120
|
+
dots?: DotStyle;
|
|
121
|
+
/** `false` disables cursor kicks; click kicks have independent settings. */
|
|
122
|
+
kick?: DotKick | false | null;
|
|
123
|
+
/** `false` (or omitted) leaves thrown dots looking like resting ones. */
|
|
124
|
+
thrown?: ThrownDotStyle | false | null;
|
|
125
|
+
/** `true` for the default timing. Kicks are ignored until the reveal has finished. */
|
|
126
|
+
reveal?: DotReveal | boolean | null;
|
|
127
|
+
/** `true` for the defaults; `false` (or omitted) for no pulsing. */
|
|
128
|
+
pulse?: DotPulse | boolean | null;
|
|
129
|
+
/** `true` for the default ripple; `false` (or omitted) disables click effects. */
|
|
130
|
+
click?: DotClick | boolean | null;
|
|
131
|
+
/** Ambient effects across resting dots; omitted or false leaves the grid at rest. */
|
|
132
|
+
rest?: DotRest | boolean | null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface ResolvedDotGridOptions {
|
|
136
|
+
dots: Required<DotStyle>;
|
|
137
|
+
/** Null when cursor kicks are disabled. */
|
|
138
|
+
kick: Required<DotKick> | null;
|
|
139
|
+
/** Null when thrown dots keep the resting look. */
|
|
140
|
+
thrown: (Required<Omit<ThrownDotStyle, 'color'>> & { color: string | null }) | null;
|
|
141
|
+
/** Null when there is no intro. The filled state's bottom fade is `fadeBottom` pixels high (0 for none), to `fadeColor` (null for transparent). */
|
|
142
|
+
reveal: (Required<Omit<DotReveal, 'fadeBottom'>> & { fadeBottom: number; fadeColor: string | null }) | null;
|
|
143
|
+
/** Null when nothing pulses. */
|
|
144
|
+
pulse: Required<DotPulse> | null;
|
|
145
|
+
/** Null when clicks have no effect. */
|
|
146
|
+
click: Required<DotClick> | null;
|
|
147
|
+
/** Null when there is no animation across the resting grid. */
|
|
148
|
+
rest: Required<DotRest> | null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Matches the `.pattern_speckle` mask tile: a 7×7 tile with a centered 2.25px dot. */
|
|
152
|
+
export const DOT_GRID_DEFAULTS = {
|
|
153
|
+
dots: {
|
|
154
|
+
size: 2.25,
|
|
155
|
+
shape: 'circle' as DotShape,
|
|
156
|
+
spacing: 7,
|
|
157
|
+
color: '#ffffff',
|
|
158
|
+
opacity: 1,
|
|
159
|
+
alignX: 'center' as DotAlignX | number | DotAlignXOffset,
|
|
160
|
+
alignY: 'center' as DotAlignY | number | DotAlignYOffset,
|
|
161
|
+
},
|
|
162
|
+
kick: { radius: 80, strength: 1, randomness: 0, bounceDuration: 1200, bounciness: 0.5 },
|
|
163
|
+
reveal: { effect: 'fade_out' as const, initialSize: 0, delay: 1000, duration: 800, zoomOnMount: 1, zoomDuration: 1000 },
|
|
164
|
+
pulse: { count: 20, interval: 3000, duration: 1500, scale: 3 },
|
|
165
|
+
click: { ...DOT_EFFECT_DEFAULTS, kickStrength: 0, kickRadius: 80, kickRandomness: 0 },
|
|
166
|
+
rest: { interval: 6000, duration: 1800, scale: 2, origin: 'center' as const },
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/*
|
|
170
|
+
* A bottom fade's height in CSS pixels (0 without one) and the flat color it fades to (null
|
|
171
|
+
* for transparent).
|
|
172
|
+
*/
|
|
173
|
+
export function resolveDotFade(fade: DotFade | undefined) {
|
|
174
|
+
return {
|
|
175
|
+
height: Math.max(0, (typeof fade === 'number' ? fade : fade?.height) ?? 0),
|
|
176
|
+
color: (typeof fade === 'object' && fade.color) || null,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/*
|
|
181
|
+
* Clamp a number into a range, falling back to a default when it is not given.
|
|
182
|
+
*/
|
|
183
|
+
function clampOption(value: number | undefined, fallback: number, min: number, max = Number.POSITIVE_INFINITY) {
|
|
184
|
+
return Math.min(max, Math.max(min, value ?? fallback));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/*
|
|
188
|
+
* The pixel offset of an `{ edge: px }` anchor when it names one of `edges` with a finite
|
|
189
|
+
* number, or null when it is anything else.
|
|
190
|
+
*/
|
|
191
|
+
function getAlignOffset(value: unknown, edges: readonly string[]): { edge: string; offset: number } | null {
|
|
192
|
+
if (!value || typeof value !== 'object') return null;
|
|
193
|
+
for (const edge of edges) {
|
|
194
|
+
const offset = (value as Record<string, unknown>)[edge];
|
|
195
|
+
if (typeof offset === 'number' && Number.isFinite(offset)) return { edge, offset };
|
|
196
|
+
}
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/*
|
|
201
|
+
* An alignment as given, when it is one of the keywords, a finite pixel offset, or an
|
|
202
|
+
* `{ edge: px }` offset from the axis's start or end edge; the default otherwise.
|
|
203
|
+
*/
|
|
204
|
+
function resolveAlign<T extends string, O>(value: T | number | O | undefined, keywords: readonly T[], fallback: T | number | O) {
|
|
205
|
+
if (typeof value === 'number') return Number.isFinite(value) ? value : fallback;
|
|
206
|
+
if (getAlignOffset(value, keywords)) return value as O;
|
|
207
|
+
return typeof value === 'string' && keywords.includes(value as T) ? (value as T) : fallback;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/*
|
|
211
|
+
* Fill in defaults and clamp to values the renderer can lay out (a zero spacing would mean an
|
|
212
|
+
* infinite grid). A kick with a non-positive radius or zero strength is off; the strength
|
|
213
|
+
* keeps its sign so negative pulls dots toward the cursor.
|
|
214
|
+
*/
|
|
215
|
+
export function resolveDotGridOptions(options: DotGridOptions = {}): ResolvedDotGridOptions {
|
|
216
|
+
const { dots = {}, kick = {}, thrown = false, reveal = false, pulse = false, click = false, rest = false } = options;
|
|
217
|
+
const shape = dots.shape && DOT_SHAPES.includes(dots.shape) ? dots.shape : DOT_GRID_DEFAULTS.dots.shape;
|
|
218
|
+
const revealOptions = reveal === true ? {} : reveal;
|
|
219
|
+
const revealFade = resolveDotFade(revealOptions ? revealOptions.fadeBottom : undefined);
|
|
220
|
+
const pulseOptions = pulse === true ? {} : pulse;
|
|
221
|
+
const clickOptions = click === true ? {} : click;
|
|
222
|
+
const restOptions = rest === true ? {} : rest;
|
|
223
|
+
const restInterval = restOptions ? clampOption(restOptions.interval, DOT_GRID_DEFAULTS.rest.interval, 1) : 0;
|
|
224
|
+
const pulseInterval = pulseOptions ? clampOption(pulseOptions.interval, DOT_GRID_DEFAULTS.pulse.interval, 1) : 0;
|
|
225
|
+
const opacity = clampOption(dots.opacity, DOT_GRID_DEFAULTS.dots.opacity, 0, 1);
|
|
226
|
+
const dotSize = clampOption(dots.size, DOT_GRID_DEFAULTS.dots.size, 0);
|
|
227
|
+
const kickRadius = kick ? clampOption(kick.radius, DOT_GRID_DEFAULTS.kick.radius, 0) : 0;
|
|
228
|
+
const kickStrength = kick ? (kick.strength ?? DOT_GRID_DEFAULTS.kick.strength) : 0;
|
|
229
|
+
return {
|
|
230
|
+
dots: {
|
|
231
|
+
size: dotSize,
|
|
232
|
+
shape,
|
|
233
|
+
spacing: clampOption(dots.spacing, DOT_GRID_DEFAULTS.dots.spacing, 1),
|
|
234
|
+
color: dots.color ?? DOT_GRID_DEFAULTS.dots.color,
|
|
235
|
+
opacity,
|
|
236
|
+
alignX: resolveAlign(dots.alignX, DOT_ALIGN_X, DOT_GRID_DEFAULTS.dots.alignX),
|
|
237
|
+
alignY: resolveAlign(dots.alignY, DOT_ALIGN_Y, DOT_GRID_DEFAULTS.dots.alignY),
|
|
238
|
+
},
|
|
239
|
+
kick:
|
|
240
|
+
kick && kickRadius > 0 && kickStrength !== 0
|
|
241
|
+
? {
|
|
242
|
+
radius: kickRadius,
|
|
243
|
+
strength: kickStrength,
|
|
244
|
+
randomness: clampOption(kick.randomness, DOT_GRID_DEFAULTS.kick.randomness, 0, 1),
|
|
245
|
+
bounceDuration: clampOption(kick.bounceDuration, DOT_GRID_DEFAULTS.kick.bounceDuration, 50),
|
|
246
|
+
bounciness: clampOption(kick.bounciness, DOT_GRID_DEFAULTS.kick.bounciness, 0, 1),
|
|
247
|
+
}
|
|
248
|
+
: null,
|
|
249
|
+
thrown: thrown
|
|
250
|
+
? {
|
|
251
|
+
color: thrown.color ?? null,
|
|
252
|
+
opacity: clampOption(thrown.opacity, opacity, 0, 1),
|
|
253
|
+
scale: clampOption(thrown.scale, 1, 0),
|
|
254
|
+
}
|
|
255
|
+
: null,
|
|
256
|
+
reveal: revealOptions
|
|
257
|
+
? {
|
|
258
|
+
effect: revealOptions.effect === 'appear' ? 'appear' : DOT_GRID_DEFAULTS.reveal.effect,
|
|
259
|
+
initialSize: clampOption(revealOptions.initialSize, DOT_GRID_DEFAULTS.reveal.initialSize, 0),
|
|
260
|
+
appearSize: clampOption(revealOptions.appearSize, dotSize, dotSize),
|
|
261
|
+
delay: clampOption(revealOptions.delay, DOT_GRID_DEFAULTS.reveal.delay, 0),
|
|
262
|
+
duration: clampOption(revealOptions.duration, DOT_GRID_DEFAULTS.reveal.duration, 1),
|
|
263
|
+
zoomOnMount: clampOption(revealOptions.zoomOnMount, DOT_GRID_DEFAULTS.reveal.zoomOnMount, 0.01),
|
|
264
|
+
zoomDuration: clampOption(revealOptions.zoomDuration, DOT_GRID_DEFAULTS.reveal.zoomDuration, 1),
|
|
265
|
+
fadeBottom: revealFade.height,
|
|
266
|
+
fadeColor: revealFade.color,
|
|
267
|
+
}
|
|
268
|
+
: null,
|
|
269
|
+
pulse: pulseOptions
|
|
270
|
+
? {
|
|
271
|
+
...resolveDotEffectOptions(pulseOptions, {
|
|
272
|
+
...DOT_GRID_DEFAULTS.pulse,
|
|
273
|
+
effect: 'swell',
|
|
274
|
+
shape,
|
|
275
|
+
opacity,
|
|
276
|
+
radius: (dots.spacing ?? DOT_GRID_DEFAULTS.dots.spacing) * 3,
|
|
277
|
+
}),
|
|
278
|
+
count: clampOption(pulseOptions.count, DOT_GRID_DEFAULTS.pulse.count, 0),
|
|
279
|
+
interval: pulseInterval,
|
|
280
|
+
duration: clampOption(pulseOptions.duration, DOT_GRID_DEFAULTS.pulse.duration, 1, pulseInterval),
|
|
281
|
+
}
|
|
282
|
+
: null,
|
|
283
|
+
click: clickOptions
|
|
284
|
+
? {
|
|
285
|
+
...resolveDotEffectOptions(clickOptions, { shape, opacity }),
|
|
286
|
+
kickStrength: clampOption(clickOptions.kickStrength, DOT_GRID_DEFAULTS.click.kickStrength, Number.NEGATIVE_INFINITY),
|
|
287
|
+
kickRadius: clampOption(clickOptions.kickRadius, DOT_GRID_DEFAULTS.click.kickRadius, 0),
|
|
288
|
+
kickRandomness: clampOption(clickOptions.kickRandomness, DOT_GRID_DEFAULTS.click.kickRandomness, 0, 1),
|
|
289
|
+
}
|
|
290
|
+
: null,
|
|
291
|
+
rest: restOptions
|
|
292
|
+
? {
|
|
293
|
+
...resolveDotEffectOptions(restOptions, { ...DOT_GRID_DEFAULTS.rest, shape, opacity }),
|
|
294
|
+
interval: restInterval,
|
|
295
|
+
duration: clampOption(restOptions.duration, DOT_GRID_DEFAULTS.rest.duration, 1, restInterval),
|
|
296
|
+
origin: restOptions.origin === 'random' ? 'random' : DOT_GRID_DEFAULTS.rest.origin,
|
|
297
|
+
}
|
|
298
|
+
: null,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/*
|
|
303
|
+
* Whether the grid ever reacts to the pointer: a kick is configured.
|
|
304
|
+
*/
|
|
305
|
+
export function isDotGridInteractive(options: ResolvedDotGridOptions) {
|
|
306
|
+
return options.kick !== null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Corner radius of a rounded square as a fraction of its half-size. */
|
|
310
|
+
export const DOT_ROUNDED_SQUARE_CORNER = 0.5;
|
|
311
|
+
|
|
312
|
+
/** Ring hole radius as a fraction of the dot's outer radius, shared by SVG and WebGL. */
|
|
313
|
+
export const DOT_RING_INNER_RADIUS = 0.6;
|
|
314
|
+
|
|
315
|
+
/*
|
|
316
|
+
* Dot diameter in CSS pixels at which a square grid leaves no pixel uncovered, plus a little
|
|
317
|
+
* for the anti-aliased rim. A circle must reach the cell corners, √2 × spacing away; a rounded
|
|
318
|
+
* square only needs to span the cell plus what its rounded corners cut away.
|
|
319
|
+
*/
|
|
320
|
+
export function getDotGridCoverSize(spacing: number, shape: DotShape) {
|
|
321
|
+
if (shape === 'rounded_square' || shape === 'ring_rectangle') {
|
|
322
|
+
// Half-size h covers the corner (spacing/2, spacing/2) when (spacing/2 - h + c)·√2 ≤ c, c = corner·h.
|
|
323
|
+
const corner = DOT_ROUNDED_SQUARE_CORNER;
|
|
324
|
+
return spacing / (1 - corner * (1 - Math.SQRT1_2)) + 1;
|
|
325
|
+
}
|
|
326
|
+
return spacing * Math.SQRT2 + 1;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/*
|
|
330
|
+
* Whether an anchor is measured from the axis's end edge (`right` / `bottom`, as a keyword or
|
|
331
|
+
* an `{ edge: px }` offset).
|
|
332
|
+
*/
|
|
333
|
+
function isEndAlign(align: DotAlign): boolean {
|
|
334
|
+
return align === 'right' || align === 'bottom' || getAlignOffset(align, ['right', 'bottom']) !== null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/*
|
|
338
|
+
* The coordinate along one axis (CSS pixels from the area's start edge) of a dot center the
|
|
339
|
+
* grid passes through, for an area `extent` long: the middle for `center`; one `size` before
|
|
340
|
+
* the start edge for `left` / `top`, so that dot is hidden and the next one sits about a
|
|
341
|
+
* spacing in; half a `size` before the end edge for `right` / `bottom`, so that dot sits flush
|
|
342
|
+
* against it; a number (or `{ left }` / `{ top }`) as it is; `{ right }` / `{ bottom }` that
|
|
343
|
+
* many pixels before the end edge. The other dots follow at `spacing` steps either way.
|
|
344
|
+
*/
|
|
345
|
+
export function getDotGridAnchor(align: DotAlign, size: number, extent: number) {
|
|
346
|
+
if (typeof align === 'number') return align;
|
|
347
|
+
const offset = getAlignOffset(align, ['left', 'top', 'right', 'bottom']);
|
|
348
|
+
if (offset) return isEndAlign(align) ? extent - offset.offset : offset.offset;
|
|
349
|
+
if (align === 'left' || align === 'top') return -size;
|
|
350
|
+
if (align === 'right' || align === 'bottom') return extent - size / 2;
|
|
351
|
+
return extent / 2;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/*
|
|
355
|
+
* The dot center nearest to `coordinate` along one axis, on the grid `getDotGridAnchor`
|
|
356
|
+
* anchors for an area `extent` long.
|
|
357
|
+
*/
|
|
358
|
+
export function getNearestDotGridCenter(coordinate: number, align: DotAlign, size: number, spacing: number, extent: number) {
|
|
359
|
+
const anchor = getDotGridAnchor(align, size, extent);
|
|
360
|
+
return anchor + Math.round((coordinate - anchor) / spacing) * spacing;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/*
|
|
364
|
+
* The CSS `mask-position` (or `background-position`) that lays `makeDotGridTileUrl`'s tile,
|
|
365
|
+
* repeated at `spacing × spacing`, on the same dots as `getDotGridAnchor` would for either
|
|
366
|
+
* axis, without knowing the area's size: the tile's dot is centered, so `center` is exact, a
|
|
367
|
+
* start-edge or pixel anchor puts the first tile half a spacing before the anchor, and an
|
|
368
|
+
* end-edge anchor moves the last tile past the end edge by half a spacing less its anchor's
|
|
369
|
+
* distance from that edge (half a dot for the keyword, the given pixels for `{ right }` /
|
|
370
|
+
* `{ bottom }`).
|
|
371
|
+
*/
|
|
372
|
+
export function getDotGridMaskPosition({ size, spacing, alignX, alignY }: Required<DotStyle>) {
|
|
373
|
+
const axis = (align: DotAlign) => {
|
|
374
|
+
if (align === 'center') return 'center';
|
|
375
|
+
// A tile position of 100% puts the last tile's end on the end edge, so its dot center
|
|
376
|
+
// sits half a spacing before it; the anchor (negative, relative to the edge) is the rest.
|
|
377
|
+
if (isEndAlign(align)) return `calc(100% + ${spacing / 2 + getDotGridAnchor(align, size, 0)}px)`;
|
|
378
|
+
return `${getDotGridAnchor(align, size, 0) - spacing / 2}px`;
|
|
379
|
+
};
|
|
380
|
+
return `${axis(alignX)} ${axis(alignY)}`;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/*
|
|
384
|
+
* One tile of the grid as an SVG data URL, for `mask-image` or `background-image`. Repeat
|
|
385
|
+
* it at `spacing × spacing` positioned by `getDotGridMaskPosition` and it lines up exactly
|
|
386
|
+
* with the WebGL grid.
|
|
387
|
+
*/
|
|
388
|
+
export function makeDotGridTileUrl({ size, shape, spacing }: Pick<Required<DotStyle>, 'size' | 'shape' | 'spacing'>) {
|
|
389
|
+
const half = spacing / 2;
|
|
390
|
+
const r = size / 2;
|
|
391
|
+
const stroke = r * (1 - DOT_RING_INNER_RADIUS);
|
|
392
|
+
let dot: string;
|
|
393
|
+
if (shape === 'rounded_square' || shape === 'ring_rectangle') {
|
|
394
|
+
const inset = shape === 'ring_rectangle' ? stroke / 2 : 0;
|
|
395
|
+
const outline = shape === 'ring_rectangle' ? ` fill="none" stroke="black" stroke-width="${stroke}"` : '';
|
|
396
|
+
dot = `<rect x="${half - r + inset}" y="${half - r + inset}" width="${size - inset * 2}" height="${size - inset * 2}" rx="${r * DOT_ROUNDED_SQUARE_CORNER - inset}"${outline}/>`;
|
|
397
|
+
} else if (shape === 'ring') {
|
|
398
|
+
dot = `<circle cx="${half}" cy="${half}" r="${r - stroke / 2}" fill="none" stroke="black" stroke-width="${stroke}"/>`;
|
|
399
|
+
} else {
|
|
400
|
+
dot = `<circle cx="${half}" cy="${half}" r="${r}"/>`;
|
|
401
|
+
}
|
|
402
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${spacing} ${spacing}">${dot}</svg>`;
|
|
403
|
+
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/*
|
|
407
|
+
* A mask that fades an element to transparent over the bottom `fadeBottom` pixels, for a
|
|
408
|
+
* static stand-in that must match the canvas's bottom fade. To fade to a color instead, give
|
|
409
|
+
* the element's parent that color as its background.
|
|
410
|
+
*/
|
|
411
|
+
export function getBottomFadeMaskStyle(fadeBottom: number): CSSProperties | undefined {
|
|
412
|
+
if (fadeBottom <= 0) return undefined;
|
|
413
|
+
return { maskImage: `linear-gradient(to bottom, #000 calc(100% - ${fadeBottom}px), transparent)` };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/*
|
|
417
|
+
* CSS that draws a resting dot grid with no WebGL: the element's background masked by the
|
|
418
|
+
* grid's SVG tile, repeated at the dot spacing and anchored per `alignX` / `alignY` so it lines
|
|
419
|
+
* up with the canvas grid. `fadeBottom` fades the dots over that many pixels along the bottom
|
|
420
|
+
* edge: to transparent through a second mask layer, or to `fadeColor` through a background
|
|
421
|
+
* gradient over the element's own background (which an image placeholder covers, so that one
|
|
422
|
+
* only fades to transparent). Set the color as the element's background separately, since an
|
|
423
|
+
* image may be what shows through the mask.
|
|
424
|
+
*/
|
|
425
|
+
export function getDotGridMaskStyle(dots: Required<DotStyle>, fadeBottom = 0, fadeColor: string | null = null): CSSProperties {
|
|
426
|
+
const position = getDotGridMaskPosition(dots);
|
|
427
|
+
if (fadeBottom > 0 && fadeColor) {
|
|
428
|
+
return {
|
|
429
|
+
opacity: dots.opacity,
|
|
430
|
+
backgroundImage: `linear-gradient(to bottom, transparent calc(100% - ${fadeBottom}px), ${fadeColor})`,
|
|
431
|
+
maskImage: `url("${makeDotGridTileUrl(dots)}")`,
|
|
432
|
+
maskRepeat: 'repeat',
|
|
433
|
+
maskSize: `${dots.spacing}px ${dots.spacing}px`,
|
|
434
|
+
maskPosition: position,
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
const fadeMask = fadeBottom > 0 ? `, linear-gradient(to bottom, #000 calc(100% - ${fadeBottom}px), transparent)` : '';
|
|
438
|
+
return {
|
|
439
|
+
opacity: dots.opacity,
|
|
440
|
+
maskImage: `url("${makeDotGridTileUrl(dots)}")${fadeMask}`,
|
|
441
|
+
maskRepeat: fadeMask ? 'repeat, no-repeat' : 'repeat',
|
|
442
|
+
maskSize: `${dots.spacing}px ${dots.spacing}px${fadeMask ? ', 100% 100%' : ''}`,
|
|
443
|
+
maskPosition: fadeMask ? `${position}, center` : position,
|
|
444
|
+
maskComposite: fadeMask ? 'intersect' : undefined,
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/*
|
|
449
|
+
* The same grid for a layer whose top-left corner rests `offset` pixels into an area
|
|
450
|
+
* `width` × `height` (a `<DotsBoard.Sticky>` inside its board): the area's anchor dot, taken
|
|
451
|
+
* into the layer's pixels, positions the tile so the layer's dots fall exactly on the area's.
|
|
452
|
+
* Numeric, so it needs the area measured; until then it is `getDotGridMaskStyle`, which can be
|
|
453
|
+
* off by up to half a spacing.
|
|
454
|
+
*/
|
|
455
|
+
export function getDotGridLayerMaskStyle(
|
|
456
|
+
dots: Required<DotStyle>,
|
|
457
|
+
offset: { left: number; top: number },
|
|
458
|
+
width: number,
|
|
459
|
+
height: number,
|
|
460
|
+
): CSSProperties {
|
|
461
|
+
if (!width || !height) return getDotGridMaskStyle(dots);
|
|
462
|
+
const { size, spacing, alignX, alignY } = dots;
|
|
463
|
+
// The tile's dot is at its center, so the tile starts half a spacing before any dot center;
|
|
464
|
+
// wrapped into the first tile so the position stays small.
|
|
465
|
+
const tileStart = (anchor: number) => ((((anchor - spacing / 2) % spacing) + spacing) % spacing) - spacing;
|
|
466
|
+
const x = tileStart(getDotGridAnchor(alignX, size, width) - offset.left);
|
|
467
|
+
const y = tileStart(getDotGridAnchor(alignY, size, height) - offset.top);
|
|
468
|
+
return { ...getDotGridMaskStyle(dots), maskPosition: `${x}px ${y}px` };
|
|
469
|
+
}
|
package/utils/easing.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Easing curves for the package's JavaScript-driven animations (reveals, effect fades). Every
|
|
3
|
+
* function maps a 0..1 progress to a 0..1 amount; callers clamp the progress first.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Ease-out cubic: visible motion from the first frame, settling gently at the end.
|
|
8
|
+
*/
|
|
9
|
+
export function easeOutCubic(t: number) {
|
|
10
|
+
return 1 - (1 - t) ** 3;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
* Ease-in-out cubic: a soft start and finish, for fades that begin and end at rest.
|
|
15
|
+
*/
|
|
16
|
+
export function easeInOutCubic(t: number) {
|
|
17
|
+
return t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
|
|
18
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { RippleSimulationOptions } from '../libs/effects/ripple.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Effect names, per-effect options, and spec normalization. This module has no runtime
|
|
5
|
+
* dependency on OGL, so route bundles can import it to build specs before the engine loads.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface ImageEffectOptions {
|
|
9
|
+
/** Cursor leaves expanding water rings that refract and catch light. */
|
|
10
|
+
ripple: RippleSimulationOptions & { strength?: number; shine?: number };
|
|
11
|
+
/** Travelling sine ripple across the whole image. */
|
|
12
|
+
waves: { frequency?: number; amplitude?: number; speed?: number };
|
|
13
|
+
/** Fine, fast, upward-drifting shimmer. */
|
|
14
|
+
heatHaze: { frequency?: number; amplitude?: number; speed?: number };
|
|
15
|
+
/** Slow, broad, jelly-like wobble. */
|
|
16
|
+
liquid: { frequency?: number; amplitude?: number; speed?: number };
|
|
17
|
+
/** Magnifying glass under the cursor. */
|
|
18
|
+
lens: { radius?: number; zoom?: number };
|
|
19
|
+
/** Glass disc under the cursor that refracts at its rim. */
|
|
20
|
+
glass: { radius?: number; strength?: number };
|
|
21
|
+
/** Strong barrel bulge over the whole image. */
|
|
22
|
+
fisheye: { strength?: number };
|
|
23
|
+
/** Mild barrel bulge; negative strength pinches instead. */
|
|
24
|
+
barrel: { strength?: number };
|
|
25
|
+
/** Pincushion pinch (edges pulled inward). */
|
|
26
|
+
pincushion: { strength?: number };
|
|
27
|
+
/** Slight zoom plus a shift opposite to the pointer, faking depth. */
|
|
28
|
+
parallax: { shift?: number };
|
|
29
|
+
/** Twist around the cursor. */
|
|
30
|
+
swirl: { radius?: number; angle?: number };
|
|
31
|
+
/** Mirror the image into wedges around the cursor. */
|
|
32
|
+
kaleidoscope: { segments?: number };
|
|
33
|
+
/** Break the photo itself into drifting Voronoi shards. */
|
|
34
|
+
shatter: {
|
|
35
|
+
scale?: number;
|
|
36
|
+
spread?: number;
|
|
37
|
+
angle?: number;
|
|
38
|
+
skew?: number;
|
|
39
|
+
speed?: number;
|
|
40
|
+
};
|
|
41
|
+
/** Mosaic that grows from nothing to `size` (fraction of height). */
|
|
42
|
+
pixelate: { size?: number };
|
|
43
|
+
/** Split color channels radially from the cursor. */
|
|
44
|
+
chromatic: { strength?: number };
|
|
45
|
+
/** Radial blur streaking toward the cursor. */
|
|
46
|
+
zoomBlur: { strength?: number };
|
|
47
|
+
/** Depth of field: sharp within `focus` of the cursor, disc-blurred (with highlight bloom) beyond it. */
|
|
48
|
+
bokeh: {
|
|
49
|
+
radius?: number;
|
|
50
|
+
focus?: number;
|
|
51
|
+
softness?: number;
|
|
52
|
+
highlight?: number;
|
|
53
|
+
samples?: number;
|
|
54
|
+
scale?: number;
|
|
55
|
+
};
|
|
56
|
+
/** Sobel outlines in `color`, blended over the photo. */
|
|
57
|
+
edges: { color?: string; strength?: number };
|
|
58
|
+
grayscale: Record<string, never>;
|
|
59
|
+
sepia: Record<string, never>;
|
|
60
|
+
invert: Record<string, never>;
|
|
61
|
+
/** Two-color gradient map. */
|
|
62
|
+
duotone: { shadow?: string; highlight?: string };
|
|
63
|
+
posterize: { levels?: number };
|
|
64
|
+
/** Radians of hue rotation at full amount. */
|
|
65
|
+
hueRotate: { angle?: number };
|
|
66
|
+
/** Full color inside a disc around the cursor, grayscale outside. */
|
|
67
|
+
colorReveal: { radius?: number; softness?: number };
|
|
68
|
+
/** Print-style dot screen; `size` is in drawing-buffer pixels. */
|
|
69
|
+
halftone: { size?: number; angle?: number; ink?: string; paper?: string };
|
|
70
|
+
scanlines: { period?: number; strength?: number };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type ImageEffectName = keyof ImageEffectOptions;
|
|
74
|
+
|
|
75
|
+
/** One effect to apply: `{ name: 'lens', zoom: 2 }`. */
|
|
76
|
+
export type ImageEffectSpec = {
|
|
77
|
+
[K in ImageEffectName]: { name: K } & ImageEffectOptions[K];
|
|
78
|
+
}[ImageEffectName];
|
|
79
|
+
|
|
80
|
+
/*
|
|
81
|
+
* Accept a single name, a single spec, or a list of either, and return normalized specs.
|
|
82
|
+
*/
|
|
83
|
+
export function normalizeImageEffectSpecs(
|
|
84
|
+
input: ImageEffectName | ImageEffectSpec | Array<ImageEffectName | ImageEffectSpec> | null | undefined,
|
|
85
|
+
): ImageEffectSpec[] {
|
|
86
|
+
if (!input) return [];
|
|
87
|
+
const list = Array.isArray(input) ? input : [input];
|
|
88
|
+
return list.map((item) => (typeof item === 'string' ? ({ name: item } as ImageEffectSpec) : item));
|
|
89
|
+
}
|
package/utils/motion.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Motion helpers for the package's JavaScript-driven animations: the visitor's reduced-motion
|
|
3
|
+
* preference, and a damped spring integrator for any 2D body. No OGL, safe for route bundles.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Types
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface Spring {
|
|
11
|
+
/** Pull toward the rest position, per unit of distance. */
|
|
12
|
+
stiffness: number;
|
|
13
|
+
/** Resistance to velocity; below about `2 × √stiffness` the motion overshoots and bounces. */
|
|
14
|
+
damping: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A point with velocity that a spring drives. */
|
|
18
|
+
export interface SpringBody {
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
vx: number;
|
|
22
|
+
vy: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Constants
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export const SPRING_PHYSICS = {
|
|
30
|
+
/** Longest frame the springs integrate; a background tab resuming does not explode. */
|
|
31
|
+
maxDt: 1 / 30,
|
|
32
|
+
/** Springs integrate in sub-steps no longer than this, which keeps stiff settings stable. */
|
|
33
|
+
subStep: 1 / 120,
|
|
34
|
+
/** A body within this many units of its target, moving slower than `settleVelocity`, snaps to rest. */
|
|
35
|
+
settleDistance: 0.05,
|
|
36
|
+
settleVelocity: 1,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Helpers
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
* Whether the visitor asked for reduced motion; animations then swap or snap instead of moving.
|
|
45
|
+
*/
|
|
46
|
+
export function prefersReducedMotion() {
|
|
47
|
+
return typeof window !== 'undefined' && !!window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/*
|
|
51
|
+
* A body at rest on a point.
|
|
52
|
+
*/
|
|
53
|
+
export function springBodyAt(x: number, y: number): SpringBody {
|
|
54
|
+
return { x, y, vx: 0, vy: 0 };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/*
|
|
58
|
+
* Put a body at rest on its target, for reduced motion or a first placement.
|
|
59
|
+
*/
|
|
60
|
+
export function snapSpring(body: SpringBody, targetX: number, targetY: number) {
|
|
61
|
+
body.x = targetX;
|
|
62
|
+
body.y = targetY;
|
|
63
|
+
body.vx = 0;
|
|
64
|
+
body.vy = 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
* Advance a damped spring toward its target by `dt` seconds (semi-implicit Euler in sub-steps),
|
|
69
|
+
* mutating the body. Returns true once it has come to rest, having snapped it exactly onto the
|
|
70
|
+
* target so no residual keeps a frame loop alive.
|
|
71
|
+
*/
|
|
72
|
+
export function stepSpring(body: SpringBody, targetX: number, targetY: number, dt: number, spring: Spring) {
|
|
73
|
+
const steps = Math.max(1, Math.ceil(dt / SPRING_PHYSICS.subStep));
|
|
74
|
+
const h = dt / steps;
|
|
75
|
+
for (let i = 0; i < steps; i++) {
|
|
76
|
+
body.vx += (-spring.stiffness * (body.x - targetX) - spring.damping * body.vx) * h;
|
|
77
|
+
body.vy += (-spring.stiffness * (body.y - targetY) - spring.damping * body.vy) * h;
|
|
78
|
+
body.x += body.vx * h;
|
|
79
|
+
body.y += body.vy * h;
|
|
80
|
+
}
|
|
81
|
+
const settled =
|
|
82
|
+
Math.abs(body.x - targetX) < SPRING_PHYSICS.settleDistance &&
|
|
83
|
+
Math.abs(body.y - targetY) < SPRING_PHYSICS.settleDistance &&
|
|
84
|
+
Math.abs(body.vx) < SPRING_PHYSICS.settleVelocity &&
|
|
85
|
+
Math.abs(body.vy) < SPRING_PHYSICS.settleVelocity;
|
|
86
|
+
if (settled) snapSpring(body, targetX, targetY);
|
|
87
|
+
return settled;
|
|
88
|
+
}
|