@arraypress/effects-astro 0.0.0-stage → 0.1.1
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/AGENTS.md +85 -0
- package/COMPONENTS.md +1264 -0
- package/LICENSE +139 -0
- package/README.md +197 -2
- package/package.json +60 -4
- package/src/components/AsciiRain.astro +36 -0
- package/src/components/BorderSweep.astro +39 -0
- package/src/components/Caustics.astro +39 -0
- package/src/components/ColorPanels.astro +62 -0
- package/src/components/Cursor.astro +97 -0
- package/src/components/DotGrid.astro +34 -0
- package/src/components/DotSurface.astro +35 -0
- package/src/components/EffectCanvas.astro +152 -0
- package/src/components/FitText.astro +35 -0
- package/src/components/FlutedGlass.astro +36 -0
- package/src/components/Glass.astro +69 -0
- package/src/components/GlassGlyph.astro +38 -0
- package/src/components/GlowField.astro +177 -0
- package/src/components/Grain.astro +98 -0
- package/src/components/HaloRing.astro +38 -0
- package/src/components/HighlightSweep.astro +74 -0
- package/src/components/ImageRipple.astro +56 -0
- package/src/components/Ink.astro +37 -0
- package/src/components/InkField.astro +45 -0
- package/src/components/IsoCity.astro +54 -0
- package/src/components/LightField.astro +40 -0
- package/src/components/LiquidMetal.astro +41 -0
- package/src/components/Magnetic.astro +52 -0
- package/src/components/Marquee.astro +216 -0
- package/src/components/MosaicReveal.astro +196 -0
- package/src/components/NebulaLogo.astro +38 -0
- package/src/components/NeonSwarm.astro +38 -0
- package/src/components/Ocean.astro +40 -0
- package/src/components/OutlineWordmark.astro +222 -0
- package/src/components/PaperBackground.astro +40 -0
- package/src/components/PaperShader.astro +55 -0
- package/src/components/Parallax.astro +49 -0
- package/src/components/ParticleLogo.astro +37 -0
- package/src/components/PixelGlitchText.astro +70 -0
- package/src/components/PointCloud.astro +37 -0
- package/src/components/ProductStack.astro +298 -0
- package/src/components/PulseMark.astro +189 -0
- package/src/components/RadiantGlass.astro +38 -0
- package/src/components/RainOnGlass.astro +71 -0
- package/src/components/Reveal.astro +115 -0
- package/src/components/ScrambleText.astro +52 -0
- package/src/components/ScrollReveal.astro +38 -0
- package/src/components/ShaderBackground.astro +55 -0
- package/src/components/SignalLines.astro +41 -0
- package/src/components/SilkRibbon.astro +37 -0
- package/src/components/SpotlightText.astro +131 -0
- package/src/components/Tilt.astro +79 -0
- package/src/components/Topography.astro +38 -0
- package/src/components/Typewriter.astro +66 -0
- package/src/components/VariableType.astro +59 -0
- package/src/components/VoxelLogo.astro +46 -0
- package/src/components/WaveBars.astro +41 -0
- package/src/components/WorldRoutes.astro +41 -0
- package/src/components/style.js +19 -0
- package/src/index.d.ts +143 -0
- package/src/index.js +105 -0
- package/src/runtime/ascii-rain.d.ts +53 -0
- package/src/runtime/ascii-rain.js +355 -0
- package/src/runtime/audio-meter.d.ts +65 -0
- package/src/runtime/audio-meter.js +282 -0
- package/src/runtime/audio.d.ts +26 -0
- package/src/runtime/audio.js +453 -0
- package/src/runtime/blur.d.ts +2 -0
- package/src/runtime/blur.js +56 -0
- package/src/runtime/canvas2d.d.ts +23 -0
- package/src/runtime/canvas2d.js +105 -0
- package/src/runtime/caustics.d.ts +40 -0
- package/src/runtime/caustics.js +286 -0
- package/src/runtime/color.d.ts +33 -0
- package/src/runtime/color.js +213 -0
- package/src/runtime/curl.d.ts +5 -0
- package/src/runtime/curl.js +81 -0
- package/src/runtime/cursor.d.ts +9 -0
- package/src/runtime/cursor.js +80 -0
- package/src/runtime/device.d.ts +14 -0
- package/src/runtime/device.js +73 -0
- package/src/runtime/dot-grid.d.ts +27 -0
- package/src/runtime/dot-grid.js +245 -0
- package/src/runtime/dot-surface.d.ts +46 -0
- package/src/runtime/dot-surface.js +429 -0
- package/src/runtime/feedback.d.ts +18 -0
- package/src/runtime/feedback.js +131 -0
- package/src/runtime/fit-text.d.ts +5 -0
- package/src/runtime/fit-text.js +60 -0
- package/src/runtime/fluid.d.ts +55 -0
- package/src/runtime/fluid.js +504 -0
- package/src/runtime/fluted-glass.d.ts +27 -0
- package/src/runtime/fluted-glass.js +157 -0
- package/src/runtime/gl.d.ts +19 -0
- package/src/runtime/gl.js +104 -0
- package/src/runtime/glass-glyph.d.ts +49 -0
- package/src/runtime/glass-glyph.js +355 -0
- package/src/runtime/glass-sheen.d.ts +2 -0
- package/src/runtime/glass-sheen.js +30 -0
- package/src/runtime/glsl.d.ts +17 -0
- package/src/runtime/glsl.js +78 -0
- package/src/runtime/glyphs.d.ts +22 -0
- package/src/runtime/glyphs.js +107 -0
- package/src/runtime/halo-ring.d.ts +47 -0
- package/src/runtime/halo-ring.js +334 -0
- package/src/runtime/height-field.d.ts +50 -0
- package/src/runtime/height-field.js +176 -0
- package/src/runtime/image-ripple.d.ts +25 -0
- package/src/runtime/image-ripple.js +353 -0
- package/src/runtime/image.d.ts +11 -0
- package/src/runtime/image.js +81 -0
- package/src/runtime/in-view.d.ts +6 -0
- package/src/runtime/in-view.js +36 -0
- package/src/runtime/ink-field/shaders.js +258 -0
- package/src/runtime/ink-field/sim.js +582 -0
- package/src/runtime/ink-field.d.ts +39 -0
- package/src/runtime/ink-field.js +311 -0
- package/src/runtime/ink.d.ts +31 -0
- package/src/runtime/ink.js +109 -0
- package/src/runtime/instancing.d.ts +12 -0
- package/src/runtime/instancing.js +37 -0
- package/src/runtime/iso-camera.d.ts +35 -0
- package/src/runtime/iso-camera.js +85 -0
- package/src/runtime/iso-city/figures.d.ts +30 -0
- package/src/runtime/iso-city/figures.js +136 -0
- package/src/runtime/iso-city/look.d.ts +34 -0
- package/src/runtime/iso-city/look.js +105 -0
- package/src/runtime/iso-city/plan.d.ts +59 -0
- package/src/runtime/iso-city/plan.js +463 -0
- package/src/runtime/iso-city/roads.d.ts +64 -0
- package/src/runtime/iso-city/roads.js +320 -0
- package/src/runtime/iso-city/scene.d.ts +27 -0
- package/src/runtime/iso-city/scene.js +432 -0
- package/src/runtime/iso-city/shaders.d.ts +6 -0
- package/src/runtime/iso-city/shaders.js +233 -0
- package/src/runtime/iso-city/shadows.d.ts +13 -0
- package/src/runtime/iso-city/shadows.js +148 -0
- package/src/runtime/iso-city/traffic.d.ts +80 -0
- package/src/runtime/iso-city/traffic.js +457 -0
- package/src/runtime/iso-city.d.ts +37 -0
- package/src/runtime/iso-city.js +458 -0
- package/src/runtime/lens-map.d.ts +11 -0
- package/src/runtime/lens-map.js +146 -0
- package/src/runtime/light-field.d.ts +29 -0
- package/src/runtime/light-field.js +292 -0
- package/src/runtime/liquid-glass.d.ts +48 -0
- package/src/runtime/liquid-glass.js +423 -0
- package/src/runtime/liquid-metal/beads.js +217 -0
- package/src/runtime/liquid-metal/field.js +88 -0
- package/src/runtime/liquid-metal/look.js +64 -0
- package/src/runtime/liquid-metal/shaders.js +255 -0
- package/src/runtime/liquid-metal/wave.js +53 -0
- package/src/runtime/liquid-metal.d.ts +46 -0
- package/src/runtime/liquid-metal.js +366 -0
- package/src/runtime/loop.d.ts +7 -0
- package/src/runtime/loop.js +112 -0
- package/src/runtime/magnetic.d.ts +9 -0
- package/src/runtime/magnetic.js +46 -0
- package/src/runtime/marquee.d.ts +2 -0
- package/src/runtime/marquee.js +27 -0
- package/src/runtime/mesh.d.ts +30 -0
- package/src/runtime/mesh.js +217 -0
- package/src/runtime/mosaic-reveal.d.ts +34 -0
- package/src/runtime/mosaic-reveal.js +504 -0
- package/src/runtime/motion.d.ts +2 -0
- package/src/runtime/motion.js +21 -0
- package/src/runtime/mount.d.ts +13 -0
- package/src/runtime/mount.js +120 -0
- package/src/runtime/nebula-logo.d.ts +31 -0
- package/src/runtime/nebula-logo.js +282 -0
- package/src/runtime/neon-swarm.d.ts +31 -0
- package/src/runtime/neon-swarm.js +258 -0
- package/src/runtime/ocean/presets.d.ts +54 -0
- package/src/runtime/ocean/presets.js +184 -0
- package/src/runtime/ocean/shader.d.ts +14 -0
- package/src/runtime/ocean/shader.js +389 -0
- package/src/runtime/ocean/view.d.ts +24 -0
- package/src/runtime/ocean/view.js +98 -0
- package/src/runtime/ocean/waves.d.ts +29 -0
- package/src/runtime/ocean/waves.js +119 -0
- package/src/runtime/ocean.d.ts +31 -0
- package/src/runtime/ocean.js +173 -0
- package/src/runtime/palettes.d.ts +37 -0
- package/src/runtime/palettes.js +75 -0
- package/src/runtime/paper-look.d.ts +42 -0
- package/src/runtime/paper-look.js +91 -0
- package/src/runtime/paper-looks.d.ts +17 -0
- package/src/runtime/paper-looks.js +21 -0
- package/src/runtime/paper.d.ts +37 -0
- package/src/runtime/paper.js +303 -0
- package/src/runtime/particle-logo.d.ts +31 -0
- package/src/runtime/particle-logo.js +196 -0
- package/src/runtime/ping-pong.d.ts +43 -0
- package/src/runtime/ping-pong.js +175 -0
- package/src/runtime/pixel-glitch.d.ts +17 -0
- package/src/runtime/pixel-glitch.js +287 -0
- package/src/runtime/point-cloud.d.ts +33 -0
- package/src/runtime/point-cloud.js +287 -0
- package/src/runtime/pointer.d.ts +27 -0
- package/src/runtime/pointer.js +128 -0
- package/src/runtime/points.d.ts +25 -0
- package/src/runtime/points.js +130 -0
- package/src/runtime/pools.d.ts +2 -0
- package/src/runtime/pools.js +25 -0
- package/src/runtime/product-stack.d.ts +24 -0
- package/src/runtime/product-stack.js +210 -0
- package/src/runtime/radiant-glass.d.ts +29 -0
- package/src/runtime/radiant-glass.js +282 -0
- package/src/runtime/rain-on-glass/backdrop.d.ts +22 -0
- package/src/runtime/rain-on-glass/backdrop.js +185 -0
- package/src/runtime/rain-on-glass/shader.d.ts +2 -0
- package/src/runtime/rain-on-glass/shader.js +168 -0
- package/src/runtime/rain-on-glass/wipe.d.ts +16 -0
- package/src/runtime/rain-on-glass/wipe.js +65 -0
- package/src/runtime/rain-on-glass.d.ts +27 -0
- package/src/runtime/rain-on-glass.js +210 -0
- package/src/runtime/ramp.d.ts +2 -0
- package/src/runtime/ramp.js +30 -0
- package/src/runtime/random.d.ts +2 -0
- package/src/runtime/random.js +22 -0
- package/src/runtime/scroll-reveal.d.ts +22 -0
- package/src/runtime/scroll-reveal.js +73 -0
- package/src/runtime/shader-background.d.ts +42 -0
- package/src/runtime/shader-background.js +261 -0
- package/src/runtime/shader-backgrounds/band-lens.js +72 -0
- package/src/runtime/shader-backgrounds/common.js +53 -0
- package/src/runtime/shader-backgrounds/grain-gradient.js +87 -0
- package/src/runtime/shader-backgrounds/grid.js +33 -0
- package/src/runtime/shader-backgrounds/mesh-gradient.js +74 -0
- package/src/runtime/shader-backgrounds/silk.js +57 -0
- package/src/runtime/shader-backgrounds/waves.js +31 -0
- package/src/runtime/shape.d.ts +42 -0
- package/src/runtime/shape.js +313 -0
- package/src/runtime/silk-ribbon.d.ts +23 -0
- package/src/runtime/silk-ribbon.js +206 -0
- package/src/runtime/slideshow.d.ts +27 -0
- package/src/runtime/slideshow.js +234 -0
- package/src/runtime/spotlight.d.ts +9 -0
- package/src/runtime/spotlight.js +97 -0
- package/src/runtime/stack-poses.d.ts +53 -0
- package/src/runtime/stack-poses.js +273 -0
- package/src/runtime/stage.d.ts +47 -0
- package/src/runtime/stage.js +187 -0
- package/src/runtime/swarm.d.ts +49 -0
- package/src/runtime/swarm.js +199 -0
- package/src/runtime/text-motion.d.ts +41 -0
- package/src/runtime/text-motion.js +165 -0
- package/src/runtime/text.d.ts +11 -0
- package/src/runtime/text.js +90 -0
- package/src/runtime/tilt.d.ts +19 -0
- package/src/runtime/tilt.js +116 -0
- package/src/runtime/topography.d.ts +71 -0
- package/src/runtime/topography.js +650 -0
- package/src/runtime/trackers.d.ts +21 -0
- package/src/runtime/trackers.js +76 -0
- package/src/runtime/variable-type.d.ts +43 -0
- package/src/runtime/variable-type.js +358 -0
- package/src/runtime/voxel-logo.d.ts +31 -0
- package/src/runtime/voxel-logo.js +237 -0
- package/src/runtime/waves.d.ts +70 -0
- package/src/runtime/waves.js +799 -0
- package/src/runtime/world-labels.d.ts +29 -0
- package/src/runtime/world-labels.js +59 -0
- package/src/runtime/world-land.d.ts +8 -0
- package/src/runtime/world-land.js +105 -0
- package/src/runtime/world-map.d.ts +69 -0
- package/src/runtime/world-map.js +308 -0
- package/src/runtime/world-plan.d.ts +101 -0
- package/src/runtime/world-plan.js +333 -0
- package/src/runtime/world-routes.d.ts +75 -0
- package/src/runtime/world-routes.js +673 -0
- package/src/styles/border-sweep.css +82 -0
- package/src/styles/glass.css +104 -0
- package/src/styles/motion.css +218 -0
- package/src/styles/reveal.css +51 -0
- package/src/vendor/paper/LICENSE +201 -0
- package/src/vendor/paper/NOTICE +5 -0
- package/src/vendor/paper/README.md +8 -0
- package/src/vendor/paper/get-shader-color-from-string.js +100 -0
- package/src/vendor/paper/get-shader-noise-texture.js +17 -0
- package/src/vendor/paper/presets.js +3678 -0
- package/src/vendor/paper/shader-sizing.js +37 -0
- package/src/vendor/paper/shader-utils.js +130 -0
- package/src/vendor/paper/shaders/color-panels.js +223 -0
- package/src/vendor/paper/shaders/dithering.js +270 -0
- package/src/vendor/paper/shaders/dot-grid.js +109 -0
- package/src/vendor/paper/shaders/dot-orbit.js +122 -0
- package/src/vendor/paper/shaders/fluted-glass.js +360 -0
- package/src/vendor/paper/shaders/gem-smoke.js +572 -0
- package/src/vendor/paper/shaders/god-rays.js +132 -0
- package/src/vendor/paper/shaders/grain-gradient.js +300 -0
- package/src/vendor/paper/shaders/halftone-cmyk.js +294 -0
- package/src/vendor/paper/shaders/halftone-dots.js +320 -0
- package/src/vendor/paper/shaders/heatmap.js +383 -0
- package/src/vendor/paper/shaders/image-dithering.js +198 -0
- package/src/vendor/paper/shaders/lens-distortion.js +275 -0
- package/src/vendor/paper/shaders/liquid-metal.js +632 -0
- package/src/vendor/paper/shaders/mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/metaballs.js +110 -0
- package/src/vendor/paper/shaders/neuro-noise.js +76 -0
- package/src/vendor/paper/shaders/paper-texture.js +526 -0
- package/src/vendor/paper/shaders/perlin-noise.js +166 -0
- package/src/vendor/paper/shaders/pulsing-border.js +238 -0
- package/src/vendor/paper/shaders/simplex-noise.js +91 -0
- package/src/vendor/paper/shaders/smoke-ring.js +132 -0
- package/src/vendor/paper/shaders/spiral.js +82 -0
- package/src/vendor/paper/shaders/static-mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/static-radial-gradient.js +187 -0
- package/src/vendor/paper/shaders/swirl.js +103 -0
- package/src/vendor/paper/shaders/voronoi.js +138 -0
- package/src/vendor/paper/shaders/warp.js +129 -0
- package/src/vendor/paper/shaders/water.js +121 -0
- package/src/vendor/paper/shaders/waves.js +65 -0
- package/src/vendor/paper/vertex-shader.js +159 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** What a shape is drawn from: a word in a font, or an image. */
|
|
2
|
+
export interface ShapeSource {
|
|
3
|
+
/** A word or short line to draw. */
|
|
4
|
+
text?: string;
|
|
5
|
+
/** The CSS font for `text`, without the size. Default: the page's body font, bold. */
|
|
6
|
+
font?: string;
|
|
7
|
+
/** An image URL (an SVG logo, a PNG) to draw instead of text. */
|
|
8
|
+
src?: string;
|
|
9
|
+
/** Space around the shape, a share of its area. Default 0.08. */
|
|
10
|
+
padding?: number;
|
|
11
|
+
/** Where in the canvas the shape goes, each 0–1 from the top left. Default all of it. */
|
|
12
|
+
area?: { x: number; y: number; width: number; height: number };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A shape drawn white on transparent, and its coverage a byte a pixel. */
|
|
16
|
+
export interface Mask {
|
|
17
|
+
canvas: HTMLCanvasElement;
|
|
18
|
+
alpha: Uint8ClampedArray;
|
|
19
|
+
width: number;
|
|
20
|
+
height: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A CSS font at a size, style and weight words first: `fontAt('900 Anton, sans-serif', 120)` is `900 120px Anton, sans-serif`. */
|
|
24
|
+
export function fontAt(font: string, size: number): string;
|
|
25
|
+
|
|
26
|
+
/** Draws a shape into a mask, fitted with its aspect kept. Rejects when an image cannot be loaded. */
|
|
27
|
+
export function drawShape(source: ShapeSource, width: number, height: number): Promise<Mask>;
|
|
28
|
+
|
|
29
|
+
/** `count` points scattered evenly over a shape: x, y pairs, 0–1, y up. */
|
|
30
|
+
export function samplePoints(mask: Mask, count: number, random?: () => number): Float32Array;
|
|
31
|
+
|
|
32
|
+
/** `count` points scattered along a shape's edge (covered pixels beside uncovered ones): x, y pairs, 0–1, y up. */
|
|
33
|
+
export function edgePoints(mask: Mask, count: number, random?: () => number): Float32Array;
|
|
34
|
+
|
|
35
|
+
/** Points on a grid `columns` across, wherever a cell's centre is inside the shape: x, y pairs, 0–1, y up. */
|
|
36
|
+
export function gridPoints(mask: Mask, columns: number): Float32Array;
|
|
37
|
+
|
|
38
|
+
/** A signed distance field, a byte a pixel: 0.5 at the edge, 1 `spread` px inside, 0 `spread` px outside. */
|
|
39
|
+
export function distanceField(mask: Mask, spread?: number): Uint8Array;
|
|
40
|
+
|
|
41
|
+
/** Softens a one-byte field (rows from the top) with a near-gaussian blur of `radius` px; the input is left unchanged. */
|
|
42
|
+
export function blurField(field: Uint8Array, width: number, height: number, radius: number): Uint8Array;
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/shape
|
|
3
|
+
*
|
|
4
|
+
* A shape for an effect to be made of: a word in a font, or an image (an
|
|
5
|
+
* SVG logo, a PNG), drawn white into a canvas as a mask. From the mask,
|
|
6
|
+
* effects take points (particles that form the shape), a distance field
|
|
7
|
+
* (glass that bevels from its edges), or the mask itself (voxels).
|
|
8
|
+
*
|
|
9
|
+
* Shapes are drawn once the page's fonts have loaded, so a word is in the
|
|
10
|
+
* font the page uses, not a fallback.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { loadImage } from './image.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {object} ShapeSource
|
|
17
|
+
* @property {string} [text] A word or short line to draw.
|
|
18
|
+
* @property {string} [font] The CSS font for `text`, without the size
|
|
19
|
+
* (default: the page's body font, bold).
|
|
20
|
+
* @property {string} [src] An image URL (an SVG logo, a PNG) to draw instead of text.
|
|
21
|
+
* @property {number} [padding=0.08] Space around the shape, a share of its area.
|
|
22
|
+
* @property {{ x: number, y: number, width: number, height: number }} [area]
|
|
23
|
+
* Where in the canvas the shape goes, each 0–1 from the top left (default: all of it).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {object} Mask
|
|
28
|
+
* @property {HTMLCanvasElement} canvas The shape, white on transparent.
|
|
29
|
+
* @property {Uint8ClampedArray} alpha One byte a pixel: the shape's coverage, 0–255.
|
|
30
|
+
* @property {number} width The canvas width.
|
|
31
|
+
* @property {number} height The canvas height.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A CSS font at a size: the style and weight words of `font` go before the
|
|
36
|
+
* size, as the canvas needs them, and the family after.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} font A CSS font without a size, e.g. `900 Anton, sans-serif`.
|
|
39
|
+
* @param {number} size The size in pixels.
|
|
40
|
+
* @returns {string} e.g. `900 120px Anton, sans-serif`.
|
|
41
|
+
*/
|
|
42
|
+
export function fontAt(font, size) {
|
|
43
|
+
const lead = font.match(/^((?:normal|italic|oblique|small-caps|bold|bolder|lighter|\d{3})\s+)*/)?.[0] ?? '';
|
|
44
|
+
return `${lead}${size}px ${font.slice(lead.length)}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Draws a shape into a mask, fitted inside the canvas with its aspect kept.
|
|
49
|
+
*
|
|
50
|
+
* @param {ShapeSource} source What to draw.
|
|
51
|
+
* @param {number} width The mask's width in pixels.
|
|
52
|
+
* @param {number} height The mask's height in pixels.
|
|
53
|
+
* @returns {Promise<Mask>} The mask.
|
|
54
|
+
* @throws {Error} When an image source cannot be loaded.
|
|
55
|
+
*/
|
|
56
|
+
export async function drawShape(source, width, height) {
|
|
57
|
+
const canvas = document.createElement('canvas');
|
|
58
|
+
canvas.width = width;
|
|
59
|
+
canvas.height = height;
|
|
60
|
+
const ctx = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d', { willReadFrequently: true }));
|
|
61
|
+
const pad = source.padding ?? 0.08;
|
|
62
|
+
const area = source.area ?? { x: 0, y: 0, width: 1, height: 1 };
|
|
63
|
+
const room = { w: width * area.width * (1 - pad * 2), h: height * area.height * (1 - pad * 2) };
|
|
64
|
+
// The middle of the area: shapes are centred on it.
|
|
65
|
+
const cx = width * (area.x + area.width / 2);
|
|
66
|
+
const cy = height * (area.y + area.height / 2);
|
|
67
|
+
ctx.fillStyle = '#fff';
|
|
68
|
+
|
|
69
|
+
if (source.src) {
|
|
70
|
+
const img = await loadImage(source.src);
|
|
71
|
+
const scale = Math.min(room.w / (img.naturalWidth || room.w), room.h / (img.naturalHeight || room.h));
|
|
72
|
+
const w = (img.naturalWidth || room.w) * scale;
|
|
73
|
+
const h = (img.naturalHeight || room.h) * scale;
|
|
74
|
+
ctx.drawImage(img, cx - w / 2, cy - h / 2, w, h);
|
|
75
|
+
// Whatever its colours, the shape is where it is opaque.
|
|
76
|
+
ctx.globalCompositeOperation = 'source-in';
|
|
77
|
+
ctx.fillRect(0, 0, width, height);
|
|
78
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
79
|
+
} else {
|
|
80
|
+
await document.fonts?.ready;
|
|
81
|
+
const family = source.font ?? `700 ${getComputedStyle(document.body).fontFamily}`;
|
|
82
|
+
const text = source.text ?? '';
|
|
83
|
+
ctx.font = fontAt(family, 100);
|
|
84
|
+
const m = ctx.measureText(text);
|
|
85
|
+
const textHeight = (m.actualBoundingBoxAscent + m.actualBoundingBoxDescent) || 100;
|
|
86
|
+
const size = 100 * Math.min(room.w / (m.width || 1), room.h / textHeight);
|
|
87
|
+
ctx.font = fontAt(family, size);
|
|
88
|
+
const fitted = ctx.measureText(text);
|
|
89
|
+
ctx.textBaseline = 'alphabetic';
|
|
90
|
+
const top = cy - (fitted.actualBoundingBoxAscent + fitted.actualBoundingBoxDescent) / 2;
|
|
91
|
+
ctx.fillText(text, cx - fitted.width / 2, top + fitted.actualBoundingBoxAscent);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const data = ctx.getImageData(0, 0, width, height).data;
|
|
95
|
+
const alpha = new Uint8ClampedArray(width * height);
|
|
96
|
+
for (let i = 0; i < alpha.length; i++) alpha[i] = data[i * 4 + 3];
|
|
97
|
+
return { canvas, alpha, width, height };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Scatters points over a shape, evenly: each lands on a pixel the shape
|
|
102
|
+
* covers, with a little jitter so they do not sit on a grid.
|
|
103
|
+
*
|
|
104
|
+
* @param {Mask} mask The shape.
|
|
105
|
+
* @param {number} count How many points.
|
|
106
|
+
* @param {() => number} [random=Math.random] The random source (seed it for repeatable layouts).
|
|
107
|
+
* @returns {Float32Array} x, y pairs, each 0–1 (y up).
|
|
108
|
+
*/
|
|
109
|
+
export function samplePoints(mask, count, random = Math.random) {
|
|
110
|
+
const inside = [];
|
|
111
|
+
for (let i = 0; i < mask.alpha.length; i++) if (mask.alpha[i] > 127) inside.push(i);
|
|
112
|
+
const points = new Float32Array(count * 2);
|
|
113
|
+
if (!inside.length) return points;
|
|
114
|
+
for (let k = 0; k < count; k++) {
|
|
115
|
+
const i = inside[Math.floor(random() * inside.length)];
|
|
116
|
+
const x = (i % mask.width) + random();
|
|
117
|
+
const y = Math.floor(i / mask.width) + random();
|
|
118
|
+
points[k * 2] = x / mask.width;
|
|
119
|
+
points[k * 2 + 1] = 1 - y / mask.height;
|
|
120
|
+
}
|
|
121
|
+
return points;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Scatters points along a shape's edge: each lands on a covered pixel with
|
|
126
|
+
* an uncovered one beside it, with a little jitter, for effects that trace
|
|
127
|
+
* the outline more brightly than the fill.
|
|
128
|
+
*
|
|
129
|
+
* @param {Mask} mask The shape.
|
|
130
|
+
* @param {number} count How many points.
|
|
131
|
+
* @param {() => number} [random=Math.random] The random source.
|
|
132
|
+
* @returns {Float32Array} x, y pairs, each 0–1 (y up); zeros when the shape has no edge.
|
|
133
|
+
*/
|
|
134
|
+
export function edgePoints(mask, count, random = Math.random) {
|
|
135
|
+
const { width, height, alpha } = mask;
|
|
136
|
+
/**
|
|
137
|
+
* Is the pixel at (x, y) covered? Outside the mask counts as not.
|
|
138
|
+
*
|
|
139
|
+
* @param {number} x Across.
|
|
140
|
+
* @param {number} y Down.
|
|
141
|
+
* @returns {boolean} Whether the shape covers it.
|
|
142
|
+
*/
|
|
143
|
+
const covered = (x, y) => x >= 0 && y >= 0 && x < width && y < height && alpha[y * width + x] > 127;
|
|
144
|
+
const edge = [];
|
|
145
|
+
for (let y = 0; y < height; y++) {
|
|
146
|
+
for (let x = 0; x < width; x++) {
|
|
147
|
+
if (covered(x, y) && !(covered(x - 1, y) && covered(x + 1, y) && covered(x, y - 1) && covered(x, y + 1))) edge.push(y * width + x);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const points = new Float32Array(count * 2);
|
|
151
|
+
if (!edge.length) return points;
|
|
152
|
+
for (let k = 0; k < count; k++) {
|
|
153
|
+
const i = edge[Math.floor(random() * edge.length)];
|
|
154
|
+
points[k * 2] = ((i % width) + random()) / width;
|
|
155
|
+
points[k * 2 + 1] = 1 - (Math.floor(i / width) + random()) / height;
|
|
156
|
+
}
|
|
157
|
+
return points;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Points on a grid over a shape: one wherever a grid cell's centre is
|
|
162
|
+
* inside, for shapes drawn as a dot matrix.
|
|
163
|
+
*
|
|
164
|
+
* @param {Mask} mask The shape.
|
|
165
|
+
* @param {number} columns Grid cells across; rows follow from the aspect.
|
|
166
|
+
* @returns {Float32Array} x, y pairs, each 0–1 (y up).
|
|
167
|
+
*/
|
|
168
|
+
export function gridPoints(mask, columns) {
|
|
169
|
+
const step = mask.width / columns;
|
|
170
|
+
const rows = Math.floor(mask.height / step);
|
|
171
|
+
const out = [];
|
|
172
|
+
for (let r = 0; r < rows; r++) {
|
|
173
|
+
for (let c = 0; c < columns; c++) {
|
|
174
|
+
const x = Math.floor((c + 0.5) * step);
|
|
175
|
+
const y = Math.floor((r + 0.5) * step);
|
|
176
|
+
if (mask.alpha[y * mask.width + x] > 127) out.push(x / mask.width, 1 - y / mask.height);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return new Float32Array(out);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* One row of a squared Euclidean distance transform (Felzenszwalb and
|
|
184
|
+
* Huttenlocher): for each cell, the squared distance to the nearest cell
|
|
185
|
+
* whose value is 0.
|
|
186
|
+
*
|
|
187
|
+
* @param {Float64Array} f The row, in place: 0 at sources, a large number elsewhere.
|
|
188
|
+
* @param {number} n Its length.
|
|
189
|
+
* @param {Float64Array} d Scratch for the output.
|
|
190
|
+
* @param {Int32Array} v Scratch: parabola positions.
|
|
191
|
+
* @param {Float64Array} z Scratch: parabola boundaries.
|
|
192
|
+
*/
|
|
193
|
+
function edt1d(f, n, d, v, z) {
|
|
194
|
+
let k = 0;
|
|
195
|
+
v[0] = 0;
|
|
196
|
+
z[0] = -Infinity;
|
|
197
|
+
z[1] = Infinity;
|
|
198
|
+
for (let q = 1; q < n; q++) {
|
|
199
|
+
let s = (f[q] + q * q - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);
|
|
200
|
+
while (s <= z[k]) {
|
|
201
|
+
k--;
|
|
202
|
+
s = (f[q] + q * q - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);
|
|
203
|
+
}
|
|
204
|
+
k++;
|
|
205
|
+
v[k] = q;
|
|
206
|
+
z[k] = s;
|
|
207
|
+
z[k + 1] = Infinity;
|
|
208
|
+
}
|
|
209
|
+
k = 0;
|
|
210
|
+
for (let q = 0; q < n; q++) {
|
|
211
|
+
while (z[k + 1] < q) k++;
|
|
212
|
+
d[q] = (q - v[k]) * (q - v[k]) + f[v[k]];
|
|
213
|
+
}
|
|
214
|
+
for (let q = 0; q < n; q++) f[q] = d[q];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The distance from every pixel to the nearest pixel where `set(i)` is true.
|
|
219
|
+
*
|
|
220
|
+
* @param {number} width The grid's width.
|
|
221
|
+
* @param {number} height The grid's height.
|
|
222
|
+
* @param {(i: number) => boolean} set Whether pixel i is a source.
|
|
223
|
+
* @returns {Float64Array} Distances in pixels.
|
|
224
|
+
*/
|
|
225
|
+
function distanceTo(width, height, set) {
|
|
226
|
+
const grid = new Float64Array(width * height);
|
|
227
|
+
for (let i = 0; i < grid.length; i++) grid[i] = set(i) ? 0 : 1e20;
|
|
228
|
+
const n = Math.max(width, height);
|
|
229
|
+
const f = new Float64Array(n);
|
|
230
|
+
const d = new Float64Array(n);
|
|
231
|
+
const v = new Int32Array(n);
|
|
232
|
+
const z = new Float64Array(n + 1);
|
|
233
|
+
for (let x = 0; x < width; x++) {
|
|
234
|
+
for (let y = 0; y < height; y++) f[y] = grid[y * width + x];
|
|
235
|
+
edt1d(f, height, d, v, z);
|
|
236
|
+
for (let y = 0; y < height; y++) grid[y * width + x] = f[y];
|
|
237
|
+
}
|
|
238
|
+
for (let y = 0; y < height; y++) {
|
|
239
|
+
for (let x = 0; x < width; x++) f[x] = grid[y * width + x];
|
|
240
|
+
edt1d(f, width, d, v, z);
|
|
241
|
+
for (let x = 0; x < width; x++) grid[y * width + x] = Math.sqrt(f[x]);
|
|
242
|
+
}
|
|
243
|
+
return grid;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* A signed distance field of a shape, packed for a texture: 0.5 at the
|
|
248
|
+
* edge, rising inside, falling outside, `spread` pixels from the edge to
|
|
249
|
+
* either end.
|
|
250
|
+
*
|
|
251
|
+
* @param {Mask} mask The shape.
|
|
252
|
+
* @param {number} [spread=24] How far from the edge, in mask pixels, the field reaches 0 or 1.
|
|
253
|
+
* @returns {Uint8Array} One byte a pixel, rows from the top.
|
|
254
|
+
*/
|
|
255
|
+
export function distanceField(mask, spread = 24) {
|
|
256
|
+
const { width, height, alpha } = mask;
|
|
257
|
+
const outside = distanceTo(width, height, (i) => alpha[i] > 127);
|
|
258
|
+
const inside = distanceTo(width, height, (i) => alpha[i] <= 127);
|
|
259
|
+
const out = new Uint8Array(width * height);
|
|
260
|
+
for (let i = 0; i < out.length; i++) {
|
|
261
|
+
const signed = inside[i] - outside[i];
|
|
262
|
+
out[i] = Math.max(0, Math.min(255, Math.round((0.5 + signed / (2 * spread)) * 255)));
|
|
263
|
+
}
|
|
264
|
+
return out;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Softens a one-byte field with two passes of a box blur, across then down,
|
|
269
|
+
* twice: near enough a gaussian. Rounds the ridge a distance field has down
|
|
270
|
+
* the middle of every stroke.
|
|
271
|
+
*
|
|
272
|
+
* @param {Uint8Array} field The field, rows from the top; left unchanged.
|
|
273
|
+
* @param {number} width Its width.
|
|
274
|
+
* @param {number} height Its height.
|
|
275
|
+
* @param {number} radius The blur's reach, in pixels.
|
|
276
|
+
* @returns {Uint8Array} The softened field.
|
|
277
|
+
*/
|
|
278
|
+
export function blurField(field, width, height, radius) {
|
|
279
|
+
const r = Math.max(1, Math.round(radius));
|
|
280
|
+
let src = Float32Array.from(field);
|
|
281
|
+
let dst = new Float32Array(src.length);
|
|
282
|
+
/**
|
|
283
|
+
* One box pass along rows or columns.
|
|
284
|
+
*
|
|
285
|
+
* @param {boolean} across Along rows (true) or down columns (false).
|
|
286
|
+
*/
|
|
287
|
+
const pass = (across) => {
|
|
288
|
+
const lines = across ? height : width;
|
|
289
|
+
const length = across ? width : height;
|
|
290
|
+
/**
|
|
291
|
+
* A pixel's index, by line and position along it.
|
|
292
|
+
*
|
|
293
|
+
* @param {number} line The row (across) or column (down).
|
|
294
|
+
* @param {number} i The position along it.
|
|
295
|
+
* @returns {number} The index into the field.
|
|
296
|
+
*/
|
|
297
|
+
const at = (line, i) => (across ? line * width + i : i * width + line);
|
|
298
|
+
for (let line = 0; line < lines; line++) {
|
|
299
|
+
let sum = 0;
|
|
300
|
+
for (let i = -r; i <= r; i++) sum += src[at(line, Math.min(length - 1, Math.max(0, i)))];
|
|
301
|
+
for (let i = 0; i < length; i++) {
|
|
302
|
+
dst[at(line, i)] = sum / (2 * r + 1);
|
|
303
|
+
sum += src[at(line, Math.min(length - 1, i + r + 1))] - src[at(line, Math.max(0, i - r))];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
[src, dst] = [dst, src];
|
|
307
|
+
};
|
|
308
|
+
pass(true);
|
|
309
|
+
pass(false);
|
|
310
|
+
pass(true);
|
|
311
|
+
pass(false);
|
|
312
|
+
return Uint8Array.from(src, (v) => Math.round(v));
|
|
313
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface SilkRibbonConfig {
|
|
4
|
+
/** The satin's colours, shaded from the darkest to the lightest whatever their order. Default hot pink, coral, peach. */
|
|
5
|
+
colors?: string[];
|
|
6
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
|
|
7
|
+
palette?: PaletteName | (string & {});
|
|
8
|
+
/** The ground around the ribbon. Default `#f6f4f1`. */
|
|
9
|
+
background?: string;
|
|
10
|
+
/** Where the ribbon runs. Default `right`. */
|
|
11
|
+
side?: 'right' | 'left' | 'center';
|
|
12
|
+
/** How tightly it turns along its length; 0 lies flat. Default 1. */
|
|
13
|
+
twist?: number;
|
|
14
|
+
/** How deep the folds along it are; 0 for flat satin. Default 1. */
|
|
15
|
+
folds?: number;
|
|
16
|
+
/** The soft out-of-focus glow behind it; 0 for none. Default 1. */
|
|
17
|
+
glow?: number;
|
|
18
|
+
/** How fast the folds move. Default 1. */
|
|
19
|
+
speed?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Starts a silk ribbon in `el`'s canvas. Returns a function that stops it. */
|
|
23
|
+
export function silkRibbon(el: HTMLElement, config: SilkRibbonConfig): () => void;
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/silk-ribbon
|
|
3
|
+
*
|
|
4
|
+
* A wide ribbon of satin twisting across a light ground, a soft
|
|
5
|
+
* out-of-focus glow of the same colours behind it. The ribbon is shaded
|
|
6
|
+
* analytically in one pass: it turns about its length, so it narrows to a
|
|
7
|
+
* pinch where it shows its edge and widens where it faces you; soft folds
|
|
8
|
+
* run along it, gathering at each pinch, and a satin sheen picks out
|
|
9
|
+
* every fold as it turns to the light. Its colour runs from the darkest
|
|
10
|
+
* colour in its shadows to the lightest on its highlights, mixed in Oklab.
|
|
11
|
+
*
|
|
12
|
+
* The folds undulate slowly and lean towards the pointer. Reduced motion
|
|
13
|
+
* gets a still frame.
|
|
14
|
+
*
|
|
15
|
+
* Opaque: the effect is the whole background of what it fills. Without
|
|
16
|
+
* WebGL it removes its canvas and the element's background shows.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { createStage } from './stage.js';
|
|
20
|
+
import { GLSL_OKLAB, parseColor, toOklab } from './color.js';
|
|
21
|
+
import { canHover } from './device.js';
|
|
22
|
+
import { pointerIn } from './pointer.js';
|
|
23
|
+
import { watchColors } from './mount.js';
|
|
24
|
+
import { GLSL_NOISE, paletteUniform } from './glsl.js';
|
|
25
|
+
import { pickColors } from './palettes.js';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {object} SilkRibbonConfig
|
|
29
|
+
* @property {string[]} [colors] The satin's colours, shaded from the darkest to the lightest whatever their order (default hot pink, coral, peach).
|
|
30
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
|
|
31
|
+
* @property {string} [background='#f6f4f1'] The ground around the ribbon.
|
|
32
|
+
* @property {'right' | 'left' | 'center'} [side='right'] Where the ribbon runs.
|
|
33
|
+
* @property {number} [twist=1] How tightly it turns along its length; 0 lies flat.
|
|
34
|
+
* @property {number} [folds=1] How deep the folds along it are; 0 for flat satin.
|
|
35
|
+
* @property {number} [glow=1] The soft out-of-focus glow behind it; 0 for none.
|
|
36
|
+
* @property {number} [speed=1] How fast the folds move.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
const FRAG = `
|
|
40
|
+
precision highp float;
|
|
41
|
+
uniform vec2 uRes;
|
|
42
|
+
uniform float uTime;
|
|
43
|
+
uniform float uLean;
|
|
44
|
+
uniform float uLeanAt;
|
|
45
|
+
uniform float uSide;
|
|
46
|
+
uniform float uTwist;
|
|
47
|
+
uniform float uFolds;
|
|
48
|
+
uniform float uGlow;
|
|
49
|
+
uniform vec3 uGround;
|
|
50
|
+
uniform vec3 uPal[3];
|
|
51
|
+
${GLSL_OKLAB}
|
|
52
|
+
${GLSL_NOISE}
|
|
53
|
+
|
|
54
|
+
// The satin's colour for a shade 0 (in shadow) to 1 (lit), dark to light in Oklab.
|
|
55
|
+
vec3 satin(float k) {
|
|
56
|
+
k = clamp(k, 0.0, 1.0);
|
|
57
|
+
vec3 lab = k < 0.5 ? mix(uPal[0], uPal[1], smoothstep(0.0, 0.5, k)) : mix(uPal[1], uPal[2], smoothstep(0.5, 1.0, k));
|
|
58
|
+
return fromOklab(lab);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// The ribbon's centre line, across its axis, at a point along it.
|
|
62
|
+
float centre(float s, float t) {
|
|
63
|
+
return 0.1 * sin(s * 2.0 + t * 0.31) + 0.03 * sin(s * 3.3 - t * 0.23) + uLean * 0.03;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// How far the ribbon has turned about its length at a point along it.
|
|
67
|
+
float turn(float s, float t) {
|
|
68
|
+
float lean = uLean * 0.5 * exp(-(s - uLeanAt) * (s - uLeanAt) * 3.0);
|
|
69
|
+
return uTwist * (1.7 * s + 0.5 * sin(s * 1.3 + t * 0.21)) + 0.8 + 0.6 * sin(t * 0.11) + lean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void main() {
|
|
73
|
+
float t = uTime;
|
|
74
|
+
// Centred, in units of the height; on a narrow screen, of a shorter
|
|
75
|
+
// height, so the ribbon keeps its shape and fits the width.
|
|
76
|
+
float zoom = min(uRes.x / uRes.y / 1.5, 1.0);
|
|
77
|
+
vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / (uRes.y * zoom);
|
|
78
|
+
float aspect = uRes.x / (uRes.y * zoom);
|
|
79
|
+
p.x *= uSide < 0.0 ? -1.0 : 1.0;
|
|
80
|
+
// The ribbon's axis leans right as it rises; along it s, across it x.
|
|
81
|
+
vec2 origin = vec2(abs(uSide) * max(aspect * 0.5 - 0.42, 0.12), 0.0);
|
|
82
|
+
vec2 axis = normalize(vec2(0.42, 1.0));
|
|
83
|
+
vec2 side = vec2(axis.y, -axis.x);
|
|
84
|
+
vec2 q = p - origin;
|
|
85
|
+
float s = dot(q, axis);
|
|
86
|
+
float x = dot(q, side);
|
|
87
|
+
float eps = 1.5 / (uRes.y * zoom);
|
|
88
|
+
|
|
89
|
+
// The ground, and the glow: a wide blurred ribbon behind, warmer on its
|
|
90
|
+
// near side, as if a second length of the same silk were out of focus.
|
|
91
|
+
vec3 col = uGround;
|
|
92
|
+
float gx = x - 0.02 - 0.08 * sin(s * 1.4 - t * 0.17);
|
|
93
|
+
float gw = 0.44 + 0.06 * sin(s * 2.1 + t * 0.19);
|
|
94
|
+
float glow = smoothstep(gw + 0.3, gw - 0.1, abs(gx)) * uGlow;
|
|
95
|
+
col = mix(col, satin(0.62 - 0.55 * smoothstep(-0.45, 0.2, gx) + 0.06 * sin(s * 2.0 + t * 0.2)), glow);
|
|
96
|
+
|
|
97
|
+
// The ribbon: half its width shows as it turns, never quite none.
|
|
98
|
+
float c = centre(s, t);
|
|
99
|
+
float a = turn(s, t);
|
|
100
|
+
float facing = cos(a);
|
|
101
|
+
float hw = 0.36 * sqrt(facing * facing + 0.004);
|
|
102
|
+
float dx = x - c;
|
|
103
|
+
// A soft shadow on the glow beside the ribbon.
|
|
104
|
+
col *= 1.0 - 0.1 * glow * smoothstep(hw + 0.2, hw, abs(dx - 0.03));
|
|
105
|
+
float inside = smoothstep(hw + eps, hw - eps, abs(dx));
|
|
106
|
+
if (inside > 0.0) {
|
|
107
|
+
// Across the cloth, -1 to 1, the same thread wherever it turns.
|
|
108
|
+
float v = dx / hw * sign(facing);
|
|
109
|
+
// Folds along the length: their slope tilts the normal about the axis.
|
|
110
|
+
float fold = uFolds * (0.6 * cos(v * 6.5 + s * 1.2 + t * 0.4) + 0.35 * cos(v * 10.0 - s * 2.0 - t * 0.33));
|
|
111
|
+
float phi = a + atan(fold * 1.6);
|
|
112
|
+
// The normal, in screen terms; turned to face the viewer on the back.
|
|
113
|
+
vec3 n = vec3(-sin(phi) * side, cos(phi));
|
|
114
|
+
n.z = abs(n.z);
|
|
115
|
+
n.xy *= sign(facing);
|
|
116
|
+
n.xy += axis * 0.12 * sin(s * 2.6 + v * 1.5 - t * 0.3);
|
|
117
|
+
n = normalize(n);
|
|
118
|
+
// Light from the upper left, from the upper right on the back, so both faces show their folds.
|
|
119
|
+
vec3 light = normalize(vec3(-0.8 * sign(facing), 0.3, 0.3));
|
|
120
|
+
vec3 view = vec3(0.0, 0.0, 1.0);
|
|
121
|
+
// Wrapped light, so no fold goes flat in shadow.
|
|
122
|
+
float diffuse = dot(n, light) * 0.5 + 0.5;
|
|
123
|
+
float k = pow(diffuse, 3.4);
|
|
124
|
+
// Faint streaks woven along the length.
|
|
125
|
+
k += 0.12 * (noise(vec2(v * 4.0, s * 0.7 + t * 0.03)) - 0.5);
|
|
126
|
+
vec3 silk = satin(k);
|
|
127
|
+
// The satin sheen: a broad glow and a tighter highlight off each fold.
|
|
128
|
+
float nh = max(dot(n, normalize(light + view)), 0.0);
|
|
129
|
+
float sheen = pow(nh, 12.0) * 0.25 + pow(nh, 60.0) * 0.3;
|
|
130
|
+
silk = mix(silk, fromOklab(uPal[2]) + 0.05, clamp(sheen, 0.0, 1.0));
|
|
131
|
+
// A slightly lighter rolled hem.
|
|
132
|
+
silk = mix(silk, satin(k + 0.25), smoothstep(0.88, 1.0, abs(v)) * 0.5);
|
|
133
|
+
col = mix(col, silk, inside);
|
|
134
|
+
}
|
|
135
|
+
col += (hash(gl_FragCoord.xy + fract(t)) - 0.5) / 255.0 * 1.5;
|
|
136
|
+
gl_FragColor = vec4(col, 1.0);
|
|
137
|
+
}
|
|
138
|
+
`;
|
|
139
|
+
|
|
140
|
+
/** The default satin: hot pink, coral, peach. */
|
|
141
|
+
const COLORS = ['#e2357a', '#f0906a', '#f5d0a4'];
|
|
142
|
+
|
|
143
|
+
/** Where each `side` puts the ribbon, for `uSide`. */
|
|
144
|
+
const SIDES = { right: 1, left: -1, center: 0 };
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Colours sorted dark to light, as the satin shades them.
|
|
148
|
+
*
|
|
149
|
+
* @param {string[]} colors CSS hex colours.
|
|
150
|
+
* @returns {string[]} The first three, darkest first.
|
|
151
|
+
*/
|
|
152
|
+
const byLightness = (colors) =>
|
|
153
|
+
[0, 1, 2]
|
|
154
|
+
.map((i) => colors[i % colors.length])
|
|
155
|
+
.sort((a, b) => toOklab(parseColor(a))[0] - toOklab(parseColor(b))[0]);
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Starts a silk ribbon on an element: an opaque canvas inside it, filling it.
|
|
159
|
+
*
|
|
160
|
+
* @param {HTMLElement} el The element (its first canvas is drawn into).
|
|
161
|
+
* @param {SilkRibbonConfig} config The settings.
|
|
162
|
+
* @returns {() => void} Stops it.
|
|
163
|
+
*/
|
|
164
|
+
export function silkRibbon(el, config) {
|
|
165
|
+
let pal = paletteUniform(byLightness(pickColors(config, COLORS)));
|
|
166
|
+
const start = performance.now();
|
|
167
|
+
const speed = config.speed ?? 1;
|
|
168
|
+
const side = SIDES[config.side ?? 'right'] ?? 1;
|
|
169
|
+
const lean = { x: 0, at: 0 };
|
|
170
|
+
|
|
171
|
+
const { stage, stop } = createStage(el, {
|
|
172
|
+
fragment: FRAG,
|
|
173
|
+
alpha: false,
|
|
174
|
+
setup({ gl, uniform }) {
|
|
175
|
+
gl.uniform3fv(uniform('uGround'), parseColor(config.background ?? '#f6f4f1'));
|
|
176
|
+
gl.uniform1f(uniform('uSide'), side);
|
|
177
|
+
gl.uniform1f(uniform('uTwist'), config.twist ?? 1);
|
|
178
|
+
gl.uniform1f(uniform('uFolds'), config.folds ?? 1);
|
|
179
|
+
gl.uniform1f(uniform('uGlow'), config.glow ?? 1);
|
|
180
|
+
},
|
|
181
|
+
uniforms({ gl, uniform, still }, now) {
|
|
182
|
+
const t = ((now - start) / 1000) * speed;
|
|
183
|
+
const p = !still && canHover() ? pointerIn(el) : null;
|
|
184
|
+
// The folds lean towards the pointer, easing back when it leaves.
|
|
185
|
+
const goal = p?.inside ? { x: p.x - 0.5, at: p.y - 0.5 } : { x: 0, at: lean.at };
|
|
186
|
+
lean.x += (goal.x - lean.x) * 0.03;
|
|
187
|
+
lean.at += (goal.at - lean.at) * 0.03;
|
|
188
|
+
// Eight seconds in, where the first (and the still) frame shows a twist.
|
|
189
|
+
gl.uniform1f(uniform('uTime'), t + 8);
|
|
190
|
+
// The shader mirrors a left-hand ribbon, so the lean is mirrored with it.
|
|
191
|
+
gl.uniform1f(uniform('uLean'), side < 0 ? -lean.x : lean.x);
|
|
192
|
+
gl.uniform1f(uniform('uLeanAt'), lean.at);
|
|
193
|
+
gl.uniform3fv(uniform('uPal'), pal);
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
if (!stage) return stop;
|
|
197
|
+
|
|
198
|
+
const unwatch = watchColors(el, (next) => {
|
|
199
|
+
pal = paletteUniform(byLightness(next.colors));
|
|
200
|
+
if (stage.still) stage.redraw();
|
|
201
|
+
});
|
|
202
|
+
return () => {
|
|
203
|
+
stop();
|
|
204
|
+
unwatch();
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** A slide number brought into range, wrapping round at either end. */
|
|
2
|
+
export function wrap(i: number, count: number): number;
|
|
3
|
+
|
|
4
|
+
/** Which way a finished drag goes: 1 for the next slide (a swipe left), -1 for the previous, 0 for no swipe. */
|
|
5
|
+
export function swipeStep(dx: number, dy: number, ms: number): -1 | 0 | 1;
|
|
6
|
+
|
|
7
|
+
export interface SlideshowOptions {
|
|
8
|
+
/** How many slides there are. */
|
|
9
|
+
count: number;
|
|
10
|
+
/** Seconds each slide stays before the next, when playing. Default 5. */
|
|
11
|
+
interval?: number;
|
|
12
|
+
/** Move on by itself. Default true. */
|
|
13
|
+
autoplay?: boolean;
|
|
14
|
+
/** What to read out for a slide (its alt). */
|
|
15
|
+
describe?(index: number): string;
|
|
16
|
+
/** Draws the change; the slideshow waits for it, keeping the latest asked for meanwhile. */
|
|
17
|
+
show(to: number, from: number, dir: 1 | -1): Promise<void> | void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Runs a slideshow on an element: moving on every `interval` (held while
|
|
22
|
+
* hovered, focused, pressed, off screen or in a hidden tab), the arrow keys
|
|
23
|
+
* and a swipe, `[data-slide-dot]` buttons (with `aria-current` and a
|
|
24
|
+
* `--slide-progress` while playing), and `[data-slide-live]` reading out the slide
|
|
25
|
+
* a person moved to.
|
|
26
|
+
*/
|
|
27
|
+
export function slideshow(el: HTMLElement, options: SlideshowOptions): { go(to: number, dir?: 1 | -1, user?: boolean): void; stop(): void };
|