@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,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/image-ripple
|
|
3
|
+
*
|
|
4
|
+
* A photo under a skin of water. The pointer drags a wake through it and a
|
|
5
|
+
* click drops a ring; the waves bend the photo as they pass, part its
|
|
6
|
+
* colours a little at their fronts, and catch the light on their crests.
|
|
7
|
+
* Then they die away, and the photo is left exactly as it was: still and
|
|
8
|
+
* sharp.
|
|
9
|
+
*
|
|
10
|
+
* - The water is a height field on the GPU (height-field.js): a damped
|
|
11
|
+
* wave equation over a grid of square cells, soaked up at the edges.
|
|
12
|
+
* - The photo is drawn through it (image.js), fitted like `object-fit`:
|
|
13
|
+
* each colour bent by the water's slope by its own amount, with a glint
|
|
14
|
+
* where a slope faces the light.
|
|
15
|
+
*
|
|
16
|
+
* Nothing runs while the water is calm: the photo is drawn once, and the
|
|
17
|
+
* page's loop is joined only from the first stir to the moment the last
|
|
18
|
+
* ripple has faded. With prefers-reduced-motion, without `interactive`,
|
|
19
|
+
* without WebGL or float render targets, or when the photo cannot be read,
|
|
20
|
+
* it removes its canvas and the plain photo under it shows.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { createStage } from './stage.js';
|
|
24
|
+
import { animate } from './loop.js';
|
|
25
|
+
import { isMobileGpu, prefersReducedMotion } from './device.js';
|
|
26
|
+
import { pointerIn, trail, pressAt } from './pointer.js';
|
|
27
|
+
import { loadImage, imageTexture, coverScale, GLSL_COVER } from './image.js';
|
|
28
|
+
import { createHeightField, GLSL_HEIGHT_FIELD, MAX_PUSHES } from './height-field.js';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {object} ImageRippleConfig
|
|
32
|
+
* @property {string} src The photo's URL (same origin, or served with CORS).
|
|
33
|
+
* @property {number} [strength=1] How far the ripples bend the photo, 0–3.
|
|
34
|
+
* @property {number} [size=1] How wide the ripples the pointer makes are, 0.3–3.
|
|
35
|
+
* @property {number} [viscosity=1] How thick the water is: more settles sooner, with smaller ripples, 0.25–4.
|
|
36
|
+
* @property {number} [fringe=1] How far the colours part at the wave fronts, 0–3; 0 for none.
|
|
37
|
+
* @property {boolean} [interactive=true] Whether the pointer ripples it; false leaves the plain photo.
|
|
38
|
+
* @property {'cover' | 'contain'} [fit='cover'] How the photo fills the element, as CSS `object-fit`.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/** Simulation cells along the longer side (fewer on phones). */
|
|
42
|
+
const CELLS = 256;
|
|
43
|
+
const CELLS_MOBILE = 160;
|
|
44
|
+
/** Simulation steps a second, and the most in one frame. */
|
|
45
|
+
const RATE = 150;
|
|
46
|
+
const MAX_STEPS = 4;
|
|
47
|
+
/** The pointer's reach, element heights. */
|
|
48
|
+
const REACH = 0.022;
|
|
49
|
+
/** A stroke's push, per element height travelled. */
|
|
50
|
+
const STROKE = 30;
|
|
51
|
+
/** A dropped ring: how hard it pushes (a second), how fast it bobs and dies (s), and how long it lasts (s). */
|
|
52
|
+
const DROP = 36;
|
|
53
|
+
const DROP_PERIOD = 0.1;
|
|
54
|
+
const DROP_DECAY = 0.07;
|
|
55
|
+
const DROP_TIME = 0.32;
|
|
56
|
+
/** The most rings dropping at once. */
|
|
57
|
+
const MAX_DROPS = 6;
|
|
58
|
+
/** How far the slope bends the photo, element heights per unit of slope. */
|
|
59
|
+
const BEND = 0.0013;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The step: the wave equation, pushes, and a band round the edge that soaks
|
|
63
|
+
* the waves up, so they run on as if the water went past the frame.
|
|
64
|
+
*/
|
|
65
|
+
const STEP = `
|
|
66
|
+
precision highp float;
|
|
67
|
+
varying vec2 vUv;
|
|
68
|
+
${GLSL_HEIGHT_FIELD}
|
|
69
|
+
void main() {
|
|
70
|
+
vec4 s = waveState(vUv);
|
|
71
|
+
float v = s.y + waveForce(s) - wavePushes(vUv);
|
|
72
|
+
float edge = min(min(vUv.x, 1.0 - vUv.x) * uAspect, min(vUv.y, 1.0 - vUv.y));
|
|
73
|
+
v *= mix(0.86, uMotion.z, smoothstep(0.0, 0.06, edge));
|
|
74
|
+
gl_FragColor = wavePack(s.x + v, v);
|
|
75
|
+
}
|
|
76
|
+
`;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The render: the photo, through the water while there are waves. Red,
|
|
80
|
+
* green and blue are bent by the slope by slightly different amounts, so
|
|
81
|
+
* the colours part only where the water tilts (the wave fronts), and a soft
|
|
82
|
+
* glint and a little shade follow the slopes that face towards and away
|
|
83
|
+
* from a light at the top left. Flat water draws the photo untouched.
|
|
84
|
+
*/
|
|
85
|
+
const RENDER = `
|
|
86
|
+
precision highp float;
|
|
87
|
+
uniform vec2 uRes;
|
|
88
|
+
uniform sampler2D uPhoto;
|
|
89
|
+
uniform sampler2D uState;
|
|
90
|
+
uniform vec2 uStateTexel;
|
|
91
|
+
uniform vec2 uScale;
|
|
92
|
+
uniform float uAspect;
|
|
93
|
+
uniform float uReady;
|
|
94
|
+
uniform float uWaves;
|
|
95
|
+
uniform float uBend;
|
|
96
|
+
uniform float uFringe;
|
|
97
|
+
${GLSL_COVER}
|
|
98
|
+
|
|
99
|
+
// The photo at a point of the element (y up): transparent outside it, for 'contain'.
|
|
100
|
+
vec4 photo(vec2 uv) {
|
|
101
|
+
vec2 t = coverUv(uv, uScale);
|
|
102
|
+
t.y = 1.0 - t.y;
|
|
103
|
+
vec2 inside = step(vec2(0.0), t) * step(t, vec2(1.0));
|
|
104
|
+
return texture2D(uPhoto, t) * inside.x * inside.y;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void main() {
|
|
108
|
+
vec2 uv = gl_FragCoord.xy / uRes;
|
|
109
|
+
if (uReady < 0.5) { gl_FragColor = vec4(0.0); return; }
|
|
110
|
+
if (uWaves < 0.5) { gl_FragColor = photo(uv); return; }
|
|
111
|
+
vec2 s = uStateTexel;
|
|
112
|
+
// The slope, per element height.
|
|
113
|
+
vec2 g = vec2(
|
|
114
|
+
texture2D(uState, uv + vec2(s.x, 0.0)).r - texture2D(uState, uv - vec2(s.x, 0.0)).r,
|
|
115
|
+
texture2D(uState, uv + vec2(0.0, s.y)).r - texture2D(uState, uv - vec2(0.0, s.y)).r
|
|
116
|
+
) / (2.0 * s * vec2(uAspect, 1.0));
|
|
117
|
+
vec2 d = g * uBend / vec2(uAspect, 1.0);
|
|
118
|
+
float f = 0.12 * uFringe;
|
|
119
|
+
vec4 r = photo(uv - d * (1.0 - f));
|
|
120
|
+
vec4 c = photo(uv - d);
|
|
121
|
+
vec4 b = photo(uv - d * (1.0 + f));
|
|
122
|
+
vec3 col = vec3(r.r, c.g, b.b);
|
|
123
|
+
// Light from the top left: slopes facing it brighten, those turned away
|
|
124
|
+
// darken a touch, and the steepest catch a glint. Flat water adds nothing.
|
|
125
|
+
vec2 light = normalize(vec2(-0.6, 0.8));
|
|
126
|
+
float facing = dot(g, light) * uBend * 60.0;
|
|
127
|
+
float glint = pow(clamp(facing, 0.0, 1.0), 3.0);
|
|
128
|
+
col *= 1.0 + clamp(facing, -0.5, 0.5) * 0.25;
|
|
129
|
+
col += vec3(1.0, 0.98, 0.94) * glint * 0.22 * c.a;
|
|
130
|
+
gl_FragColor = vec4(min(col, vec3(1.0)), c.a);
|
|
131
|
+
}
|
|
132
|
+
`;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The texture-coordinate scale that fits a picture to a box the way CSS
|
|
136
|
+
* `object-fit` does, for `coverUv`: `cover` crops it to fill the box,
|
|
137
|
+
* `contain` shows all of it, with room to spare.
|
|
138
|
+
*
|
|
139
|
+
* @param {'cover' | 'contain' | string | undefined} fit How it fits; anything else is `cover`.
|
|
140
|
+
* @param {number} imageAspect The picture's width over its height.
|
|
141
|
+
* @param {number} boxAspect The box's width over its height.
|
|
142
|
+
* @returns {[number, number]} The scale for x and y.
|
|
143
|
+
*/
|
|
144
|
+
export function fitScale(fit, imageAspect, boxAspect) {
|
|
145
|
+
if (fit !== 'contain') return coverScale(imageAspect, boxAspect);
|
|
146
|
+
return boxAspect > imageAspect ? [boxAspect / imageAspect, 1] : [1, imageAspect / boxAspect];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* How hard a dropped ring pushes the water at an age: the surface bobs
|
|
151
|
+
* down and up a few times, each smaller than the last, so the drop sends
|
|
152
|
+
* out a train of rings rather than one swell.
|
|
153
|
+
*
|
|
154
|
+
* @param {number} age Seconds since the drop.
|
|
155
|
+
* @returns {number} The push, a second (negative lifts); 0 once it is spent.
|
|
156
|
+
*/
|
|
157
|
+
export function dropForce(age) {
|
|
158
|
+
if (age < 0 || age >= DROP_TIME) return 0;
|
|
159
|
+
return DROP * Math.sin((2 * Math.PI * age) / DROP_PERIOD) * Math.exp(-age / DROP_DECAY);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* How the water moves for a viscosity, and how long its waves take to fade
|
|
164
|
+
* from a strong ring to far below a pixel's worth of bend.
|
|
165
|
+
*
|
|
166
|
+
* @param {number} viscosity How thick the water is (1 is the default).
|
|
167
|
+
* @returns {{ motion: import('./height-field.js').WaveMotion, settle: number }} The motion per step, and the time to settle, ms.
|
|
168
|
+
*/
|
|
169
|
+
export function waterMotion(viscosity) {
|
|
170
|
+
const thick = Math.max(0.1, viscosity);
|
|
171
|
+
const keep = 1 - 0.006 * thick;
|
|
172
|
+
return {
|
|
173
|
+
motion: { speed: 0.3, spring: 0, keep, viscosity: Math.min(0.1, 0.02 * thick) },
|
|
174
|
+
settle: Math.min(9000, (Math.log(0.002) / (RATE * Math.log(keep))) * 1000),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Starts the ripples on an element: its first canvas is drawn into, over
|
|
180
|
+
* the photo the component puts under it.
|
|
181
|
+
*
|
|
182
|
+
* @param {HTMLElement} el The element.
|
|
183
|
+
* @param {ImageRippleConfig} config The settings.
|
|
184
|
+
* @returns {() => void} Stops it.
|
|
185
|
+
*/
|
|
186
|
+
export function imageRipple(el, config) {
|
|
187
|
+
if (!config.src || config.interactive === false || prefersReducedMotion()) {
|
|
188
|
+
el.querySelector('canvas')?.remove();
|
|
189
|
+
return () => {};
|
|
190
|
+
}
|
|
191
|
+
const { motion, settle } = waterMotion(config.viscosity ?? 1);
|
|
192
|
+
const strength = config.strength ?? 1;
|
|
193
|
+
const reach = REACH * Math.max(0.2, config.size ?? 1);
|
|
194
|
+
const pushes = new Float32Array(MAX_PUSHES * 4);
|
|
195
|
+
let pushCount = 0;
|
|
196
|
+
/** Rings dropping: where (0–1, y up) and how long since. */
|
|
197
|
+
let drops = /** @type {{ x: number, y: number, age: number }[]} */ ([]);
|
|
198
|
+
/** @type {import('./height-field.js').HeightField | null} */
|
|
199
|
+
let water = null;
|
|
200
|
+
/** @type {WebGLTexture | null} */
|
|
201
|
+
let photo = null;
|
|
202
|
+
let imageAspect = 1;
|
|
203
|
+
let aspect = 1;
|
|
204
|
+
let ready = false;
|
|
205
|
+
/** Unregisters the frame callback; set while the water moves. */
|
|
206
|
+
let stopLoop = /** @type {(() => void) | null} */ (null);
|
|
207
|
+
let lastStir = 0;
|
|
208
|
+
let backlog = 0;
|
|
209
|
+
const path = trail(el, { step: 0.006 });
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Queues a push for the next step.
|
|
213
|
+
*
|
|
214
|
+
* @param {number} x Across, 0–1.
|
|
215
|
+
* @param {number} y Up, 0–1.
|
|
216
|
+
* @param {number} radius Its reach, element heights.
|
|
217
|
+
* @param {number} force How hard.
|
|
218
|
+
*/
|
|
219
|
+
const push = (x, y, radius, force) => {
|
|
220
|
+
if (pushCount >= MAX_PUSHES) return;
|
|
221
|
+
pushes.set([x * aspect, y, radius, force], pushCount * 4);
|
|
222
|
+
pushCount++;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* One frame while the water moves: the pointer's wake, the steps due,
|
|
227
|
+
* a draw, and sleep once the last ripple has faded.
|
|
228
|
+
*
|
|
229
|
+
* @param {number} now The time, ms.
|
|
230
|
+
* @param {number} dt Seconds since the last frame.
|
|
231
|
+
*/
|
|
232
|
+
const frame = (now, dt) => {
|
|
233
|
+
if (!water || !stage) return;
|
|
234
|
+
const time = dt || 1 / 60;
|
|
235
|
+
const points = path.read(time);
|
|
236
|
+
for (const p of points) {
|
|
237
|
+
if (p.x < 0 || p.y < 0 || p.x > 1 || p.y > 1) continue;
|
|
238
|
+
const speed = Math.hypot(p.dx * aspect, p.dy);
|
|
239
|
+
push(p.x, p.y, reach, (STROKE * Math.min(speed, 3) * time) / points.length);
|
|
240
|
+
}
|
|
241
|
+
drops = drops.filter((d) => (d.age += time) < DROP_TIME);
|
|
242
|
+
for (const d of drops) push(d.x, d.y, reach, dropForce(d.age) * time);
|
|
243
|
+
if (pushCount) lastStir = now;
|
|
244
|
+
backlog += time;
|
|
245
|
+
let steps = 0;
|
|
246
|
+
while (backlog >= 1 / RATE && steps < MAX_STEPS) {
|
|
247
|
+
water.step(motion, pushes, steps === 0 ? pushCount : 0);
|
|
248
|
+
backlog -= 1 / RATE;
|
|
249
|
+
steps++;
|
|
250
|
+
}
|
|
251
|
+
if (steps === MAX_STEPS) backlog = 0;
|
|
252
|
+
if (steps) pushCount = 0;
|
|
253
|
+
if (now - lastStir > settle) {
|
|
254
|
+
// Calm: what is left is far below a pixel. Flatten it and stop.
|
|
255
|
+
water.clear();
|
|
256
|
+
stopLoop?.();
|
|
257
|
+
stopLoop = null;
|
|
258
|
+
}
|
|
259
|
+
stage.redraw();
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/** Joins the page's loop, if the water was asleep. */
|
|
263
|
+
const wake = () => {
|
|
264
|
+
lastStir = performance.now();
|
|
265
|
+
if (stopLoop || !ready) return;
|
|
266
|
+
backlog = 0;
|
|
267
|
+
path.reset();
|
|
268
|
+
stopLoop = animate(el, frame);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/** A move over the photo wakes the water; the frames then follow the stroke. */
|
|
272
|
+
const move = () => {
|
|
273
|
+
if (!stopLoop && pointerIn(el).inside) wake();
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* A press drops a ring where it lands.
|
|
278
|
+
*
|
|
279
|
+
* @param {PointerEvent} e The press.
|
|
280
|
+
*/
|
|
281
|
+
const down = (e) => {
|
|
282
|
+
const at = pressAt(el, e);
|
|
283
|
+
if (!at || !ready) return;
|
|
284
|
+
if (drops.length < MAX_DROPS) drops.push({ x: at.x, y: at.y, age: 0 });
|
|
285
|
+
wake();
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const { stage, stop } = createStage(el, {
|
|
289
|
+
fragment: RENDER,
|
|
290
|
+
alpha: true,
|
|
291
|
+
// Drawn on demand: once at rest, and every frame only while the water moves.
|
|
292
|
+
still: true,
|
|
293
|
+
setup({ gl, uniform, program }) {
|
|
294
|
+
water = createHeightField(gl, STEP);
|
|
295
|
+
if (!water) return false;
|
|
296
|
+
gl.useProgram(program);
|
|
297
|
+
gl.uniform1i(uniform('uPhoto'), 1);
|
|
298
|
+
gl.uniform1i(uniform('uState'), 2);
|
|
299
|
+
gl.uniform1f(uniform('uBend'), BEND * strength);
|
|
300
|
+
gl.uniform1f(uniform('uFringe'), config.fringe ?? 1);
|
|
301
|
+
document.addEventListener('pointermove', move, { passive: true });
|
|
302
|
+
document.addEventListener('pointerdown', down, { passive: true });
|
|
303
|
+
return () => {
|
|
304
|
+
document.removeEventListener('pointermove', move);
|
|
305
|
+
document.removeEventListener('pointerdown', down);
|
|
306
|
+
stopLoop?.();
|
|
307
|
+
stopLoop = null;
|
|
308
|
+
water?.dispose();
|
|
309
|
+
water = null;
|
|
310
|
+
gl.deleteTexture(photo);
|
|
311
|
+
};
|
|
312
|
+
},
|
|
313
|
+
resize({ gl, uniform, canvas }) {
|
|
314
|
+
aspect = canvas.width / canvas.height;
|
|
315
|
+
gl.uniform1f(uniform('uAspect'), aspect);
|
|
316
|
+
gl.uniform2fv(uniform('uScale'), fitScale(config.fit, imageAspect, aspect));
|
|
317
|
+
// Last: it binds its own program.
|
|
318
|
+
water?.resize(isMobileGpu() ? CELLS_MOBILE : CELLS, aspect);
|
|
319
|
+
},
|
|
320
|
+
uniforms({ gl, uniform }) {
|
|
321
|
+
const waves = Boolean(water && stopLoop);
|
|
322
|
+
gl.uniform1f(uniform('uReady'), ready ? 1 : 0);
|
|
323
|
+
gl.uniform1f(uniform('uWaves'), waves ? 1 : 0);
|
|
324
|
+
if (water && waves) {
|
|
325
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
326
|
+
gl.bindTexture(gl.TEXTURE_2D, water.texture());
|
|
327
|
+
gl.uniform2fv(uniform('uStateTexel'), water.texel());
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
if (!stage) return stop;
|
|
332
|
+
|
|
333
|
+
loadImage(config.src)
|
|
334
|
+
.then((image) => {
|
|
335
|
+
if (!water) return;
|
|
336
|
+
const { gl, uniform, program } = stage;
|
|
337
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
338
|
+
photo = imageTexture(gl, image, { mipmap: true });
|
|
339
|
+
imageAspect = image.naturalWidth / image.naturalHeight;
|
|
340
|
+
gl.useProgram(program);
|
|
341
|
+
gl.uniform2fv(uniform('uScale'), fitScale(config.fit, imageAspect, aspect));
|
|
342
|
+
ready = true;
|
|
343
|
+
stage.redraw();
|
|
344
|
+
})
|
|
345
|
+
.catch((error) => {
|
|
346
|
+
// The plain photo stays.
|
|
347
|
+
console.warn('[effects-astro]', error);
|
|
348
|
+
stop();
|
|
349
|
+
stage.canvas.remove();
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
return stop;
|
|
353
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Loads and decodes an image with CORS, for a WebGL texture. Rejects when it cannot be loaded. */
|
|
2
|
+
export function loadImage(src: string): Promise<HTMLImageElement>;
|
|
3
|
+
|
|
4
|
+
/** Uploads a picture as a clamped, smoothly filtered texture on the active unit; `mipmap` where the GPU allows. */
|
|
5
|
+
export function imageTexture(gl: WebGLRenderingContext | WebGL2RenderingContext, source: TexImageSource, options?: { mipmap?: boolean }): WebGLTexture | null;
|
|
6
|
+
|
|
7
|
+
/** The texture-coordinate scale that makes a picture cover a box (CSS `object-fit: cover`), for `coverUv`. */
|
|
8
|
+
export function coverScale(imageAspect: number, boxAspect: number): [number, number];
|
|
9
|
+
|
|
10
|
+
/** GLSL: `coverUv(uv, scale)`, a box coordinate to a picture's, cropped to cover. */
|
|
11
|
+
export const GLSL_COVER: string;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/image
|
|
3
|
+
*
|
|
4
|
+
* Pictures for the effects: loading one, putting it on the GPU, and
|
|
5
|
+
* fitting it to a canvas the way CSS `object-fit: cover` does.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Loads and decodes an image, asking for CORS so a WebGL texture may read
|
|
10
|
+
* it (same origin, or served with Access-Control-Allow-Origin).
|
|
11
|
+
*
|
|
12
|
+
* @param {string} src Its URL.
|
|
13
|
+
* @returns {Promise<HTMLImageElement>} The image, decoded.
|
|
14
|
+
* @throws {Error} When it cannot be loaded or decoded.
|
|
15
|
+
*/
|
|
16
|
+
export function loadImage(src) {
|
|
17
|
+
const img = new Image();
|
|
18
|
+
img.crossOrigin = 'anonymous';
|
|
19
|
+
img.src = src;
|
|
20
|
+
return img.decode().then(
|
|
21
|
+
() => img,
|
|
22
|
+
() => {
|
|
23
|
+
throw new Error(`Could not load ${src}`);
|
|
24
|
+
},
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Uploads a picture as a texture on the active texture unit: clamped at
|
|
30
|
+
* its edges, smoothly filtered, and mipmapped when asked (WebGL 1 needs
|
|
31
|
+
* power-of-two sizes for that, so it is skipped there for any other size).
|
|
32
|
+
*
|
|
33
|
+
* @param {WebGLRenderingContext | WebGL2RenderingContext} gl The context.
|
|
34
|
+
* @param {TexImageSource} source The picture (an image, canvas or video).
|
|
35
|
+
* @param {{ mipmap?: boolean }} [options] `mipmap` for a picture drawn much smaller than it is.
|
|
36
|
+
* @returns {WebGLTexture | null} The texture, bound.
|
|
37
|
+
*/
|
|
38
|
+
export function imageTexture(gl, source, { mipmap = false } = {}) {
|
|
39
|
+
const texture = gl.createTexture();
|
|
40
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
41
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
42
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
43
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
44
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
45
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
46
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
|
|
47
|
+
const { width, height } = /** @type {{ width: number, height: number }} */ (source);
|
|
48
|
+
/**
|
|
49
|
+
* Whether a size is a power of two.
|
|
50
|
+
*
|
|
51
|
+
* @param {number} n The size.
|
|
52
|
+
* @returns {boolean} True for 1, 2, 4, 8…
|
|
53
|
+
*/
|
|
54
|
+
const pow2 = (n) => (n & (n - 1)) === 0;
|
|
55
|
+
const webgl2 = typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext;
|
|
56
|
+
if (mipmap && (webgl2 || (pow2(width) && pow2(height)))) {
|
|
57
|
+
gl.generateMipmap(gl.TEXTURE_2D);
|
|
58
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
|
|
59
|
+
}
|
|
60
|
+
return texture;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* How to scale texture coordinates so a picture covers a box without
|
|
65
|
+
* stretching, cropping the overflow evenly (CSS `object-fit: cover`).
|
|
66
|
+
* Pass it to `coverUv` in GLSL_COVER.
|
|
67
|
+
*
|
|
68
|
+
* @param {number} imageAspect The picture's width over its height.
|
|
69
|
+
* @param {number} boxAspect The box's width over its height.
|
|
70
|
+
* @returns {[number, number]} The scale for x and y, each at most 1.
|
|
71
|
+
*/
|
|
72
|
+
export function coverScale(imageAspect, boxAspect) {
|
|
73
|
+
return boxAspect > imageAspect ? [1, imageAspect / boxAspect] : [boxAspect / imageAspect, 1];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** GLSL: `coverUv(uv, scale)`, a 0–1 box coordinate to a picture's, cropped to cover (scale from coverScale). */
|
|
77
|
+
export const GLSL_COVER = `
|
|
78
|
+
vec2 coverUv(vec2 uv, vec2 scale) {
|
|
79
|
+
return (uv - 0.5) * scale + 0.5;
|
|
80
|
+
}
|
|
81
|
+
`;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calls `show` when `el` comes into view (at once without
|
|
3
|
+
* IntersectionObserver); unless `once`, `hide` when it leaves. Returns a
|
|
4
|
+
* function that stops watching.
|
|
5
|
+
*/
|
|
6
|
+
export function inView(el: Element, show: () => void, once?: boolean, hide?: () => void): () => void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/in-view
|
|
3
|
+
*
|
|
4
|
+
* Calling back as an element scrolls into view, for effects that play on
|
|
5
|
+
* arrival rather than run all the time.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Calls back when an element first comes into view (or at once, where
|
|
10
|
+
* IntersectionObserver is missing).
|
|
11
|
+
*
|
|
12
|
+
* @param {Element} el The element.
|
|
13
|
+
* @param {() => void} show Called when it is in view.
|
|
14
|
+
* @param {boolean} [once=true] Stop watching after the first time.
|
|
15
|
+
* @param {() => void} [hide] Called when it leaves view (when not `once`).
|
|
16
|
+
* @returns {() => void} Stops watching.
|
|
17
|
+
*/
|
|
18
|
+
export function inView(el, show, once = true, hide) {
|
|
19
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
20
|
+
show();
|
|
21
|
+
return () => {};
|
|
22
|
+
}
|
|
23
|
+
const observer = new IntersectionObserver(
|
|
24
|
+
([entry]) => {
|
|
25
|
+
if (entry.isIntersecting) {
|
|
26
|
+
show();
|
|
27
|
+
if (once) observer.disconnect();
|
|
28
|
+
} else {
|
|
29
|
+
hide?.();
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
{ rootMargin: '0px 0px -10% 0px' },
|
|
33
|
+
);
|
|
34
|
+
observer.observe(el);
|
|
35
|
+
return () => observer.disconnect();
|
|
36
|
+
}
|