@arraypress/effects-astro 0.0.0-stage → 0.1.0
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 +285 -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,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/rain-on-glass/backdrop
|
|
3
|
+
*
|
|
4
|
+
* The photo as it looks through fogged glass, made once at load: a small
|
|
5
|
+
* blurred copy, with the photo's brightest lights opened out into bokeh
|
|
6
|
+
* discs the way a lens out of focus draws them.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { boxBlur } from '../blur.js';
|
|
10
|
+
import { parseColor } from '../color.js';
|
|
11
|
+
|
|
12
|
+
/** Pixels across the copy the lights are found in, the copy that is blurred, and the copy the discs are drawn on. */
|
|
13
|
+
const FIND = 256;
|
|
14
|
+
const BLUR = 128;
|
|
15
|
+
const LARGE = 640;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {object} Light
|
|
19
|
+
* @property {number} x Across the picture, 0–1 from the left.
|
|
20
|
+
* @property {number} y Down the picture, 0–1 from the top.
|
|
21
|
+
* @property {number} level How bright, 0–1.
|
|
22
|
+
* @property {[number, number, number]} rgb Its colour around it, each 0–1, brightest channel 1.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Finds the brightest points in a picture: local peaks over `threshold`,
|
|
27
|
+
* brightest first, none closer than `spacing` to a brighter one.
|
|
28
|
+
*
|
|
29
|
+
* @param {Uint8ClampedArray} data The pixels, RGBA, row by row.
|
|
30
|
+
* @param {number} width Pixels across.
|
|
31
|
+
* @param {number} height Pixels down.
|
|
32
|
+
* @param {{ count?: number, threshold?: number, spacing?: number }} [options] At most `count` (default 22),
|
|
33
|
+
* at least `threshold` bright (0–1, default 0.6), at least `spacing` pixels apart (default 10).
|
|
34
|
+
* @returns {Light[]} The lights.
|
|
35
|
+
*/
|
|
36
|
+
export function findLights(data, width, height, { count = 22, threshold = 0.6, spacing = 10 } = {}) {
|
|
37
|
+
const level = new Float32Array(width * height);
|
|
38
|
+
for (let i = 0; i < level.length; i++) {
|
|
39
|
+
level[i] = (data[i * 4] * 0.2126 + data[i * 4 + 1] * 0.7152 + data[i * 4 + 2] * 0.0722) / 255;
|
|
40
|
+
}
|
|
41
|
+
/** @type {number[]} */
|
|
42
|
+
const peaks = [];
|
|
43
|
+
for (let y = 1; y < height - 1; y++) {
|
|
44
|
+
for (let x = 1; x < width - 1; x++) {
|
|
45
|
+
const i = y * width + x;
|
|
46
|
+
const v = level[i];
|
|
47
|
+
if (v < threshold) continue;
|
|
48
|
+
let top = true;
|
|
49
|
+
for (let dy = -1; dy <= 1 && top; dy++) for (let dx = -1; dx <= 1; dx++) if (level[i + dy * width + dx] > v) top = false;
|
|
50
|
+
if (top) peaks.push(i);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
peaks.sort((a, b) => level[b] - level[a]);
|
|
54
|
+
/** @type {Light[]} */
|
|
55
|
+
const lights = [];
|
|
56
|
+
for (const i of peaks) {
|
|
57
|
+
if (lights.length >= count) break;
|
|
58
|
+
const x = i % width;
|
|
59
|
+
const y = (i - x) / width;
|
|
60
|
+
if (lights.some((l) => Math.hypot(l.x * width - x, l.y * height - y) < spacing)) continue;
|
|
61
|
+
lights.push({ x: x / width, y: y / height, level: level[i], rgb: around(data, width, height, x, y) });
|
|
62
|
+
}
|
|
63
|
+
return lights;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The colour around a pixel, brightened until its strongest channel is
|
|
68
|
+
* full and pushed away from grey: a light's own hue, which its blown-out
|
|
69
|
+
* centre often loses.
|
|
70
|
+
*
|
|
71
|
+
* @param {Uint8ClampedArray} data The pixels, RGBA, row by row.
|
|
72
|
+
* @param {number} width Pixels across.
|
|
73
|
+
* @param {number} height Pixels down.
|
|
74
|
+
* @param {number} x The pixel's column.
|
|
75
|
+
* @param {number} y The pixel's row.
|
|
76
|
+
* @returns {[number, number, number]} The colour, each channel 0–1.
|
|
77
|
+
*/
|
|
78
|
+
function around(data, width, height, x, y) {
|
|
79
|
+
const sum = [0, 0, 0];
|
|
80
|
+
for (let dy = -3; dy <= 3; dy++) {
|
|
81
|
+
for (let dx = -3; dx <= 3; dx++) {
|
|
82
|
+
const px = Math.min(width - 1, Math.max(0, x + dx));
|
|
83
|
+
const py = Math.min(height - 1, Math.max(0, y + dy));
|
|
84
|
+
for (let c = 0; c < 3; c++) sum[c] += data[(py * width + px) * 4 + c];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const top = Math.max(1, ...sum);
|
|
88
|
+
const rgb = sum.map((c) => c / top);
|
|
89
|
+
const mean = (rgb[0] + rgb[1] + rgb[2]) / 3;
|
|
90
|
+
const [r, g, b] = rgb.map((c) => Math.min(1, Math.max(0, mean + (c - mean) * 1.8)));
|
|
91
|
+
return [r, g, b];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A small, blurred copy of a photo with bokeh discs over its lights.
|
|
96
|
+
*
|
|
97
|
+
* @param {CanvasImageSource & { width: number, height: number }} img The photo.
|
|
98
|
+
* @param {{ blur: number, bokeh: number, colors: string[] }} options `blur` and `bokeh` 0–1;
|
|
99
|
+
* `colors` for the discs (empty: each light's own colour).
|
|
100
|
+
* @returns {HTMLCanvasElement} The copy, `LARGE` pixels across.
|
|
101
|
+
* @throws {Error} When the browser gives no 2D canvas.
|
|
102
|
+
*/
|
|
103
|
+
export function backdrop(img, { blur, bokeh, colors }) {
|
|
104
|
+
const aspect = img.width / img.height;
|
|
105
|
+
// Scaled down once from the photo; the blurred copy is halved from that.
|
|
106
|
+
const find = canvas(FIND, Math.max(1, Math.round(FIND / aspect)));
|
|
107
|
+
const fc = context(find);
|
|
108
|
+
fc.drawImage(img, 0, 0, find.width, find.height);
|
|
109
|
+
const lights = bokeh > 0 ? findLights(fc.getImageData(0, 0, find.width, find.height).data, find.width, find.height) : [];
|
|
110
|
+
const small = canvas(BLUR, Math.max(1, Math.round(BLUR / aspect)));
|
|
111
|
+
const sc = context(small);
|
|
112
|
+
sc.drawImage(find, 0, 0, small.width, small.height);
|
|
113
|
+
const pixels = sc.getImageData(0, 0, small.width, small.height);
|
|
114
|
+
boxBlur(pixels.data, small.width, small.height, 0.5 + blur * 3.5);
|
|
115
|
+
sc.putImageData(pixels, 0, 0);
|
|
116
|
+
|
|
117
|
+
const out = canvas(LARGE, Math.max(1, Math.round(LARGE / aspect)));
|
|
118
|
+
const ctx = context(out);
|
|
119
|
+
ctx.imageSmoothingQuality = 'high';
|
|
120
|
+
ctx.drawImage(small, 0, 0, out.width, out.height);
|
|
121
|
+
ctx.globalCompositeOperation = 'screen';
|
|
122
|
+
const tints = colors.map(parseColor);
|
|
123
|
+
const size = out.width * (0.008 + blur * 0.022);
|
|
124
|
+
lights.forEach((light, i) => {
|
|
125
|
+
// A steady spread of sizes and strengths, the same on every load.
|
|
126
|
+
const jitter = fract(Math.sin(i * 12.9898 + 4.1) * 43758.5453);
|
|
127
|
+
const r = size * (0.6 + jitter * 0.9);
|
|
128
|
+
const [cr, cg, cb] = (tints.length ? tints[i % tints.length] : light.rgb).map((c) => Math.round(c * 255));
|
|
129
|
+
const a = bokeh * (0.5 + 0.6 * light.level) * (1.1 - jitter * 0.4);
|
|
130
|
+
/**
|
|
131
|
+
* The disc's colour at a share of its strength.
|
|
132
|
+
*
|
|
133
|
+
* @param {number} k The share, 0–1.
|
|
134
|
+
* @returns {string} A CSS colour.
|
|
135
|
+
*/
|
|
136
|
+
const tone = (k) => `rgb(${cr} ${cg} ${cb} / ${Math.min(1, a * k)})`;
|
|
137
|
+
const x = light.x * out.width;
|
|
138
|
+
const y = light.y * out.height;
|
|
139
|
+
const disc = ctx.createRadialGradient(x, y, 0, x, y, r);
|
|
140
|
+
disc.addColorStop(0, tone(0.4));
|
|
141
|
+
disc.addColorStop(0.75, tone(0.55));
|
|
142
|
+
disc.addColorStop(0.92, tone(0.95));
|
|
143
|
+
disc.addColorStop(1, tone(0));
|
|
144
|
+
ctx.fillStyle = disc;
|
|
145
|
+
ctx.beginPath();
|
|
146
|
+
ctx.arc(x, y, r, 0, Math.PI * 2);
|
|
147
|
+
ctx.fill();
|
|
148
|
+
});
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* A detached canvas.
|
|
154
|
+
*
|
|
155
|
+
* @param {number} width Pixels across.
|
|
156
|
+
* @param {number} height Pixels down.
|
|
157
|
+
* @returns {HTMLCanvasElement} The canvas.
|
|
158
|
+
*/
|
|
159
|
+
function canvas(width, height) {
|
|
160
|
+
const c = document.createElement('canvas');
|
|
161
|
+
c.width = width;
|
|
162
|
+
c.height = height;
|
|
163
|
+
return c;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* A canvas's 2D context, read back often.
|
|
168
|
+
*
|
|
169
|
+
* @param {HTMLCanvasElement} c The canvas.
|
|
170
|
+
* @returns {CanvasRenderingContext2D} Its context.
|
|
171
|
+
* @throws {Error} When the browser gives none.
|
|
172
|
+
*/
|
|
173
|
+
function context(c) {
|
|
174
|
+
const ctx = c.getContext('2d', { willReadFrequently: true });
|
|
175
|
+
if (!ctx) throw new Error('No 2D canvas');
|
|
176
|
+
return ctx;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The fractional part of a number.
|
|
181
|
+
*
|
|
182
|
+
* @param {number} n The number.
|
|
183
|
+
* @returns {number} It, less its floor.
|
|
184
|
+
*/
|
|
185
|
+
const fract = (n) => n - Math.floor(n);
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/rain-on-glass/shader
|
|
3
|
+
*
|
|
4
|
+
* The rain on glass fragment shader. Drops are procedural, one per cell of
|
|
5
|
+
* a few grids: small beads that swell and dry where they sit, and larger
|
|
6
|
+
* runners that slide down tall cells along wandering paths, clearing a
|
|
7
|
+
* trail that beads up and fogs over behind them. Each drop is a little
|
|
8
|
+
* fisheye lens, showing the photo sharp and upside down; between them the
|
|
9
|
+
* photo is the soft, bokeh'd copy under a veil of condensation.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { GLSL_NOISE } from '../glsl.js';
|
|
13
|
+
import { GLSL_COVER } from '../image.js';
|
|
14
|
+
|
|
15
|
+
/** The fragment shader. */
|
|
16
|
+
export const FRAG = `
|
|
17
|
+
precision highp float;
|
|
18
|
+
uniform vec2 uRes;
|
|
19
|
+
uniform float uTime;
|
|
20
|
+
uniform float uUnit;
|
|
21
|
+
uniform vec2 uCover;
|
|
22
|
+
uniform sampler2D uPhoto;
|
|
23
|
+
uniform sampler2D uSoft;
|
|
24
|
+
uniform sampler2D uWipe;
|
|
25
|
+
uniform float uRain;
|
|
26
|
+
uniform float uFog;
|
|
27
|
+
uniform float uClearBias;
|
|
28
|
+
uniform vec3 uTint;
|
|
29
|
+
uniform float uShow;
|
|
30
|
+
${GLSL_NOISE}
|
|
31
|
+
${GLSL_COVER}
|
|
32
|
+
|
|
33
|
+
// The drop that covers this pixel most so far: how much, where in it the
|
|
34
|
+
// pixel sits (centre 0, rim 1), and its centre in glass units.
|
|
35
|
+
float lensCover;
|
|
36
|
+
vec2 lensAt;
|
|
37
|
+
vec2 lensCentre;
|
|
38
|
+
|
|
39
|
+
vec3 hash3(vec2 p) { return vec3(hash(p), hash(p + 19.19), hash(p + 47.31)); }
|
|
40
|
+
|
|
41
|
+
// A box coordinate (0–1, y up) to the photo's (cropped to cover, y down).
|
|
42
|
+
vec2 photoUv(vec2 uv) {
|
|
43
|
+
vec2 c = coverUv(uv, uCover);
|
|
44
|
+
return vec2(c.x, 1.0 - c.y);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Offers a drop to this pixel; it is kept when it covers the pixel more
|
|
48
|
+
// than the last. squash < 1 draws its top out into a tail.
|
|
49
|
+
void offer(vec2 q, vec2 centre, float radius, float squash) {
|
|
50
|
+
if (radius < 1e-4) return;
|
|
51
|
+
vec2 o = (q - centre) / radius;
|
|
52
|
+
o.y *= o.y > 0.0 ? squash : 1.0;
|
|
53
|
+
float cover = clamp((1.0 - length(o)) * radius * uUnit / 1.5, 0.0, 1.0);
|
|
54
|
+
if (cover > lensCover) {
|
|
55
|
+
lensCover = cover;
|
|
56
|
+
lensAt = o;
|
|
57
|
+
lensCentre = centre;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Beads: at most one in each cell, which swells in, sits and dries away on
|
|
62
|
+
// a clock of its own. keep shrinks them where the glass was just cleared.
|
|
63
|
+
void beads(vec2 q, float cell, float seed, float amount, float keep) {
|
|
64
|
+
vec2 id = floor(q / cell);
|
|
65
|
+
vec3 h = hash3(id + seed);
|
|
66
|
+
if (h.x > amount) return;
|
|
67
|
+
float life = fract(uTime * (0.015 + 0.03 * h.y) + h.z);
|
|
68
|
+
float grow = smoothstep(0.0, 0.2, life) * smoothstep(1.0, 0.85, life);
|
|
69
|
+
float radius = (0.08 + 0.2 * h.y * h.y) * cell * grow * keep;
|
|
70
|
+
vec2 centre = (id + 0.3 + 0.4 * hash3(id + seed + 7.0).xy) * cell;
|
|
71
|
+
offer(q, centre, radius, 0.9);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// A runner's x in its cell at height y (both 0–1): a lazy wander, the same
|
|
75
|
+
// for the drop and for the trail it leaves.
|
|
76
|
+
float path(float y, vec3 h) {
|
|
77
|
+
return 0.5 + 0.17 * sin(y * (4.0 + 5.0 * h.y) + h.z * 6.283) + 0.06 * sin(y * 17.0 + h.x * 40.0);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Runners: in each tall cell, maybe a drop that slides down in fits and
|
|
81
|
+
// starts, leaving a clear trail that beads up and fogs over again behind
|
|
82
|
+
// it. Returns how clear the trail leaves the glass here, 0–1.
|
|
83
|
+
float runners(vec2 q, vec2 cell, float seed, float amount) {
|
|
84
|
+
float column = floor(q.x / cell.x);
|
|
85
|
+
float lift = hash(vec2(column, seed)) * cell.y * 9.0;
|
|
86
|
+
vec2 s = q + vec2(0.0, lift);
|
|
87
|
+
vec2 id = floor(s / cell);
|
|
88
|
+
vec2 at = fract(s / cell);
|
|
89
|
+
vec3 h = hash3(id + seed);
|
|
90
|
+
if (h.x > amount) return 0.0;
|
|
91
|
+
|
|
92
|
+
float run = fract(uTime * (0.04 + 0.05 * h.y) + h.z);
|
|
93
|
+
float k = 6.283 * (2.0 + floor(h.z * 3.0));
|
|
94
|
+
float fall = run - 0.9 * sin(k * run) / k;
|
|
95
|
+
float y = mix(0.92, 0.08, fall);
|
|
96
|
+
float alive = smoothstep(0.0, 0.05, run) * smoothstep(1.0, 0.9, run);
|
|
97
|
+
float radius = (0.14 + 0.1 * h.y) * cell.x;
|
|
98
|
+
vec2 drop = (id + vec2(path(y, h), y)) * cell;
|
|
99
|
+
offer(q, drop - vec2(0.0, lift), radius * alive, 0.6);
|
|
100
|
+
|
|
101
|
+
// The trail: as wide as most of the drop, fogging over as it ages and
|
|
102
|
+
// gone by the top of the cell.
|
|
103
|
+
float above = (at.y - y) * cell.y;
|
|
104
|
+
float across = abs(at.x - path(at.y, h)) * cell.x;
|
|
105
|
+
float trail = smoothstep(radius * 1.05, radius * 0.35, across) * smoothstep(-0.2 * radius, 0.8 * radius, above) * exp(-max(above, 0.0) / 0.28) * smoothstep(1.0, 0.7, at.y) * alive;
|
|
106
|
+
|
|
107
|
+
// The beads it leaves behind, one in each short stretch of the trail.
|
|
108
|
+
float gap = radius * 1.6;
|
|
109
|
+
float row = floor(s.y / gap);
|
|
110
|
+
float by = (row + 0.5) * gap / cell.y - id.y;
|
|
111
|
+
float hb = hash(vec2(row, id.x + seed * 3.7));
|
|
112
|
+
float since = (by - y) * cell.y;
|
|
113
|
+
float size = radius * (0.2 + 0.22 * hb) * step(hb, 0.7) * smoothstep(radius, radius * 2.5, since) * exp(-since / 0.35) * step(by, 0.98) * alive;
|
|
114
|
+
offer(q, vec2((id.x + path(by, h) + (hb - 0.5) * 0.08) * cell.x, (row + 0.5) * gap - lift), size, 1.0);
|
|
115
|
+
return trail;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
void main() {
|
|
119
|
+
if (uShow <= 0.0) {
|
|
120
|
+
gl_FragColor = vec4(0.0);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
vec2 uv = gl_FragCoord.xy / uRes;
|
|
124
|
+
vec2 q = gl_FragCoord.xy / uUnit;
|
|
125
|
+
lensCover = 0.0;
|
|
126
|
+
lensAt = vec2(0.0);
|
|
127
|
+
lensCentre = vec2(0.0);
|
|
128
|
+
|
|
129
|
+
float trail = max(runners(q, vec2(0.13, 0.75), 1.0, uRain * 0.7), runners(q + vec2(0.04, 0.0), vec2(0.08, 0.45), 2.0, uRain * 0.5));
|
|
130
|
+
float wipe = texture2D(uWipe, uv).r;
|
|
131
|
+
float clear = clamp(max(trail, wipe), 0.0, 1.0);
|
|
132
|
+
float keep = 1.0 - clear;
|
|
133
|
+
beads(q, 0.075, 5.0, 0.05 + 0.3 * uRain, keep);
|
|
134
|
+
beads(q, 0.034, 3.0, 0.15 + 0.6 * uRain, keep);
|
|
135
|
+
beads(q, 0.014, 4.0, 0.3 + 0.55 * uRain, keep);
|
|
136
|
+
|
|
137
|
+
// Behind the glass: the soft copy, sharper where the glass is clear,
|
|
138
|
+
// under a mottled veil of condensation in the tint.
|
|
139
|
+
vec2 p = photoUv(uv);
|
|
140
|
+
vec3 soft = texture2D(uSoft, p).rgb;
|
|
141
|
+
vec3 sharp = texture2D(uPhoto, p, uClearBias).rgb;
|
|
142
|
+
vec3 col = mix(soft, sharp, clear * 0.8);
|
|
143
|
+
col = max(mix(vec3(dot(col, vec3(0.2126, 0.7152, 0.0722))), col, 1.25), 0.0);
|
|
144
|
+
float haze = uFog * keep * (0.75 + 0.5 * noise(q * 12.0));
|
|
145
|
+
col = mix(col, 1.0 - (1.0 - col) * (1.0 - uTint * 0.3), haze);
|
|
146
|
+
col = mix(col, vec3(dot(col, vec3(0.2126, 0.7152, 0.0722))), haze * 0.2);
|
|
147
|
+
|
|
148
|
+
// A drop: a fisheye lens, the photo sharp and upside down in it, dark
|
|
149
|
+
// at the rim, lit at the top and focusing a crescent at the bottom.
|
|
150
|
+
float rr = dot(lensAt, lensAt);
|
|
151
|
+
vec2 centre = lensCentre * uUnit / uRes;
|
|
152
|
+
vec2 look = centre - lensAt * (1.0 + 0.7 * rr) * vec2(uRes.y / uRes.x, 1.0) * 0.14;
|
|
153
|
+
vec3 seen = texture2D(uPhoto, photoUv(look)).rgb * 1.3 + 0.02;
|
|
154
|
+
float r = sqrt(rr);
|
|
155
|
+
float rim = smoothstep(0.72, 1.0, r);
|
|
156
|
+
seen = mix(seen, seen * 0.3, rim * 0.8);
|
|
157
|
+
float glint = smoothstep(0.24, 0.0, length(lensAt - vec2(-0.3, 0.45)));
|
|
158
|
+
float crescent = smoothstep(0.5, 0.8, r) * smoothstep(0.98, 0.8, r) * smoothstep(0.1, -0.8, lensAt.y);
|
|
159
|
+
seen += glint * glint * 0.6 + crescent * (0.06 + soft * 0.6 + uTint * 0.08);
|
|
160
|
+
col = mix(col, seen, lensCover);
|
|
161
|
+
|
|
162
|
+
// A soft vignette, and a little dither so the gradients do not band.
|
|
163
|
+
vec2 v = uv - 0.5;
|
|
164
|
+
col *= 1.0 - 0.7 * dot(v, v);
|
|
165
|
+
col += (hash(gl_FragCoord.xy + fract(uTime)) - 0.5) / 128.0;
|
|
166
|
+
gl_FragColor = vec4(clamp(col, 0.0, 1.0) * uShow, uShow);
|
|
167
|
+
}
|
|
168
|
+
`;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** A coarse grid of where the pointer has wiped the fog off the glass. */
|
|
2
|
+
export interface Wipe {
|
|
3
|
+
/** Cells across. */
|
|
4
|
+
width: number;
|
|
5
|
+
/** Cells up. */
|
|
6
|
+
height: number;
|
|
7
|
+
/** Wipes a soft spot at x, y (0–1, y up); `radius` a share of the height. */
|
|
8
|
+
stamp(x: number, y: number, radius: number): void;
|
|
9
|
+
/** Lets the fog return for `dt` seconds; true when `bytes` changed (upload them), false while the glass stays all fogged. */
|
|
10
|
+
settle(dt: number): boolean;
|
|
11
|
+
/** Clearness per cell, 0–255, bottom row first: for a LUMINANCE texture. */
|
|
12
|
+
bytes: Uint8Array;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Makes a wipe grid of `width` × `height` cells, all fogged. */
|
|
16
|
+
export function createWipe(width: number, height: number): Wipe;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/rain-on-glass/wipe
|
|
3
|
+
*
|
|
4
|
+
* Where the pointer has wiped the fog off the glass: a coarse grid that a
|
|
5
|
+
* soft brush clears along each stroke and that fogs back over, from the
|
|
6
|
+
* edges of each patch inwards, a few seconds later.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** How clear a fresh wipe is: over 1, so its middle holds clear a moment before fading. */
|
|
10
|
+
const FRESH = 1.6;
|
|
11
|
+
/** How fast the fog returns, in clearness a second. */
|
|
12
|
+
const RETURN = 0.36;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {object} Wipe
|
|
16
|
+
* @property {number} width Cells across.
|
|
17
|
+
* @property {number} height Cells up.
|
|
18
|
+
* @property {(x: number, y: number, radius: number) => void} stamp Wipes a soft spot at x, y (0–1, y up), `radius` a share of the height.
|
|
19
|
+
* @property {(dt: number) => boolean} settle Lets the fog return for `dt` seconds; true when `bytes` changed (upload them), false while the glass stays all fogged.
|
|
20
|
+
* @property {Uint8Array} bytes Clearness per cell, 0–255, bottom row first: for a LUMINANCE texture.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Makes a wipe grid.
|
|
25
|
+
*
|
|
26
|
+
* @param {number} width Cells across.
|
|
27
|
+
* @param {number} height Cells up.
|
|
28
|
+
* @returns {Wipe} The grid, all fogged.
|
|
29
|
+
*/
|
|
30
|
+
export function createWipe(width, height) {
|
|
31
|
+
const clear = new Float32Array(width * height);
|
|
32
|
+
const bytes = new Uint8Array(width * height);
|
|
33
|
+
let active = false;
|
|
34
|
+
return {
|
|
35
|
+
width,
|
|
36
|
+
height,
|
|
37
|
+
bytes,
|
|
38
|
+
stamp(x, y, radius) {
|
|
39
|
+
const r = Math.max(1, radius * height);
|
|
40
|
+
const cx = x * width;
|
|
41
|
+
const cy = y * height;
|
|
42
|
+
for (let j = Math.max(0, Math.floor(cy - r)); j <= Math.min(height - 1, Math.ceil(cy + r)); j++) {
|
|
43
|
+
for (let i = Math.max(0, Math.floor(cx - r)); i <= Math.min(width - 1, Math.ceil(cx + r)); i++) {
|
|
44
|
+
const d = Math.hypot(i + 0.5 - cx, j + 0.5 - cy) / r;
|
|
45
|
+
if (d >= 1) continue;
|
|
46
|
+
const k = 1 - d * d;
|
|
47
|
+
clear[j * width + i] = Math.max(clear[j * width + i], FRESH * k * k * (3 - 2 * k));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
active = true;
|
|
51
|
+
},
|
|
52
|
+
settle(dt) {
|
|
53
|
+
if (!active) return false;
|
|
54
|
+
active = false;
|
|
55
|
+
const step = RETURN * dt;
|
|
56
|
+
for (let i = 0; i < clear.length; i++) {
|
|
57
|
+
const v = Math.max(0, clear[i] - step);
|
|
58
|
+
clear[i] = v;
|
|
59
|
+
bytes[i] = Math.min(255, Math.round(v * 255));
|
|
60
|
+
if (v > 0) active = true;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface RainOnGlassConfig {
|
|
4
|
+
/** The photo behind the glass (same origin, or served with CORS). */
|
|
5
|
+
src: string;
|
|
6
|
+
/** How much rain, 0–1: beads on the glass and drops running down it. Default 0.6. */
|
|
7
|
+
rain?: number;
|
|
8
|
+
/** How soft the photo is behind the glass, 0–1. Default 0.6. */
|
|
9
|
+
blur?: number;
|
|
10
|
+
/** How thick the condensation is, 0–1. Default 0.5. */
|
|
11
|
+
fog?: number;
|
|
12
|
+
/** How fast the drops run and the beads come and go, 0–3. Default 1. */
|
|
13
|
+
speed?: number;
|
|
14
|
+
/** The pointer wipes a clear patch that fogs back over. Default true. */
|
|
15
|
+
interactive?: boolean;
|
|
16
|
+
/** The colour of the fog. Default `#a3b8cc`. */
|
|
17
|
+
tint?: string;
|
|
18
|
+
/** How strongly the photo's lights open into bokeh discs, 0–1. Default 0.6. */
|
|
19
|
+
bokeh?: number;
|
|
20
|
+
/** Colours for the bokeh discs. Default: each light's own colour. */
|
|
21
|
+
colors?: string[];
|
|
22
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
|
|
23
|
+
palette?: PaletteName | (string & {});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Starts rain on glass in `el`'s canvas. Returns a function that stops it. */
|
|
27
|
+
export function rainOnGlass(el: HTMLElement, config: RainOnGlassConfig): () => void;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/rain-on-glass
|
|
3
|
+
*
|
|
4
|
+
* Rain on a window over a photo, like looking out of a café on a wet
|
|
5
|
+
* evening. Small beads sit on the glass; larger drops slide down it in
|
|
6
|
+
* fits and starts, leaving clear trails that bead up and fog over again.
|
|
7
|
+
* Each drop is a little lens, holding the photo sharp and upside down; the
|
|
8
|
+
* rest of the photo is soft behind the condensation, its lights opened
|
|
9
|
+
* into bokeh. The pointer wipes a clear patch that fogs back over.
|
|
10
|
+
*
|
|
11
|
+
* Transparent until the photo has loaded, then fading in over the `<img>`
|
|
12
|
+
* under it (the component's), which is what shows without WebGL. With
|
|
13
|
+
* reduced motion it is one still frame, drops and all.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { createStage } from './stage.js';
|
|
17
|
+
import { parseColor } from './color.js';
|
|
18
|
+
import { trail } from './pointer.js';
|
|
19
|
+
import { watchColors } from './mount.js';
|
|
20
|
+
import { pickColors } from './palettes.js';
|
|
21
|
+
import { loadImage, imageTexture, coverScale } from './image.js';
|
|
22
|
+
import { backdrop } from './rain-on-glass/backdrop.js';
|
|
23
|
+
import { createWipe } from './rain-on-glass/wipe.js';
|
|
24
|
+
import { FRAG } from './rain-on-glass/shader.js';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {object} RainOnGlassConfig
|
|
28
|
+
* @property {string} src The photo behind the glass (same origin, or served with CORS).
|
|
29
|
+
* @property {number} [rain=0.6] How much rain, 0–1: beads on the glass and drops running down it.
|
|
30
|
+
* @property {number} [blur=0.6] How soft the photo is behind the glass, 0–1.
|
|
31
|
+
* @property {number} [fog=0.5] How thick the condensation is, 0–1.
|
|
32
|
+
* @property {number} [speed=1] How fast the drops run and the beads come and go, 0–3.
|
|
33
|
+
* @property {boolean} [interactive=true] The pointer wipes a clear patch that fogs back over.
|
|
34
|
+
* @property {string} [tint='#a3b8cc'] The colour of the fog.
|
|
35
|
+
* @property {number} [bokeh=0.6] How strongly the photo's lights open into bokeh discs, 0–1.
|
|
36
|
+
* @property {string[]} [colors] Colours for the bokeh discs (default: each light's own).
|
|
37
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/** CSS pixels to a unit of glass, the scale the drops are sized in. */
|
|
41
|
+
const UNIT = 720;
|
|
42
|
+
/** Seconds into the rain the first frame is, so it opens mid-shower; and the moment the still frame shows. */
|
|
43
|
+
const OPENING = 40;
|
|
44
|
+
/** The wiping brush's radius, CSS pixels. */
|
|
45
|
+
const BRUSH = 58;
|
|
46
|
+
/** Seconds the canvas takes to fade in over the `<img>` once the photo has loaded. */
|
|
47
|
+
const FADE = 0.9;
|
|
48
|
+
/** Cells across the wipe grid. */
|
|
49
|
+
const WIPE_CELLS = 96;
|
|
50
|
+
/** The largest side the photo is put on the GPU at, pixels. */
|
|
51
|
+
const LARGEST = 2560;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Starts rain on glass on an element: a canvas inside it, filling it.
|
|
55
|
+
*
|
|
56
|
+
* @param {HTMLElement} el The element (its first canvas is drawn into).
|
|
57
|
+
* @param {RainOnGlassConfig} config The settings.
|
|
58
|
+
* @returns {() => void} Stops it.
|
|
59
|
+
*/
|
|
60
|
+
export function rainOnGlass(el, config) {
|
|
61
|
+
const speed = config.speed ?? 1;
|
|
62
|
+
const blur = clamp01(config.blur ?? 0.6);
|
|
63
|
+
const bokeh = clamp01(config.bokeh ?? 0.6);
|
|
64
|
+
const interactive = config.interactive ?? true;
|
|
65
|
+
let colors = pickColors(config, []);
|
|
66
|
+
const strokes = trail(el, { step: 0.01, max: 24 });
|
|
67
|
+
const start = performance.now();
|
|
68
|
+
/** @type {HTMLImageElement | null} */
|
|
69
|
+
let photo = null;
|
|
70
|
+
/** @type {WebGLTexture | null} */
|
|
71
|
+
let softTexture = null;
|
|
72
|
+
/** @type {WebGLTexture | null} */
|
|
73
|
+
let photoTexture = null;
|
|
74
|
+
/** @type {WebGLTexture | null} */
|
|
75
|
+
let wipeTexture = null;
|
|
76
|
+
let wipe = createWipe(1, 1);
|
|
77
|
+
let shown = 0;
|
|
78
|
+
let stopped = false;
|
|
79
|
+
|
|
80
|
+
const { stage, stop } = createStage(el, {
|
|
81
|
+
fragment: FRAG,
|
|
82
|
+
setup({ gl, uniform }) {
|
|
83
|
+
gl.uniform1i(uniform('uPhoto'), 0);
|
|
84
|
+
gl.uniform1i(uniform('uSoft'), 1);
|
|
85
|
+
gl.uniform1i(uniform('uWipe'), 2);
|
|
86
|
+
gl.uniform1f(uniform('uRain'), clamp01(config.rain ?? 0.6));
|
|
87
|
+
gl.uniform1f(uniform('uFog'), clamp01(config.fog ?? 0.5));
|
|
88
|
+
gl.uniform1f(uniform('uClearBias'), 0.5 + blur * 1.5);
|
|
89
|
+
gl.uniform3fv(uniform('uTint'), parseColor(config.tint ?? '#a3b8cc'));
|
|
90
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
91
|
+
wipeTexture = gl.createTexture();
|
|
92
|
+
gl.bindTexture(gl.TEXTURE_2D, wipeTexture);
|
|
93
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
94
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
95
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
96
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
97
|
+
return () => [photoTexture, softTexture, wipeTexture].forEach((t) => gl.deleteTexture(t));
|
|
98
|
+
},
|
|
99
|
+
resize(s) {
|
|
100
|
+
s.gl.uniform1f(s.uniform('uUnit'), s.ratio * UNIT);
|
|
101
|
+
fit(s);
|
|
102
|
+
// A fresh, fogged wipe grid in the canvas's proportions.
|
|
103
|
+
wipe = createWipe(WIPE_CELLS, Math.min(240, Math.max(12, Math.round((WIPE_CELLS * s.canvas.height) / s.canvas.width))));
|
|
104
|
+
upload(s.gl, true);
|
|
105
|
+
},
|
|
106
|
+
uniforms({ gl, uniform, still }, now, dt) {
|
|
107
|
+
if (photo && shown < 1) {
|
|
108
|
+
shown = still ? 1 : Math.min(1, shown + dt / FADE);
|
|
109
|
+
if (shown === 1) el.dataset.drawn = '';
|
|
110
|
+
}
|
|
111
|
+
if (interactive && !still) {
|
|
112
|
+
const radius = BRUSH / Math.max(1, el.clientHeight);
|
|
113
|
+
for (const p of strokes.read(dt)) wipe.stamp(p.x, p.y, radius);
|
|
114
|
+
}
|
|
115
|
+
if (wipe.settle(dt)) upload(gl, false);
|
|
116
|
+
gl.uniform1f(uniform('uTime'), OPENING + (still ? 0 : ((now - start) / 1000) * speed));
|
|
117
|
+
gl.uniform1f(uniform('uShow'), shown * shown * (3 - 2 * shown));
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
if (!stage) return stop;
|
|
121
|
+
const { gl } = stage;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Sets how the photo is cropped to cover the canvas, once it has loaded.
|
|
125
|
+
*
|
|
126
|
+
* @param {import('./stage.js').Stage} s The stage.
|
|
127
|
+
*/
|
|
128
|
+
function fit(s) {
|
|
129
|
+
if (!photo) return;
|
|
130
|
+
s.gl.useProgram(s.program);
|
|
131
|
+
s.gl.uniform2fv(s.uniform('uCover'), coverScale(photo.width / photo.height, s.canvas.width / s.canvas.height));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Puts the wipe grid on the GPU.
|
|
136
|
+
*
|
|
137
|
+
* @param {import('./gl.js').GL} gl The context.
|
|
138
|
+
* @param {boolean} fresh The grid is new (a new size): allocate the texture again.
|
|
139
|
+
*/
|
|
140
|
+
function upload(gl, fresh) {
|
|
141
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
142
|
+
gl.bindTexture(gl.TEXTURE_2D, wipeTexture);
|
|
143
|
+
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
|
|
144
|
+
if (fresh) gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, wipe.width, wipe.height, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, wipe.bytes);
|
|
145
|
+
else gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, wipe.width, wipe.height, gl.LUMINANCE, gl.UNSIGNED_BYTE, wipe.bytes);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Makes the soft, bokeh'd copy of the photo in the current colours and puts it on the GPU. */
|
|
149
|
+
function soften() {
|
|
150
|
+
if (!photo) return;
|
|
151
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
152
|
+
gl.deleteTexture(softTexture);
|
|
153
|
+
softTexture = imageTexture(gl, backdrop(photo, { blur, bokeh, colors }));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
loadImage(config.src)
|
|
157
|
+
.then((img) => {
|
|
158
|
+
if (stopped) return;
|
|
159
|
+
photo = img;
|
|
160
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
161
|
+
photoTexture = imageTexture(gl, within(img, Math.min(LARGEST, gl.getParameter(gl.MAX_TEXTURE_SIZE))), { mipmap: true });
|
|
162
|
+
soften();
|
|
163
|
+
fit(stage);
|
|
164
|
+
if (stage.still) stage.redraw();
|
|
165
|
+
})
|
|
166
|
+
.catch((error) => {
|
|
167
|
+
console.warn('[effects-astro]', error);
|
|
168
|
+
stop();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const unwatch = watchColors(el, (next) => {
|
|
172
|
+
colors = next.colors;
|
|
173
|
+
soften();
|
|
174
|
+
if (stage.still) stage.redraw();
|
|
175
|
+
});
|
|
176
|
+
return () => {
|
|
177
|
+
stopped = true;
|
|
178
|
+
stop();
|
|
179
|
+
unwatch();
|
|
180
|
+
delete el.dataset.drawn;
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A picture no larger than `largest` on either side: itself, or a copy
|
|
186
|
+
* scaled down on a canvas.
|
|
187
|
+
*
|
|
188
|
+
* @param {HTMLImageElement} img The picture.
|
|
189
|
+
* @param {number} largest The most pixels a side may have.
|
|
190
|
+
* @returns {HTMLImageElement | HTMLCanvasElement} It, or its smaller copy.
|
|
191
|
+
*/
|
|
192
|
+
function within(img, largest) {
|
|
193
|
+
const scale = largest / Math.max(img.width, img.height);
|
|
194
|
+
if (scale >= 1) return img;
|
|
195
|
+
const canvas = document.createElement('canvas');
|
|
196
|
+
canvas.width = Math.round(img.width * scale);
|
|
197
|
+
canvas.height = Math.round(img.height * scale);
|
|
198
|
+
canvas.getContext('2d')?.drawImage(img, 0, 0, canvas.width, canvas.height);
|
|
199
|
+
return canvas;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Clamps a number to 0–1.
|
|
204
|
+
*
|
|
205
|
+
* @param {number} n The number.
|
|
206
|
+
* @returns {number} It, clamped.
|
|
207
|
+
*/
|
|
208
|
+
function clamp01(n) {
|
|
209
|
+
return Math.min(1, Math.max(0, n));
|
|
210
|
+
}
|