@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
package/scenes/Hero.ts
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { PassChain } from '../libs/chain.ts';
|
|
2
|
+
import { hexToRgb, mixRgb } from '../utils/color.ts';
|
|
3
|
+
import type { EffectRegion, EffectRegionInstance } from '../libs/effects/effectRegion.ts';
|
|
4
|
+
import type { ImageEffectImage, ImageEffectInstance, ImageEffectSpec } from '../libs/effects/imageEffects.ts';
|
|
5
|
+
import {
|
|
6
|
+
createBokehPass,
|
|
7
|
+
createImageLightCompositePass,
|
|
8
|
+
createSineWarpPass,
|
|
9
|
+
createSpotlightPass,
|
|
10
|
+
createVoronoiShatterPass,
|
|
11
|
+
} from '../libs/passes.ts';
|
|
12
|
+
import { createImageTexture } from '../libs/texture.ts';
|
|
13
|
+
import type { ImageTexture } from '../libs/texture.ts';
|
|
14
|
+
import type { SceneContext, Scene, SceneSize, RGB } from '../libs/types.ts';
|
|
15
|
+
import { setVec2, setVec3 } from '../libs/uniforms.ts';
|
|
16
|
+
import type { Program } from 'ogl';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Types
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export interface HeroSceneParams {
|
|
23
|
+
imageUrl: string;
|
|
24
|
+
/** Hex brand color that tints the photo and seeds the light and shade colors. */
|
|
25
|
+
baseColor: string;
|
|
26
|
+
/** 0..1 strength of the light field over the photo. */
|
|
27
|
+
intensity: number;
|
|
28
|
+
/** Image effects applied to the photo while the pointer is over the hero. */
|
|
29
|
+
hoverEffects: ImageEffectSpec[];
|
|
30
|
+
/** Image effects that are always on, limited to `imageEffectRegion` when one is given. */
|
|
31
|
+
imageEffects: ImageEffectSpec[];
|
|
32
|
+
imageEffectRegion: EffectRegion | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type HeroEffectParams = Pick<HeroSceneParams, 'hoverEffects' | 'imageEffects' | 'imageEffectRegion'>;
|
|
36
|
+
|
|
37
|
+
/** The effect code is a separate chunk, loaded only when a hero actually asks for effects. */
|
|
38
|
+
type HeroEffectsModule = typeof import('../libs/effects/index.ts');
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Constants
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
// Shape parameters follow the microsoft.ai hero: a wide skewed spotlight, a slow horizontal
|
|
45
|
+
// ripple, tall drifting shards, and a soft disc blur, all rendered at half resolution.
|
|
46
|
+
const HERO_EFFECT = {
|
|
47
|
+
chainScale: 0.5,
|
|
48
|
+
chainMaxWidth: 1024,
|
|
49
|
+
spotlight: { radius: 0.354, falloff: 1, skew: 0.54, angle: 0 },
|
|
50
|
+
sine: { frequency: 0.35, amplitude: 1.18, speed: 0.52, axis: 0 },
|
|
51
|
+
shatter: {
|
|
52
|
+
scale: 0.534,
|
|
53
|
+
spread: 1,
|
|
54
|
+
angle: -44 / 360,
|
|
55
|
+
skew: 0.84,
|
|
56
|
+
speed: 0.4,
|
|
57
|
+
},
|
|
58
|
+
bokeh: { radius: 0.003, highlight: 150, samples: 50 },
|
|
59
|
+
tint: 0.3,
|
|
60
|
+
warmWhite: [1, 0.96, 0.86] as RGB,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/*
|
|
64
|
+
* Derive the three effect colors from one brand color: the color itself, a deep cool shade for
|
|
65
|
+
* the unlit area, and a warm highlight for the lit area.
|
|
66
|
+
*/
|
|
67
|
+
function getHeroColors(baseColor: string) {
|
|
68
|
+
const base = hexToRgb(baseColor, [1, 0.667, 0.647]);
|
|
69
|
+
return {
|
|
70
|
+
base,
|
|
71
|
+
shade: [base[0] * 0.3, base[1] * 0.05, base[2] * 0.35] as RGB,
|
|
72
|
+
light: mixRgb(base, HERO_EFFECT.warmWhite, 0.6),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/*
|
|
77
|
+
* Whether the params ask for anything that needs the effects module.
|
|
78
|
+
*/
|
|
79
|
+
function needsEffects(params: HeroEffectParams) {
|
|
80
|
+
return params.hoverEffects.length > 0 || params.imageEffects.length > 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/*
|
|
84
|
+
* Build the hero scene: a spotlight → sine → shatter → bokeh light field at half resolution,
|
|
85
|
+
* composited over the cover-fit photo at full resolution, with optional photo effects.
|
|
86
|
+
*/
|
|
87
|
+
export default function createHeroScene(
|
|
88
|
+
{ gl, signal, invalidate, onDispose }: SceneContext,
|
|
89
|
+
params: HeroSceneParams,
|
|
90
|
+
): Scene<HeroSceneParams> {
|
|
91
|
+
const chain = new PassChain(gl);
|
|
92
|
+
onDispose(() => chain.dispose());
|
|
93
|
+
|
|
94
|
+
let colors = getHeroColors(params.baseColor);
|
|
95
|
+
const spotlight = createSpotlightPass(gl, {
|
|
96
|
+
...HERO_EFFECT.spotlight,
|
|
97
|
+
shadeColor: colors.shade,
|
|
98
|
+
clearColor: colors.light,
|
|
99
|
+
});
|
|
100
|
+
const sine = createSineWarpPass(gl, HERO_EFFECT.sine);
|
|
101
|
+
const shatter = createVoronoiShatterPass(gl, HERO_EFFECT.shatter);
|
|
102
|
+
const bokeh = createBokehPass(gl, HERO_EFFECT.bokeh);
|
|
103
|
+
const lightPasses = [spotlight, sine, shatter, bokeh];
|
|
104
|
+
for (const program of lightPasses) onDispose(() => program.remove());
|
|
105
|
+
|
|
106
|
+
// One object, mutated in place, is handed to effects every frame instead of a fresh allocation.
|
|
107
|
+
const presented: ImageEffectImage = {
|
|
108
|
+
texture: null as never,
|
|
109
|
+
size: [1, 1],
|
|
110
|
+
loaded: false,
|
|
111
|
+
};
|
|
112
|
+
let image: ImageTexture | null = null;
|
|
113
|
+
let imageUrl = '';
|
|
114
|
+
let intensity = params.intensity;
|
|
115
|
+
let size: SceneSize | null = null;
|
|
116
|
+
let composite: Program | null = null;
|
|
117
|
+
let effects: ImageEffectInstance[] = [];
|
|
118
|
+
let effectsKey = '';
|
|
119
|
+
let region: EffectRegionInstance | null = null;
|
|
120
|
+
let regionKey = '';
|
|
121
|
+
let effectsModule: HeroEffectsModule | null = null;
|
|
122
|
+
let effectsLoading = false;
|
|
123
|
+
let wanted: HeroEffectParams = params;
|
|
124
|
+
onDispose(() => {
|
|
125
|
+
image?.dispose();
|
|
126
|
+
composite?.remove();
|
|
127
|
+
effectsModule?.disposeImageEffects(effects);
|
|
128
|
+
region?.dispose();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
/*
|
|
132
|
+
* Push the current image state into the composite; called after the image loads or the
|
|
133
|
+
* composite is rebuilt, since a rebuilt program starts from blank uniforms.
|
|
134
|
+
*/
|
|
135
|
+
function syncImageUniforms() {
|
|
136
|
+
if (!composite || !image) return;
|
|
137
|
+
composite.uniforms.tImage.value = image.texture;
|
|
138
|
+
composite.uniforms.uImageSize.value = image.size;
|
|
139
|
+
composite.uniforms.uImageLoaded.value = image.loaded ? 1 : 0;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/*
|
|
143
|
+
* Fetch the effects chunk once, then build what the latest params asked for.
|
|
144
|
+
*/
|
|
145
|
+
function loadEffects() {
|
|
146
|
+
if (effectsModule || effectsLoading) return;
|
|
147
|
+
effectsLoading = true;
|
|
148
|
+
import('../libs/effects/index.ts').then(
|
|
149
|
+
(module) => {
|
|
150
|
+
if (signal.aborted) return;
|
|
151
|
+
effectsModule = module;
|
|
152
|
+
region = module.createEffectRegion(gl, { signal, onLoad: invalidate });
|
|
153
|
+
if (size) region.resize(size);
|
|
154
|
+
applyEffects(wanted);
|
|
155
|
+
invalidate();
|
|
156
|
+
},
|
|
157
|
+
() => {
|
|
158
|
+
// Without the chunk the hero simply stays effect-free.
|
|
159
|
+
effectsLoading = false;
|
|
160
|
+
},
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/*
|
|
165
|
+
* (Re)compile the composite for the requested effect lists. Runs effect-free immediately and
|
|
166
|
+
* again with effects once their chunk is in; effects own GPU state, so the previous set is
|
|
167
|
+
* disposed with the previous program.
|
|
168
|
+
*/
|
|
169
|
+
function buildComposite(next: HeroEffectParams) {
|
|
170
|
+
const module = effectsModule;
|
|
171
|
+
const nextKey = module ? JSON.stringify([next.hoverEffects, next.imageEffects]) : '';
|
|
172
|
+
if (composite && nextKey === effectsKey) return;
|
|
173
|
+
composite?.remove();
|
|
174
|
+
module?.disposeImageEffects(effects);
|
|
175
|
+
effectsKey = nextKey;
|
|
176
|
+
const hoverEffects = module ? module.createImageEffects(gl, next.hoverEffects, 'uFx') : [];
|
|
177
|
+
const regionEffects = module ? module.createImageEffects(gl, next.imageEffects, 'uFxR') : [];
|
|
178
|
+
effects = [...regionEffects, ...hoverEffects];
|
|
179
|
+
composite = createImageLightCompositePass(gl, {
|
|
180
|
+
baseColor: colors.base,
|
|
181
|
+
lightColor: colors.light,
|
|
182
|
+
tint: HERO_EFFECT.tint,
|
|
183
|
+
intensity,
|
|
184
|
+
hoverEffects,
|
|
185
|
+
regionEffects,
|
|
186
|
+
region: region ?? undefined,
|
|
187
|
+
});
|
|
188
|
+
syncImageUniforms();
|
|
189
|
+
if (size) for (const effect of effects) effect.resize?.(size);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/*
|
|
193
|
+
* Point the region at a new spec only when it actually changed (uniforms and masks only).
|
|
194
|
+
*/
|
|
195
|
+
function applyRegion(next: EffectRegion | null) {
|
|
196
|
+
const nextKey = JSON.stringify(next);
|
|
197
|
+
if (!region || nextKey === regionKey) return;
|
|
198
|
+
regionKey = nextKey;
|
|
199
|
+
region.setSpec(next);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/*
|
|
203
|
+
* Apply effect params: compile what is possible now and fetch the effects chunk if needed.
|
|
204
|
+
*/
|
|
205
|
+
function applyEffects(next: HeroEffectParams) {
|
|
206
|
+
wanted = next;
|
|
207
|
+
if (needsEffects(next) && !effectsModule) loadEffects();
|
|
208
|
+
buildComposite(next);
|
|
209
|
+
applyRegion(next.imageEffectRegion);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/*
|
|
213
|
+
* Swap in a new photo; the composite keeps showing the brand color until it decodes.
|
|
214
|
+
*/
|
|
215
|
+
function loadImage(url: string) {
|
|
216
|
+
if (url === imageUrl) return;
|
|
217
|
+
imageUrl = url;
|
|
218
|
+
image?.dispose();
|
|
219
|
+
image = createImageTexture(gl, {
|
|
220
|
+
url,
|
|
221
|
+
signal,
|
|
222
|
+
onLoad: (loaded) => {
|
|
223
|
+
if (loaded !== image) return;
|
|
224
|
+
syncImageUniforms();
|
|
225
|
+
invalidate();
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
syncImageUniforms();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
applyEffects(params);
|
|
232
|
+
loadImage(params.imageUrl);
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
frameloop: 'always',
|
|
236
|
+
isReady: () => !!image?.loaded,
|
|
237
|
+
resize(nextSize) {
|
|
238
|
+
size = nextSize;
|
|
239
|
+
// The light field is all soft gradients, so it can be far smaller than the canvas.
|
|
240
|
+
const scale = Math.min(HERO_EFFECT.chainScale, HERO_EFFECT.chainMaxWidth / Math.max(1, nextSize.width));
|
|
241
|
+
chain.setSize(nextSize.width * scale, nextSize.height * scale);
|
|
242
|
+
for (const effect of effects) effect.resize?.(nextSize);
|
|
243
|
+
region?.resize(nextSize);
|
|
244
|
+
},
|
|
245
|
+
update(frame) {
|
|
246
|
+
const { elapsed, pointer } = frame;
|
|
247
|
+
setVec2(spotlight.uniforms.uPosition, pointer.x, pointer.y);
|
|
248
|
+
sine.uniforms.uTime.value = elapsed;
|
|
249
|
+
shatter.uniforms.uTime.value = elapsed;
|
|
250
|
+
if (!composite) return;
|
|
251
|
+
setVec2(composite.uniforms.uPointer, pointer.x, pointer.y);
|
|
252
|
+
composite.uniforms.uEffectAmount.value = pointer.hoverAmount;
|
|
253
|
+
composite.uniforms.uTime.value = elapsed;
|
|
254
|
+
if (effects.length && image) {
|
|
255
|
+
presented.texture = image.texture;
|
|
256
|
+
presented.size = image.size;
|
|
257
|
+
presented.loaded = image.loaded;
|
|
258
|
+
for (const effect of effects) effect.update?.(frame, presented);
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
render() {
|
|
262
|
+
if (!composite) return;
|
|
263
|
+
chain.render(lightPasses);
|
|
264
|
+
chain.renderToScreen(composite);
|
|
265
|
+
},
|
|
266
|
+
setParams(next) {
|
|
267
|
+
colors = getHeroColors(next.baseColor);
|
|
268
|
+
intensity = next.intensity;
|
|
269
|
+
setVec3(spotlight.uniforms.uShadeColor, colors.shade);
|
|
270
|
+
setVec3(spotlight.uniforms.uClearColor, colors.light);
|
|
271
|
+
applyEffects(next);
|
|
272
|
+
if (composite) {
|
|
273
|
+
setVec3(composite.uniforms.uBaseColor, colors.base);
|
|
274
|
+
setVec3(composite.uniforms.uLightColor, colors.light);
|
|
275
|
+
composite.uniforms.uIntensity.value = intensity;
|
|
276
|
+
}
|
|
277
|
+
loadImage(next.imageUrl);
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"allowImportingTsExtensions": true,
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"noEmit": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["main.ts", "libs/**/*", "utils/**/*", "scenes/**/*", "modules/**/*", "elements/**/*"]
|
|
13
|
+
}
|
package/utils/align.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Edge keywords for anchoring things along an axis, shared by the dot grid, `<Stack>` and
|
|
3
|
+
* `<DotsBoard.Item>`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const ALIGN_X = ['left', 'center', 'right'] as const;
|
|
7
|
+
export const ALIGN_Y = ['top', 'center', 'bottom'] as const;
|
|
8
|
+
export type AlignX = (typeof ALIGN_X)[number];
|
|
9
|
+
export type AlignY = (typeof ALIGN_Y)[number];
|
|
10
|
+
|
|
11
|
+
/** CSS box alignment (`justify-content` / `align-items`) for each edge keyword. */
|
|
12
|
+
export const ALIGN_TO_FLEX: Record<AlignX | AlignY, string> = {
|
|
13
|
+
left: 'flex-start',
|
|
14
|
+
top: 'flex-start',
|
|
15
|
+
center: 'center',
|
|
16
|
+
right: 'flex-end',
|
|
17
|
+
bottom: 'flex-end',
|
|
18
|
+
};
|
package/utils/color.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { RGB } from '../libs/types.ts';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Parse #rgb / #rrggbb into a 0..1 RGB triple; unparseable input falls back to the given color.
|
|
5
|
+
*/
|
|
6
|
+
export function hexToRgb(hex: string, fallback: RGB = [0, 0, 0]): RGB {
|
|
7
|
+
const digits = hex.trim().replace(/^#/, '');
|
|
8
|
+
const expanded = digits.length === 3 ? digits.replace(/./g, (d) => d + d) : digits;
|
|
9
|
+
if (!/^[0-9a-f]{6}$/i.test(expanded)) return fallback;
|
|
10
|
+
const value = Number.parseInt(expanded, 16);
|
|
11
|
+
return [((value >> 16) & 255) / 255, ((value >> 8) & 255) / 255, (value & 255) / 255];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* Linearly interpolate two colors.
|
|
16
|
+
*/
|
|
17
|
+
export function mixRgb(a: RGB, b: RGB, t: number): RGB {
|
|
18
|
+
return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/*
|
|
22
|
+
* Scale a color's brightness while keeping its hue.
|
|
23
|
+
*/
|
|
24
|
+
export function scaleRgb(color: RGB, factor: number): RGB {
|
|
25
|
+
return [color[0] * factor, color[1] * factor, color[2] * factor];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
* Parse a computed CSS color (`rgb(r, g, b)`, `rgba(...)`, `color(srgb r g b)`, or hex) into a
|
|
30
|
+
* 0..1 RGB triple; unparseable input falls back to the given color.
|
|
31
|
+
*/
|
|
32
|
+
export function cssColorToRgb(value: string, fallback: RGB = [0, 0, 0]): RGB {
|
|
33
|
+
const trimmed = value.trim();
|
|
34
|
+
if (trimmed.startsWith('#')) return hexToRgb(trimmed, fallback);
|
|
35
|
+
const match = trimmed.match(/^(rgba?|color)\((.*)\)$/i);
|
|
36
|
+
if (!match) return fallback;
|
|
37
|
+
const parts = match[2]
|
|
38
|
+
.replace(/^srgb\s+/i, '')
|
|
39
|
+
.split(/[\s,/]+/)
|
|
40
|
+
.filter(Boolean);
|
|
41
|
+
if (parts.length < 3) return fallback;
|
|
42
|
+
// rgb() channels are 0..255 (or percentages); color(srgb) channels are already 0..1.
|
|
43
|
+
const scale = match[1].toLowerCase() === 'color' ? 1 : 255;
|
|
44
|
+
const channels = parts
|
|
45
|
+
.slice(0, 3)
|
|
46
|
+
.map((part) => (part.endsWith('%') ? Number.parseFloat(part) / 100 : Number.parseFloat(part) / scale));
|
|
47
|
+
if (channels.some((channel) => Number.isNaN(channel))) return fallback;
|
|
48
|
+
return channels as RGB;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* Convert a 0..1 RGB triple to #rrggbb.
|
|
53
|
+
*/
|
|
54
|
+
export function rgbToHex(color: RGB): string {
|
|
55
|
+
const channel = (value: number) =>
|
|
56
|
+
Math.round(Math.min(1, Math.max(0, value)) * 255)
|
|
57
|
+
.toString(16)
|
|
58
|
+
.padStart(2, '0');
|
|
59
|
+
return `#${channel(color[0])}${channel(color[1])}${channel(color[2])}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/*
|
|
63
|
+
* Convert a 0..1 RGB triple to HSL (hue in degrees, saturation and lightness 0..1).
|
|
64
|
+
*/
|
|
65
|
+
export function rgbToHsl(color: RGB): [number, number, number] {
|
|
66
|
+
const [r, g, b] = color;
|
|
67
|
+
const max = Math.max(r, g, b);
|
|
68
|
+
const min = Math.min(r, g, b);
|
|
69
|
+
const l = (max + min) / 2;
|
|
70
|
+
if (max === min) return [0, 0, l];
|
|
71
|
+
const d = max - min;
|
|
72
|
+
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
73
|
+
let h: number;
|
|
74
|
+
if (max === r) h = (g - b) / d + (g < b ? 6 : 0);
|
|
75
|
+
else if (max === g) h = (b - r) / d + 2;
|
|
76
|
+
else h = (r - g) / d + 4;
|
|
77
|
+
return [h * 60, s, l];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/*
|
|
81
|
+
* Convert HSL (hue in degrees, saturation and lightness 0..1) to a 0..1 RGB triple.
|
|
82
|
+
*/
|
|
83
|
+
export function hslToRgb(h: number, s: number, l: number): RGB {
|
|
84
|
+
const k = (n: number) => (n + h / 30) % 12;
|
|
85
|
+
const a = s * Math.min(l, 1 - l);
|
|
86
|
+
const f = (n: number) => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
|
|
87
|
+
return [f(0), f(8), f(4)];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/*
|
|
91
|
+
* Shift a hex color's lightness by `delta` (-1..1) and optionally its hue by `hueDelta` degrees,
|
|
92
|
+
* returning #rrggbb. Used to derive gradient stops and ink tones from one base color.
|
|
93
|
+
*/
|
|
94
|
+
export function shiftHex(hex: string, delta: number, hueDelta = 0): string {
|
|
95
|
+
const [h, s, l] = rgbToHsl(hexToRgb(hex));
|
|
96
|
+
return rgbToHex(hslToRgb((h + hueDelta + 360) % 360, s, Math.min(1, Math.max(0, l + delta))));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/*
|
|
100
|
+
* Preserve alpha when converting a browser-computed rgb/rgba or color(srgb) value for WebGL.
|
|
101
|
+
* Resolve named colors, hex and theme variables through getComputedStyle before calling.
|
|
102
|
+
*/
|
|
103
|
+
export function cssColorToRgba(value: string): [number, number, number, number] {
|
|
104
|
+
const parts = value
|
|
105
|
+
.slice(value.indexOf('(') + 1, value.lastIndexOf(')'))
|
|
106
|
+
.replace(/^srgb\s+/i, '')
|
|
107
|
+
.split(/[\s,/]+/)
|
|
108
|
+
.filter(Boolean);
|
|
109
|
+
const alpha = parts[3];
|
|
110
|
+
const opacity = alpha === undefined ? 1 : Number.parseFloat(alpha) / (alpha.endsWith('%') ? 100 : 1);
|
|
111
|
+
return [...cssColorToRgb(value), Number.isFinite(opacity) ? Math.min(1, Math.max(0, opacity)) : 1];
|
|
112
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** Shared shapes and effect options for click, pulse, and resting dot animations; no OGL. */
|
|
2
|
+
export const DOT_SHAPES = ['circle', 'rounded_square', 'ring', 'ring_rectangle'] as const;
|
|
3
|
+
export type DotShape = (typeof DOT_SHAPES)[number];
|
|
4
|
+
|
|
5
|
+
/** Shared ordering for effect selection and matching shader IDs. */
|
|
6
|
+
export const DOT_EFFECTS = ['swell', 'ripple', 'vortex', 'magnet', 'color_wave', 'sparkle'] as const;
|
|
7
|
+
export type DotEffect = (typeof DOT_EFFECTS)[number];
|
|
8
|
+
export type DotEffectSelection = DotEffect | 'random';
|
|
9
|
+
|
|
10
|
+
/** Appearance and motion shared by every dot animation trigger. */
|
|
11
|
+
export interface DotEffectOptions {
|
|
12
|
+
/** Animation to run. Random picks once per click, pulse, or resting cycle. */
|
|
13
|
+
effect?: DotEffectSelection;
|
|
14
|
+
/** Shape at the animation's peak; defaults to the resting dot's shape. */
|
|
15
|
+
shape?: DotShape;
|
|
16
|
+
/** Maximum reach in CSS pixels from the effect's origin. */
|
|
17
|
+
radius?: number;
|
|
18
|
+
/** Half-width in CSS pixels of a ripple or color wave. */
|
|
19
|
+
width?: number;
|
|
20
|
+
/** Milliseconds for the effect to finish and return to normal. */
|
|
21
|
+
duration?: number;
|
|
22
|
+
/** Movement strength, 0..1, for vortex and magnet. */
|
|
23
|
+
strength?: number;
|
|
24
|
+
/** Dot size multiplier at the animation's peak. */
|
|
25
|
+
scale?: number;
|
|
26
|
+
/** 0..1 opacity at the peak; defaults to the resting dot's opacity. */
|
|
27
|
+
opacity?: number;
|
|
28
|
+
/** Optional hex tint, including over images. Color wave and sparkle use a seeded palette when omitted. */
|
|
29
|
+
color?: string | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type ResolvedDotEffectOptions = Required<DotEffectOptions>;
|
|
33
|
+
|
|
34
|
+
export const DOT_EFFECT_DEFAULTS: ResolvedDotEffectOptions = {
|
|
35
|
+
effect: 'ripple',
|
|
36
|
+
shape: 'circle',
|
|
37
|
+
radius: 420,
|
|
38
|
+
width: 80,
|
|
39
|
+
duration: 1400,
|
|
40
|
+
strength: 0.35,
|
|
41
|
+
scale: 4,
|
|
42
|
+
opacity: 1,
|
|
43
|
+
color: null,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/*
|
|
47
|
+
* Clamp a finite option; malformed values fall back instead of putting NaNs in GPU uniforms.
|
|
48
|
+
*/
|
|
49
|
+
function finiteOption(value: number | undefined, fallback: number, min: number, max = Number.POSITIVE_INFINITY) {
|
|
50
|
+
return Math.min(max, Math.max(min, typeof value === 'number' && Number.isFinite(value) ? value : fallback));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/*
|
|
54
|
+
* Normalize effect settings independently of their trigger, with trigger-specific defaults.
|
|
55
|
+
*/
|
|
56
|
+
export function resolveDotEffectOptions(
|
|
57
|
+
options: DotEffectOptions,
|
|
58
|
+
defaults: Partial<ResolvedDotEffectOptions> = {},
|
|
59
|
+
): ResolvedDotEffectOptions {
|
|
60
|
+
const fallback = { ...DOT_EFFECT_DEFAULTS, ...defaults };
|
|
61
|
+
return {
|
|
62
|
+
effect: options.effect === 'random' || DOT_EFFECTS.includes(options.effect as DotEffect) ? options.effect! : fallback.effect,
|
|
63
|
+
shape: DOT_SHAPES.includes(options.shape as DotShape) ? options.shape! : fallback.shape,
|
|
64
|
+
radius: finiteOption(options.radius, fallback.radius, 1),
|
|
65
|
+
width: finiteOption(options.width, fallback.width, 1),
|
|
66
|
+
duration: finiteOption(options.duration, fallback.duration, 1),
|
|
67
|
+
strength: finiteOption(options.strength, fallback.strength, 0, 1),
|
|
68
|
+
scale: finiteOption(options.scale, fallback.scale, 0),
|
|
69
|
+
opacity: finiteOption(options.opacity, fallback.opacity, 0, 1),
|
|
70
|
+
color: options.color ?? fallback.color,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Convert an effect name to its shader ID; -1 asks the shader to choose from a stable seed.
|
|
76
|
+
*/
|
|
77
|
+
export function getDotEffectId(effect: DotEffectSelection) {
|
|
78
|
+
return effect === 'random' ? -1 : DOT_EFFECTS.indexOf(effect);
|
|
79
|
+
}
|