@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,453 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/audio
|
|
3
|
+
*
|
|
4
|
+
* The sound playing on the page, as a live signal effects can draw with:
|
|
5
|
+
* its level, bass, mid and treble, a kick on each beat, a spectrum and a
|
|
6
|
+
* waveform (./audio-meter.js does the arithmetic). An effect asks for it
|
|
7
|
+
* with `audioSignal(source)`, where the source is a CSS selector for an
|
|
8
|
+
* `<audio>` or `<video>`, or for a player's container (a WaveformPlayer,
|
|
9
|
+
* whose `<audio>` is not in the page), or `'page'` for whatever plays.
|
|
10
|
+
*
|
|
11
|
+
* How it hears:
|
|
12
|
+
*
|
|
13
|
+
* - Media in the page are seen by their own `play`, `pause` and
|
|
14
|
+
* `timeupdate` events, caught on the document. A WaveformPlayer's
|
|
15
|
+
* `<audio>` is not in the page: it is reached through the player's
|
|
16
|
+
* `waveformplayer:play`, `:pause`, `:ended`, `:timeupdate` and `:destroy`
|
|
17
|
+
* events, whose `detail.player.audio` it is. `timeupdate` lets an effect
|
|
18
|
+
* that mounts mid-track join in.
|
|
19
|
+
* - One AudioContext for the page, and one analyser per media element,
|
|
20
|
+
* shared by every effect following it. `createMediaElementSource` can be
|
|
21
|
+
* called once per element, ever, and from then on the element's sound
|
|
22
|
+
* goes through the context, so the source stays connected to the
|
|
23
|
+
* speakers as well as the analyser, and the context is woken whenever
|
|
24
|
+
* the element plays, by a listener on the element itself that outlives
|
|
25
|
+
* every effect.
|
|
26
|
+
* - Browsers start an AudioContext only after the visitor has interacted
|
|
27
|
+
* with the page. It is made (or woken) on the first press or key while
|
|
28
|
+
* an effect is listening, and an element is only routed through it once
|
|
29
|
+
* it runs, so a context that cannot start never silences anything: the
|
|
30
|
+
* sound plays as before and the effects keep their own motion.
|
|
31
|
+
* - Audio from another origin can only be read with CORS: the element
|
|
32
|
+
* needs `crossorigin="anonymous"` (WaveformPlayer: `crossOrigin` or
|
|
33
|
+
* `data-cross-origin`) and the server an `Access-Control-Allow-Origin`
|
|
34
|
+
* header. Without both, routing it would silence it, so it is left alone
|
|
35
|
+
* and a warning says why. (A same-origin URL that redirects to another
|
|
36
|
+
* origin cannot be told apart beforehand; serve audio from where it is.)
|
|
37
|
+
*
|
|
38
|
+
* What it costs: nothing until something plays. No context is made before
|
|
39
|
+
* the visitor first interacts, the analysers are read only while their
|
|
40
|
+
* element plays (once a frame, however many effects follow it), and the
|
|
41
|
+
* context is suspended a few seconds after the last sound stops.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
import { createMeter } from './audio-meter.js';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {import('./audio-meter.js').Reading} Reading
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* What an effect reads each frame while sound plays: the meter's reading,
|
|
52
|
+
* how far to follow it, and where the track is.
|
|
53
|
+
*
|
|
54
|
+
* @typedef {Reading & { on: number, progress: number }} AudioReading
|
|
55
|
+
* `on` is how much the effect should follow the sound, 0–1: it eases in as
|
|
56
|
+
* playback starts and out after it stops, so the effect can blend from its
|
|
57
|
+
* own motion and back. `progress` is how far through the track, 0–1, or -1
|
|
58
|
+
* when its length is unknown (a stream).
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @typedef {object} AudioSignal
|
|
63
|
+
* @property {(now: number, dt: number) => AudioReading | null} read The sound
|
|
64
|
+
* this frame, or null when nothing it follows has played lately: the effect
|
|
65
|
+
* then runs as it would without sound, at no cost.
|
|
66
|
+
* @property {() => void} stop Stops listening.
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* One media element routed through the context.
|
|
71
|
+
*
|
|
72
|
+
* @typedef {object} Tap
|
|
73
|
+
* @property {AnalyserNode} analyser Its analyser.
|
|
74
|
+
* @property {Uint8Array<ArrayBuffer>} freq Its spectrum, read each frame.
|
|
75
|
+
* @property {Uint8Array<ArrayBuffer>} time Its waveform, read each frame.
|
|
76
|
+
* @property {import('./audio-meter.js').Meter} meter What the bytes mean.
|
|
77
|
+
* @property {number} stamp The frame last read (its time), so effects sharing it read it once.
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A media element heard playing: the element whose events told of it (the
|
|
82
|
+
* media itself, or a player's container), and when it started.
|
|
83
|
+
*
|
|
84
|
+
* @typedef {object} Heard
|
|
85
|
+
* @property {Element} owner The element its events came from.
|
|
86
|
+
* @property {number} at When it was first heard, ms.
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* One effect listening.
|
|
91
|
+
*
|
|
92
|
+
* @typedef {object} Listener
|
|
93
|
+
* @property {(media: HTMLMediaElement, owner: Element) => boolean} follows Whether it follows a media element.
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
/** The analyser's FFT size: 1024 bins, about 21 Hz each at 44.1 kHz. */
|
|
97
|
+
const FFT = 2048;
|
|
98
|
+
/** Its decibel range, mapped to the spectrum's bytes 0–255. */
|
|
99
|
+
const MIN_DB = -90;
|
|
100
|
+
const MAX_DB = -20;
|
|
101
|
+
/** Seconds `on` takes to ease in when playback starts, and out after it stops. */
|
|
102
|
+
const ON_RISE = 0.25;
|
|
103
|
+
const ON_FALL = 0.35;
|
|
104
|
+
/** How long after the last sound stops the context is suspended, ms. */
|
|
105
|
+
const IDLE = 4000;
|
|
106
|
+
/** The longest frame the meter steps, seconds: after a stall it resumes, not jumps. */
|
|
107
|
+
const MAX_DT = 0.05;
|
|
108
|
+
/** WaveformPlayer's events: those that mean its audio is playing, and those that mean it has stopped. */
|
|
109
|
+
const PLAYER_ON = ['waveformplayer:play', 'waveformplayer:timeupdate'];
|
|
110
|
+
const PLAYER_OFF = ['waveformplayer:pause', 'waveformplayer:ended', 'waveformplayer:destroy'];
|
|
111
|
+
/** Media events, caught on the document (they do not bubble): playing, and stopped. */
|
|
112
|
+
const MEDIA_ON = ['play', 'timeupdate'];
|
|
113
|
+
const MEDIA_OFF = ['pause', 'ended', 'emptied'];
|
|
114
|
+
/** Presses and keys that let a context start. */
|
|
115
|
+
const GESTURES = ['pointerdown', 'keydown'];
|
|
116
|
+
|
|
117
|
+
/** @type {AudioContext | null} */
|
|
118
|
+
let context = null;
|
|
119
|
+
/** Every element routed through the context: it stays routed for good, so it is held for good. */
|
|
120
|
+
/** @type {Map<HTMLMediaElement, Tap>} */
|
|
121
|
+
const taps = new Map();
|
|
122
|
+
/** @type {Map<HTMLMediaElement, Heard>} */
|
|
123
|
+
const heard = new Map();
|
|
124
|
+
/** @type {Set<Listener>} */
|
|
125
|
+
const listeners = new Set();
|
|
126
|
+
/** Sources already warned about (cross-origin, or taken by another context). */
|
|
127
|
+
const warned = new Set();
|
|
128
|
+
let idleTimer = 0;
|
|
129
|
+
/** True while a context is being asked to start, so it is asked once. */
|
|
130
|
+
let starting = false;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Whether audio from a URL can be read by Web Audio: same origin, a blob or
|
|
134
|
+
* data URL, or fetched with CORS (`crossorigin` set: if the server had not
|
|
135
|
+
* allowed it, the element would not have played).
|
|
136
|
+
*
|
|
137
|
+
* @param {string} src The element's current source URL ('' for a stream set as `srcObject`).
|
|
138
|
+
* @param {string | null} crossOrigin The element's `crossOrigin`.
|
|
139
|
+
* @param {string} origin The page's origin.
|
|
140
|
+
* @returns {boolean} Whether it can be read.
|
|
141
|
+
*/
|
|
142
|
+
export function readable(src, crossOrigin, origin) {
|
|
143
|
+
if (!src || crossOrigin !== null) return true;
|
|
144
|
+
try {
|
|
145
|
+
const url = new URL(src, origin);
|
|
146
|
+
return url.protocol === 'blob:' || url.protocol === 'data:' || url.origin === origin;
|
|
147
|
+
} catch {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Says once, per source, why some audio cannot be followed.
|
|
154
|
+
*
|
|
155
|
+
* @param {string} key What it is about, so it is said once.
|
|
156
|
+
* @param {string} message Why.
|
|
157
|
+
*/
|
|
158
|
+
function warnOnce(key, message) {
|
|
159
|
+
if (warned.has(key)) return;
|
|
160
|
+
warned.add(key);
|
|
161
|
+
console.warn('[effects-astro]', message);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The page's AudioContext: made on first use, but only once the visitor has
|
|
166
|
+
* interacted (before that, browsers would refuse it and log a warning).
|
|
167
|
+
*
|
|
168
|
+
* @returns {AudioContext | null} The context, or null for now.
|
|
169
|
+
*/
|
|
170
|
+
function getContext() {
|
|
171
|
+
if (context) return context;
|
|
172
|
+
const Context = window.AudioContext ?? /** @type {any} */ (window).webkitAudioContext;
|
|
173
|
+
const active = navigator.userActivation?.hasBeenActive ?? true;
|
|
174
|
+
if (!Context || !active) return null;
|
|
175
|
+
context = /** @type {AudioContext} */ (new Context());
|
|
176
|
+
return context;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Whether anything routed through the context is playing.
|
|
181
|
+
*
|
|
182
|
+
* @returns {boolean} True while a tapped element plays.
|
|
183
|
+
*/
|
|
184
|
+
function sounding() {
|
|
185
|
+
for (const media of taps.keys()) if (!media.paused) return true;
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Suspends the context a while after the last sound stops (it is woken
|
|
191
|
+
* when one plays again), and forgets elements no longer playing.
|
|
192
|
+
*/
|
|
193
|
+
function idleSoon() {
|
|
194
|
+
clearTimeout(idleTimer);
|
|
195
|
+
idleTimer = window.setTimeout(() => {
|
|
196
|
+
for (const media of heard.keys()) if (media.paused) heard.delete(media);
|
|
197
|
+
if (context?.state === 'running' && !sounding()) context.suspend();
|
|
198
|
+
}, IDLE);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Wakes the context for an element routed through it that has started to
|
|
203
|
+
* play: its sound goes nowhere else. Warns if it now plays audio that
|
|
204
|
+
* cannot be read, which Web Audio will silence.
|
|
205
|
+
*
|
|
206
|
+
* @param {Event} e The element's `play`.
|
|
207
|
+
*/
|
|
208
|
+
function wake(e) {
|
|
209
|
+
const media = /** @type {HTMLMediaElement} */ (e.target);
|
|
210
|
+
const src = media.currentSrc || media.src;
|
|
211
|
+
if (!readable(src, media.crossOrigin, location.origin)) {
|
|
212
|
+
warnOnce(src, `The audio at ${src} is on another origin without CORS, and its element is already routed through Web Audio, so it plays silent. Serve it with Access-Control-Allow-Origin and set crossorigin="anonymous" on the element (WaveformPlayer: crossOrigin: 'anonymous' or data-cross-origin="anonymous").`);
|
|
213
|
+
}
|
|
214
|
+
if (context?.state !== 'running') context?.resume();
|
|
215
|
+
idleSoon();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Routes a playing element through the context to an analyser (and on to
|
|
220
|
+
* the speakers), if it can be read and is wanted, once the context runs.
|
|
221
|
+
*
|
|
222
|
+
* @param {HTMLMediaElement} media The element.
|
|
223
|
+
*/
|
|
224
|
+
function tap(media) {
|
|
225
|
+
if (taps.has(media)) {
|
|
226
|
+
if (context?.state !== 'running') context?.resume();
|
|
227
|
+
idleSoon();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const src = media.currentSrc || media.src;
|
|
231
|
+
if (!readable(src, media.crossOrigin, location.origin)) {
|
|
232
|
+
warnOnce(src, `The audio at ${src} is on another origin without CORS, so it cannot be followed (routing it through Web Audio would silence it). Serve it with Access-Control-Allow-Origin and set crossorigin="anonymous" on the element (WaveformPlayer: crossOrigin: 'anonymous' or data-cross-origin="anonymous"). The effect keeps its own motion.`);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const ctx = getContext();
|
|
236
|
+
if (!ctx) return;
|
|
237
|
+
if (ctx.state !== 'running') {
|
|
238
|
+
// Routed only once the context runs, so a context that cannot start
|
|
239
|
+
// never silences the element; it is tried again on the next press.
|
|
240
|
+
if (starting) return;
|
|
241
|
+
starting = true;
|
|
242
|
+
ctx.resume().then(
|
|
243
|
+
() => {
|
|
244
|
+
starting = false;
|
|
245
|
+
if (ctx.state === 'running' && !media.paused) tap(media);
|
|
246
|
+
},
|
|
247
|
+
() => {
|
|
248
|
+
starting = false;
|
|
249
|
+
},
|
|
250
|
+
);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
let source;
|
|
254
|
+
try {
|
|
255
|
+
source = ctx.createMediaElementSource(media);
|
|
256
|
+
} catch {
|
|
257
|
+
warnOnce(`taken:${src}`, `The audio at ${src} is already routed through another AudioContext, so it cannot be followed.`);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const analyser = ctx.createAnalyser();
|
|
261
|
+
analyser.fftSize = FFT;
|
|
262
|
+
analyser.minDecibels = MIN_DB;
|
|
263
|
+
analyser.maxDecibels = MAX_DB;
|
|
264
|
+
analyser.smoothingTimeConstant = 0.6;
|
|
265
|
+
source.connect(ctx.destination);
|
|
266
|
+
source.connect(analyser);
|
|
267
|
+
taps.set(media, {
|
|
268
|
+
analyser,
|
|
269
|
+
freq: new Uint8Array(analyser.frequencyBinCount),
|
|
270
|
+
time: new Uint8Array(FFT),
|
|
271
|
+
meter: createMeter(),
|
|
272
|
+
stamp: -1,
|
|
273
|
+
});
|
|
274
|
+
// The element plays only through the context now, listened to or not.
|
|
275
|
+
media.addEventListener('play', wake);
|
|
276
|
+
media.addEventListener('pause', idleSoon);
|
|
277
|
+
idleSoon();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Notes an element playing, and taps it if an effect follows it.
|
|
282
|
+
*
|
|
283
|
+
* @param {HTMLMediaElement} media The element.
|
|
284
|
+
* @param {Element} owner The element its events came from.
|
|
285
|
+
* @param {boolean} [started=false] It has just started (not a `timeupdate`): the latest to start is the one followed.
|
|
286
|
+
*/
|
|
287
|
+
function playing(media, owner, started = false) {
|
|
288
|
+
if (started || !heard.has(media)) heard.set(media, { owner, at: performance.now() });
|
|
289
|
+
for (const listener of listeners) {
|
|
290
|
+
if (listener.follows(media, owner)) {
|
|
291
|
+
tap(media);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* A media element's events, caught on the document.
|
|
299
|
+
*
|
|
300
|
+
* @param {Event} e The event.
|
|
301
|
+
*/
|
|
302
|
+
function onMedia(e) {
|
|
303
|
+
const media = e.target;
|
|
304
|
+
if (!(media instanceof HTMLMediaElement)) return;
|
|
305
|
+
if (MEDIA_ON.includes(e.type)) {
|
|
306
|
+
if (!media.paused) playing(media, media, e.type === 'play');
|
|
307
|
+
} else {
|
|
308
|
+
idleSoon();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* A WaveformPlayer's events: its `<audio>` is `detail.player.audio`.
|
|
314
|
+
*
|
|
315
|
+
* @param {Event} e The event.
|
|
316
|
+
*/
|
|
317
|
+
function onPlayer(e) {
|
|
318
|
+
const media = /** @type {CustomEvent} */ (e).detail?.player?.audio;
|
|
319
|
+
if (!(media instanceof HTMLMediaElement) || !(e.target instanceof Element)) return;
|
|
320
|
+
if (PLAYER_ON.includes(e.type)) {
|
|
321
|
+
if (!media.paused) playing(media, e.target, e.type === 'waveformplayer:play');
|
|
322
|
+
} else {
|
|
323
|
+
if (e.type === 'waveformplayer:destroy') heard.delete(media);
|
|
324
|
+
idleSoon();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* A press or a key: the moment a context may start. Makes or wakes it, and
|
|
330
|
+
* taps whatever is already playing that an effect follows.
|
|
331
|
+
*/
|
|
332
|
+
function onGesture() {
|
|
333
|
+
const ctx = getContext();
|
|
334
|
+
if (!ctx) return;
|
|
335
|
+
if (ctx.state !== 'running') {
|
|
336
|
+
ctx.resume().then(() => {
|
|
337
|
+
for (const [media, { owner }] of heard) if (!media.paused) playing(media, owner);
|
|
338
|
+
}, () => {});
|
|
339
|
+
}
|
|
340
|
+
idleSoon();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Starts or stops hearing the page: its media and players' events, and the
|
|
345
|
+
* presses and keys that let a context start.
|
|
346
|
+
*
|
|
347
|
+
* @param {boolean} on Whether to listen.
|
|
348
|
+
*/
|
|
349
|
+
function hearPage(on) {
|
|
350
|
+
const method = on ? 'addEventListener' : 'removeEventListener';
|
|
351
|
+
for (const type of [...MEDIA_ON, ...MEDIA_OFF]) document[method](type, onMedia, true);
|
|
352
|
+
for (const type of [...PLAYER_ON, ...PLAYER_OFF]) document[method](type, onPlayer);
|
|
353
|
+
for (const type of GESTURES) document[method](type, onGesture, { capture: true, passive: true });
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Which media elements a source follows.
|
|
358
|
+
*
|
|
359
|
+
* @param {string} source `'page'`, or a CSS selector for a media element or a player's container.
|
|
360
|
+
* @returns {(media: HTMLMediaElement, owner: Element) => boolean} The test.
|
|
361
|
+
*/
|
|
362
|
+
function follower(source) {
|
|
363
|
+
if (source === 'page') return () => true;
|
|
364
|
+
/** @type {WeakMap<HTMLMediaElement, boolean>} */
|
|
365
|
+
const known = new WeakMap();
|
|
366
|
+
return (media, owner) => {
|
|
367
|
+
let yes = known.get(media);
|
|
368
|
+
if (yes === undefined) {
|
|
369
|
+
let target = null;
|
|
370
|
+
try {
|
|
371
|
+
target = document.querySelector(source);
|
|
372
|
+
} catch {
|
|
373
|
+
warnOnce(`selector:${source}`, `audio="${source}" is not a CSS selector; use one, or "page".`);
|
|
374
|
+
}
|
|
375
|
+
yes = !!target && (target === media || target.contains(owner) || owner.contains(target));
|
|
376
|
+
// Not found yet may mean not rendered yet: asked again next time.
|
|
377
|
+
if (target) known.set(media, yes);
|
|
378
|
+
}
|
|
379
|
+
return yes;
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Starts listening to the sound an effect follows.
|
|
385
|
+
*
|
|
386
|
+
* @param {string} source `'page'` for whatever plays, or a CSS selector for
|
|
387
|
+
* an `<audio>`/`<video>` or a player's container (a WaveformPlayer).
|
|
388
|
+
* @returns {AudioSignal} The signal: read it each frame, stop it with the effect.
|
|
389
|
+
*/
|
|
390
|
+
export function audioSignal(source) {
|
|
391
|
+
/** @type {Listener} */
|
|
392
|
+
const listener = { follows: follower(source.trim() || 'page') };
|
|
393
|
+
if (!listeners.size) hearPage(true);
|
|
394
|
+
listeners.add(listener);
|
|
395
|
+
let on = 0;
|
|
396
|
+
/** @type {HTMLMediaElement | null} */
|
|
397
|
+
let current = null;
|
|
398
|
+
/** @type {AudioReading | null} */
|
|
399
|
+
let out = null;
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* The element to follow this frame: the latest to start of those playing
|
|
403
|
+
* that this effect follows and that are tapped.
|
|
404
|
+
*
|
|
405
|
+
* @returns {HTMLMediaElement | null} It, or null.
|
|
406
|
+
*/
|
|
407
|
+
const pick = () => {
|
|
408
|
+
/** @type {HTMLMediaElement | null} */
|
|
409
|
+
let best = null;
|
|
410
|
+
let at = -Infinity;
|
|
411
|
+
for (const [media, h] of heard) {
|
|
412
|
+
if (media.paused || h.at < at || !taps.has(media) || !listener.follows(media, h.owner)) continue;
|
|
413
|
+
best = media;
|
|
414
|
+
at = h.at;
|
|
415
|
+
}
|
|
416
|
+
return best;
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
return {
|
|
420
|
+
read(now, frameDt) {
|
|
421
|
+
if (!heard.size && !on) return null;
|
|
422
|
+
const dt = Math.min(frameDt, MAX_DT);
|
|
423
|
+
const next = pick();
|
|
424
|
+
if (next) current = next;
|
|
425
|
+
on += ((next ? 1 : 0) - on) * (1 - Math.exp(-dt / (next ? ON_RISE : ON_FALL)));
|
|
426
|
+
if (!next && on < 0.01) {
|
|
427
|
+
on = 0;
|
|
428
|
+
current = null;
|
|
429
|
+
return null;
|
|
430
|
+
}
|
|
431
|
+
const t = current && taps.get(current);
|
|
432
|
+
if (!current || !t) return null;
|
|
433
|
+
if (t.stamp !== now) {
|
|
434
|
+
t.stamp = now;
|
|
435
|
+
t.analyser.getByteFrequencyData(t.freq);
|
|
436
|
+
t.analyser.getByteTimeDomainData(t.time);
|
|
437
|
+
t.meter.update(t.freq, t.time, t.analyser.context.sampleRate / FFT, dt);
|
|
438
|
+
}
|
|
439
|
+
const duration = current.duration;
|
|
440
|
+
out = Object.assign(out ?? { on: 0, progress: -1 }, t.meter.reading);
|
|
441
|
+
out.on = on;
|
|
442
|
+
out.progress = Number.isFinite(duration) && duration > 0 ? current.currentTime / duration : -1;
|
|
443
|
+
return out;
|
|
444
|
+
},
|
|
445
|
+
stop() {
|
|
446
|
+
listeners.delete(listener);
|
|
447
|
+
if (!listeners.size) {
|
|
448
|
+
hearPage(false);
|
|
449
|
+
idleSoon();
|
|
450
|
+
}
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/blur
|
|
3
|
+
*
|
|
4
|
+
* A Gaussian-like blur for small pictures on the CPU: three box blurs
|
|
5
|
+
* across and three down, each a running sum, so its cost does not grow
|
|
6
|
+
* with the radius. For a soft copy of a photo made once at load, where a
|
|
7
|
+
* GPU pass would be more code than it saves.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Box-blurs one line of RGBA pixels into another, with a running sum,
|
|
12
|
+
* repeating the edge pixels past the ends.
|
|
13
|
+
*
|
|
14
|
+
* @param {Float32Array} from The pixels read.
|
|
15
|
+
* @param {Float32Array} to The pixels written.
|
|
16
|
+
* @param {number} start Index of the line's first pixel (in pixels).
|
|
17
|
+
* @param {number} stride Pixels from one pixel of the line to the next.
|
|
18
|
+
* @param {number} length Pixels in the line.
|
|
19
|
+
* @param {number} radius Pixels each side averaged in.
|
|
20
|
+
*/
|
|
21
|
+
function blurLine(from, to, start, stride, length, radius) {
|
|
22
|
+
const span = radius * 2 + 1;
|
|
23
|
+
const last = length - 1;
|
|
24
|
+
const step = stride * 4;
|
|
25
|
+
for (let c = 0; c < 4; c++) {
|
|
26
|
+
const o = start * 4 + c;
|
|
27
|
+
let sum = from[o] * (radius + 1);
|
|
28
|
+
for (let i = 1; i <= radius; i++) sum += from[o + Math.min(i, last) * step];
|
|
29
|
+
for (let i = 0; i < length; i++) {
|
|
30
|
+
to[o + i * step] = sum / span;
|
|
31
|
+
sum += from[o + Math.min(i + radius + 1, last) * step] - from[o + Math.max(i - radius, 0) * step];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Blurs RGBA pixels in place: three box blurs each way, close to a
|
|
38
|
+
* Gaussian with a standard deviation of about `radius`.
|
|
39
|
+
*
|
|
40
|
+
* @param {Uint8ClampedArray} data The pixels, RGBA, row by row (an ImageData's `data`).
|
|
41
|
+
* @param {number} width Pixels across.
|
|
42
|
+
* @param {number} height Pixels down.
|
|
43
|
+
* @param {number} radius Pixels each side; 0 or less leaves them as they are.
|
|
44
|
+
*/
|
|
45
|
+
export function boxBlur(data, width, height, radius) {
|
|
46
|
+
const r = Math.round(radius);
|
|
47
|
+
if (r < 1) return;
|
|
48
|
+
// In floats: rounding to bytes between passes is slow and would drift.
|
|
49
|
+
const work = Float32Array.from(data);
|
|
50
|
+
const spare = new Float32Array(data.length);
|
|
51
|
+
for (let pass = 0; pass < 3; pass++) {
|
|
52
|
+
for (let y = 0; y < height; y++) blurLine(work, spare, y * width, 1, width, r);
|
|
53
|
+
for (let x = 0; x < width; x++) blurLine(spare, work, x, width, height, r);
|
|
54
|
+
}
|
|
55
|
+
data.set(work);
|
|
56
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** A running Canvas 2D effect. */
|
|
2
|
+
export interface Canvas2D {
|
|
3
|
+
/** Scaled so units are CSS px. */
|
|
4
|
+
ctx: CanvasRenderingContext2D;
|
|
5
|
+
canvas: HTMLCanvasElement;
|
|
6
|
+
/** The element's size, CSS px. */
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
/** True with prefers-reduced-motion. */
|
|
10
|
+
still: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Starts a Canvas 2D effect in `el`'s canvas: sized, looped while near the screen, still with reduced motion. */
|
|
14
|
+
export function createCanvas2D(
|
|
15
|
+
el: HTMLElement,
|
|
16
|
+
options: { draw(c: Canvas2D, now: number, dt: number): void; resize?(c: Canvas2D): void },
|
|
17
|
+
): { c: Canvas2D | null; stop(): void };
|
|
18
|
+
|
|
19
|
+
/** A repeating grey-noise tile. */
|
|
20
|
+
export function grainPattern(ctx: CanvasRenderingContext2D): CanvasPattern | null;
|
|
21
|
+
|
|
22
|
+
/** Lays grain over what has been drawn, and only there; with `shimmer`, shifted each call so it moves like film. */
|
|
23
|
+
export function grainOnto(c: Canvas2D, pattern: CanvasPattern | null, amount: number, shimmer?: boolean): void;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/canvas2d
|
|
3
|
+
*
|
|
4
|
+
* The set-up every Canvas 2D effect shares (the GPU ones have stage.js):
|
|
5
|
+
* the element's canvas kept its size at up to twice the pixel ratio,
|
|
6
|
+
* frames from the page's one loop while it is near the screen, one still
|
|
7
|
+
* frame with prefers-reduced-motion (redrawn on resize), and a grain tile
|
|
8
|
+
* for texturing strokes.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { prefersReducedMotion } from './device.js';
|
|
12
|
+
import { animate } from './loop.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {object} Canvas2D
|
|
16
|
+
* @property {CanvasRenderingContext2D} ctx The context, scaled so units are CSS px.
|
|
17
|
+
* @property {HTMLCanvasElement} canvas The canvas.
|
|
18
|
+
* @property {number} width The element's width, CSS px.
|
|
19
|
+
* @property {number} height The element's height, CSS px.
|
|
20
|
+
* @property {boolean} still True with prefers-reduced-motion.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Starts a Canvas 2D effect on an element (its first canvas is drawn into).
|
|
25
|
+
*
|
|
26
|
+
* @param {HTMLElement} el The element.
|
|
27
|
+
* @param {object} options The effect.
|
|
28
|
+
* @param {(c: Canvas2D, now: number, dt: number) => void} options.draw Draws a frame (`now` ms, `dt` seconds).
|
|
29
|
+
* @param {(c: Canvas2D) => void} [options.resize] After the canvas is resized.
|
|
30
|
+
* @returns {{ c: Canvas2D | null, stop: () => void }} The canvas (null without a 2D context) and a function that stops it.
|
|
31
|
+
*/
|
|
32
|
+
export function createCanvas2D(el, options) {
|
|
33
|
+
const canvas = el.querySelector('canvas');
|
|
34
|
+
const ctx = canvas?.getContext('2d', { alpha: true });
|
|
35
|
+
if (!canvas || !ctx) return { c: null, stop: () => {} };
|
|
36
|
+
/** @type {Canvas2D} */
|
|
37
|
+
const c = { ctx, canvas, width: 0, height: 0, still: prefersReducedMotion() };
|
|
38
|
+
|
|
39
|
+
/** Sizes the canvas to the element. */
|
|
40
|
+
const size = () => {
|
|
41
|
+
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
42
|
+
c.width = Math.max(1, el.clientWidth);
|
|
43
|
+
c.height = Math.max(1, el.clientHeight);
|
|
44
|
+
canvas.width = Math.floor(c.width * dpr);
|
|
45
|
+
canvas.height = Math.floor(c.height * dpr);
|
|
46
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
47
|
+
options.resize?.(c);
|
|
48
|
+
if (c.still) options.draw(c, performance.now(), 0);
|
|
49
|
+
};
|
|
50
|
+
const resizer = new ResizeObserver(size);
|
|
51
|
+
resizer.observe(el);
|
|
52
|
+
size();
|
|
53
|
+
const stopLoop = c.still ? () => {} : animate(el, (now, dt) => options.draw(c, now, dt));
|
|
54
|
+
return {
|
|
55
|
+
c,
|
|
56
|
+
stop: () => {
|
|
57
|
+
stopLoop();
|
|
58
|
+
resizer.disconnect();
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A repeating tile of grey noise, for laying grain over what has been drawn.
|
|
65
|
+
*
|
|
66
|
+
* @param {CanvasRenderingContext2D} ctx The context the pattern is for.
|
|
67
|
+
* @returns {CanvasPattern | null} The pattern.
|
|
68
|
+
*/
|
|
69
|
+
export function grainPattern(ctx) {
|
|
70
|
+
const size = 128;
|
|
71
|
+
const tile = document.createElement('canvas');
|
|
72
|
+
tile.width = size;
|
|
73
|
+
tile.height = size;
|
|
74
|
+
const t = tile.getContext('2d');
|
|
75
|
+
if (!t) return null;
|
|
76
|
+
const img = t.createImageData(size, size);
|
|
77
|
+
for (let i = 0; i < img.data.length; i += 4) {
|
|
78
|
+
const v = (Math.random() * 255) | 0;
|
|
79
|
+
img.data[i] = img.data[i + 1] = img.data[i + 2] = v;
|
|
80
|
+
img.data[i + 3] = 255;
|
|
81
|
+
}
|
|
82
|
+
t.putImageData(img, 0, 0);
|
|
83
|
+
return ctx.createPattern(tile, 'repeat');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Lays grain over what has been drawn, and only there (source-atop): the
|
|
88
|
+
* strokes get texture, the empty canvas stays empty.
|
|
89
|
+
*
|
|
90
|
+
* @param {Canvas2D} c The canvas.
|
|
91
|
+
* @param {CanvasPattern | null} pattern From grainPattern.
|
|
92
|
+
* @param {number} amount How strong, 0–1.
|
|
93
|
+
* @param {boolean} [shimmer=false] Shift the grain each call, so it moves like film rather than sitting still.
|
|
94
|
+
*/
|
|
95
|
+
export function grainOnto(c, pattern, amount, shimmer = false) {
|
|
96
|
+
if (!pattern || amount <= 0) return;
|
|
97
|
+
const { ctx } = c;
|
|
98
|
+
ctx.save();
|
|
99
|
+
ctx.globalCompositeOperation = 'source-atop';
|
|
100
|
+
ctx.globalAlpha = amount;
|
|
101
|
+
ctx.fillStyle = pattern;
|
|
102
|
+
if (shimmer) ctx.translate(Math.random() * 128, Math.random() * 128);
|
|
103
|
+
ctx.fillRect(-128, -128, c.width + 128, c.height + 128);
|
|
104
|
+
ctx.restore();
|
|
105
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface CausticsConfig {
|
|
4
|
+
/** The water and the light, in any order: the darkest is the deep water towards the edges, the lightest the light, the one between the floor. One or two colours work too. Default aqua `#2cc3d0`, deep teal `#0b7ea8`, sun `#f2fff9`. */
|
|
5
|
+
colors?: string[];
|
|
6
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
|
|
7
|
+
palette?: PaletteName | (string & {});
|
|
8
|
+
/** The ground the edges fall away into, and what shows without WebGL. Default: the deep water, darkened. */
|
|
9
|
+
background?: string;
|
|
10
|
+
/** How large the net of light is, 0.4–3: 2 doubles it. Default 1. */
|
|
11
|
+
scale?: number;
|
|
12
|
+
/** How fast the water moves, 0–3; 0 holds it still. Default 1. */
|
|
13
|
+
speed?: number;
|
|
14
|
+
/** How bright the light is, 0–2. Default 1. */
|
|
15
|
+
intensity?: number;
|
|
16
|
+
/** Faint pool tiles on the floor, bent by the water. Default false. */
|
|
17
|
+
tiles?: boolean;
|
|
18
|
+
/** Whether the pointer disturbs the water where it is. Default true. */
|
|
19
|
+
interactive?: boolean;
|
|
20
|
+
/** An area where the light is kept low behind copy: centre (0–1 from the top left), radius (a share of the height), depth (0–1). Default none; `{}` for the middle. */
|
|
21
|
+
quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The tones the water is drawn in, as hex. */
|
|
25
|
+
export interface CausticTones {
|
|
26
|
+
/** The floor, seen through shallow water. */
|
|
27
|
+
floor: string;
|
|
28
|
+
/** The deep water towards the edges. */
|
|
29
|
+
deep: string;
|
|
30
|
+
/** The light. */
|
|
31
|
+
light: string;
|
|
32
|
+
/** What the edges fall away into. */
|
|
33
|
+
background: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The tones from `colors` (or `palette`) in any order: darkest the deep water, lightest the light, the median the floor; `background` defaults to the deep, darkened. Throws a TypeError for a colour that is not hex. */
|
|
37
|
+
export function causticTones(config: CausticsConfig): CausticTones;
|
|
38
|
+
|
|
39
|
+
/** Starts the caustics in `el`'s canvas. Returns a function that stops it. */
|
|
40
|
+
export function caustics(el: HTMLElement, config: CausticsConfig): () => void;
|