@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,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/ink-field
|
|
3
|
+
*
|
|
4
|
+
* Ink in water, lit by a lamp: a small fluid sim (./ink-field/sim.js) that
|
|
5
|
+
* six slow emitters keep moving across the whole field, drawn as ink
|
|
6
|
+
* (./ink-field/shaders.js) whose thickness stands as a surface the lamp
|
|
7
|
+
* lights. The lamp follows the pointer and rests at the upper left; moving
|
|
8
|
+
* the pointer pushes the ink aside and leaves a small, faint trail of its
|
|
9
|
+
* own, and a still pointer does nothing. The field dims itself in the
|
|
10
|
+
* middle, behind the copy. Following sound (`audio`, ./audio.js), each
|
|
11
|
+
* kick in its low end stirs the whole field and the ink brightens with it.
|
|
12
|
+
*
|
|
13
|
+
* Adapted from Trirat's ink shader for LumiThemes (smoke-sim.ts and its
|
|
14
|
+
* LumiField driver), with his twelve palettes. The sim, the display and
|
|
15
|
+
* the driver's easing are his, unchanged.
|
|
16
|
+
*
|
|
17
|
+
* The colours change by setting the element's `data-colors` attribute to
|
|
18
|
+
* JSON `{ "colors": [three hex], "glint": "hex" }` (see `setColors` in the
|
|
19
|
+
* package index); the ink, already on screen, eases to them in about a
|
|
20
|
+
* second, as the palette is applied when it is drawn.
|
|
21
|
+
*
|
|
22
|
+
* Drawn at half the pixel ratio by default and scaled up: the field is
|
|
23
|
+
* soft. With prefers-reduced-motion (or `still`), the sim is run 240 steps
|
|
24
|
+
* ahead and one frame held. Without a float render target the canvas is
|
|
25
|
+
* removed and the palette's colours stand in, as still pools of light.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { createStage } from './stage.js';
|
|
29
|
+
import { VERTEX } from './gl.js';
|
|
30
|
+
import { parseColor } from './color.js';
|
|
31
|
+
import { canHover } from './device.js';
|
|
32
|
+
import { watchColors } from './mount.js';
|
|
33
|
+
import { pointerIn, pressAt } from './pointer.js';
|
|
34
|
+
import { PALETTES, findPalette } from './palettes.js';
|
|
35
|
+
import { colorPools } from './pools.js';
|
|
36
|
+
import { createInkSim } from './ink-field/sim.js';
|
|
37
|
+
import { DISPLAY } from './ink-field/shaders.js';
|
|
38
|
+
import { audioSignal } from './audio.js';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @typedef {object} InkFieldConfig
|
|
42
|
+
* @property {string} [palette='Lumetri'] One of the shared palettes (PALETTES in palettes.js; any case).
|
|
43
|
+
* @property {string[]} [colors] Three colours as CSS hex, in place of the palette's.
|
|
44
|
+
* @property {string} [glint] A glint colour, in place of the palette's (unused by the ink look).
|
|
45
|
+
* @property {boolean} [interactive=true] The pointer stirs the ink and the lamp follows it (devices that hover).
|
|
46
|
+
* @property {boolean} [drops=false] A press drops ink that blooms open (not on links, buttons or fields). Off by default: on a page-wide field, stray clicks would splash.
|
|
47
|
+
* @property {{ x?: number, y?: number }} [lamp] Where the lamp rests, 0–1 from the top left. Default `{ x: 0.14, y: 0.14 }`.
|
|
48
|
+
* @property {boolean} [still=false] Hold one frame of settled ink and never animate, as with reduced motion.
|
|
49
|
+
* @property {number} [scale=0.5] Share of the pixel ratio drawn at.
|
|
50
|
+
* @property {string} [audio] Follow the sound playing: `'page'` for whatever plays, or a CSS selector for an `<audio>`/`<video>` or a player's container (a WaveformPlayer). Kicks stir the ink and it brightens with the beat; while nothing plays it keeps its own motion.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
/** The palette when none is named, or the name is not one. */
|
|
55
|
+
const DEFAULT_PALETTE = 'Lumetri';
|
|
56
|
+
/** The water's colour where there is no ink: the display adds it to every pixel. */
|
|
57
|
+
const GROUND = '#08080a';
|
|
58
|
+
/** Where the lamp rests without a pointer, 0–1 from the top left. */
|
|
59
|
+
const LAMP_REST = { x: 0.14, y: 0.14 };
|
|
60
|
+
/** Steps run ahead for the still frame, and the clock it is drawn at. */
|
|
61
|
+
const STILL_FRAMES = 240;
|
|
62
|
+
const STILL_TIME = 40;
|
|
63
|
+
/** Rebuilding the sim on every pixel of a window drag would thrash: wait this long, ms. */
|
|
64
|
+
const RESIZE_WAIT = 150;
|
|
65
|
+
/** The longest step the sim takes, seconds: after a stall it resumes rather than jumps. */
|
|
66
|
+
const MAX_STEP = 0.033;
|
|
67
|
+
/** Share of the way to its target the pointer eases, and the lamp, per 1/60 s. */
|
|
68
|
+
const POINTER_EASE = 0.06;
|
|
69
|
+
const LAMP_EASE = 0.035;
|
|
70
|
+
/** The colours' easing time constant, seconds: about a second to new colours. */
|
|
71
|
+
const COLOR_EASE = 0.35;
|
|
72
|
+
/** How much brighter the ink gets on a kick, and with the sound's level. */
|
|
73
|
+
const KICK_GAIN = 0.4;
|
|
74
|
+
const LEVEL_GAIN = 0.15;
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Three colours from a list, looped where it has fewer.
|
|
79
|
+
*
|
|
80
|
+
* @param {string[]} colors At least one colour.
|
|
81
|
+
* @returns {string[]} Three colours.
|
|
82
|
+
*/
|
|
83
|
+
function three(colors) {
|
|
84
|
+
return [0, 1, 2].map((i) => colors[i % colors.length]);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The settings an ink field runs with: its colours (the named palette, or
|
|
89
|
+
* the colours given over it) and every default filled in.
|
|
90
|
+
*
|
|
91
|
+
* @param {InkFieldConfig} [config] The config.
|
|
92
|
+
* @returns {{ colors: string[], glint: string, lamp: { x: number, y: number }, interactive: boolean, drops: boolean, still: boolean, scale: number }}
|
|
93
|
+
* Three colours and a glint, the lamp's rest (0–1 from the top left), and the rest.
|
|
94
|
+
*/
|
|
95
|
+
export function inkFieldSettings(config = {}) {
|
|
96
|
+
const palette = findPalette(config.palette) ?? PALETTES[DEFAULT_PALETTE];
|
|
97
|
+
return {
|
|
98
|
+
colors: config.colors?.length ? three(config.colors) : [...palette.colors],
|
|
99
|
+
glint: config.glint ?? palette.glint,
|
|
100
|
+
lamp: { x: config.lamp?.x ?? LAMP_REST.x, y: config.lamp?.y ?? LAMP_REST.y },
|
|
101
|
+
interactive: config.interactive !== false,
|
|
102
|
+
drops: config.drops === true,
|
|
103
|
+
still: config.still === true,
|
|
104
|
+
scale: config.scale ?? 0.5,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The stand-in without a float render target: the colours as still pools
|
|
110
|
+
* of light on the water's colour.
|
|
111
|
+
*
|
|
112
|
+
* @param {string[]} colors The colours.
|
|
113
|
+
* @returns {string} A CSS `background`.
|
|
114
|
+
*/
|
|
115
|
+
export function inkFallback(colors) {
|
|
116
|
+
return colorPools(colors, GROUND);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The colours as the display takes them: three, then the glint.
|
|
121
|
+
*
|
|
122
|
+
* @param {string[]} colors Three hex colours.
|
|
123
|
+
* @param {string} glint A hex colour.
|
|
124
|
+
* @returns {Float32Array} Twelve channels, 0–1.
|
|
125
|
+
*/
|
|
126
|
+
function channels(colors, glint) {
|
|
127
|
+
return new Float32Array([...colors, glint].flatMap(parseColor));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Starts an ink field on an element: an opaque canvas inside it, filling it.
|
|
132
|
+
*
|
|
133
|
+
* @param {HTMLElement} el The element (its first canvas is drawn into).
|
|
134
|
+
* @param {InkFieldConfig} config The settings.
|
|
135
|
+
* @returns {() => void} Stops it.
|
|
136
|
+
*/
|
|
137
|
+
export function inkField(el, config) {
|
|
138
|
+
if (config.palette && !findPalette(config.palette)) {
|
|
139
|
+
console.warn('[effects-astro]', `No palette "${config.palette}"; using ${DEFAULT_PALETTE}`);
|
|
140
|
+
}
|
|
141
|
+
const settings = inkFieldSettings(config);
|
|
142
|
+
let glint = settings.glint;
|
|
143
|
+
/** The colours drawn now, easing towards `target`. */
|
|
144
|
+
const colours = channels(settings.colors, glint);
|
|
145
|
+
/** @type {Float32Array} */
|
|
146
|
+
let target = colours.slice();
|
|
147
|
+
/** Where the lamp rests, 0–1, y up as the shader has it. */
|
|
148
|
+
const rest = { x: settings.lamp.x, y: 1 - settings.lamp.y };
|
|
149
|
+
/** The lamp now, easing towards the pointer or its rest. */
|
|
150
|
+
const lamp = { ...rest };
|
|
151
|
+
/** The eased pointer, and where it is headed, 0–1, y up. */
|
|
152
|
+
const mouse = { x: 0, y: 0, tx: 0, ty: 0 };
|
|
153
|
+
/** True while the pointer is over the element; false, nothing stirs. */
|
|
154
|
+
let pointing = false;
|
|
155
|
+
const hover = settings.interactive && canHover();
|
|
156
|
+
let clock = 0;
|
|
157
|
+
let timer = 0;
|
|
158
|
+
/** The canvas size the sim was last fitted to (0 before the first fit). */
|
|
159
|
+
let fitted = { width: 0, height: 0 };
|
|
160
|
+
/** @type {import('./ink-field/sim.js').InkSim | null} */
|
|
161
|
+
let sim = null;
|
|
162
|
+
/** @type {import('./audio.js').AudioSignal | null} */
|
|
163
|
+
let sound = null;
|
|
164
|
+
/** The kicks already answered, and how much brighter the ink is drawn now. */
|
|
165
|
+
let beats = 0;
|
|
166
|
+
let gain = 0;
|
|
167
|
+
|
|
168
|
+
const { stage, stop } = createStage(el, {
|
|
169
|
+
fragment: DISPLAY,
|
|
170
|
+
vertex: VERTEX,
|
|
171
|
+
alpha: false,
|
|
172
|
+
scale: settings.scale,
|
|
173
|
+
still: settings.still,
|
|
174
|
+
setup({ gl, still }) {
|
|
175
|
+
const made = createInkSim(gl);
|
|
176
|
+
if (!made) return false;
|
|
177
|
+
sim = made;
|
|
178
|
+
if (config.audio && !still) sound = audioSignal(config.audio);
|
|
179
|
+
return () => {
|
|
180
|
+
clearTimeout(timer);
|
|
181
|
+
sound?.stop();
|
|
182
|
+
made.destroy();
|
|
183
|
+
};
|
|
184
|
+
},
|
|
185
|
+
resize(s) {
|
|
186
|
+
const current = sim;
|
|
187
|
+
const { width, height } = s.canvas;
|
|
188
|
+
// The observer's first report repeats the size the stage started at.
|
|
189
|
+
if (!current || (width === fitted.width && height === fitted.height)) return;
|
|
190
|
+
/** Fits the sim to the canvas's shape; a still field settles again. */
|
|
191
|
+
const fit = () => {
|
|
192
|
+
current.resize(width, height);
|
|
193
|
+
if (s.still) current.warm(STILL_FRAMES);
|
|
194
|
+
};
|
|
195
|
+
// The first fit is now, before the first frame (the stage redraws a
|
|
196
|
+
// still field straight after); later ones wait for the resizing to stop.
|
|
197
|
+
const first = !fitted.width;
|
|
198
|
+
fitted = { width, height };
|
|
199
|
+
if (first) {
|
|
200
|
+
fit();
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
clearTimeout(timer);
|
|
204
|
+
timer = window.setTimeout(() => {
|
|
205
|
+
fit();
|
|
206
|
+
if (s.still) s.redraw();
|
|
207
|
+
}, RESIZE_WAIT);
|
|
208
|
+
},
|
|
209
|
+
update({ still }, now, frameDt) {
|
|
210
|
+
if (still || !sim || !frameDt) return;
|
|
211
|
+
const dt = Math.min(frameDt, MAX_STEP);
|
|
212
|
+
clock += dt;
|
|
213
|
+
|
|
214
|
+
// The sound: each kick stirs the field, and the ink brightens with it.
|
|
215
|
+
const heard = sound?.read(now, frameDt) ?? null;
|
|
216
|
+
gain = heard ? heard.on * (heard.kick * KICK_GAIN + heard.level * LEVEL_GAIN) : 0;
|
|
217
|
+
if (heard && heard.beats !== beats) {
|
|
218
|
+
beats = heard.beats;
|
|
219
|
+
sim.kick(heard.kick * heard.on);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// The pointer eases, by the clock; its inertia is the ink's input.
|
|
223
|
+
if (hover) {
|
|
224
|
+
const p = pointerIn(el);
|
|
225
|
+
if (p.inside) {
|
|
226
|
+
mouse.tx = p.x;
|
|
227
|
+
mouse.ty = p.y;
|
|
228
|
+
// Arriving (or coming back), the pointer starts where it is:
|
|
229
|
+
// no streak across the field from wherever it left.
|
|
230
|
+
if (!pointing) {
|
|
231
|
+
mouse.x = mouse.tx;
|
|
232
|
+
mouse.y = mouse.ty;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
pointing = p.inside;
|
|
236
|
+
const k = 1 - Math.pow(1 - POINTER_EASE, dt * 60);
|
|
237
|
+
const px = mouse.x;
|
|
238
|
+
const py = mouse.y;
|
|
239
|
+
mouse.x += (mouse.tx - mouse.x) * k;
|
|
240
|
+
mouse.y += (mouse.ty - mouse.y) * k;
|
|
241
|
+
sim.stir(mouse.x, mouse.y, mouse.x - px, mouse.y - py, dt);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// The lamp trails the pointer a little slower than the pointer trails the hand.
|
|
245
|
+
const goal = pointing ? { x: mouse.tx, y: mouse.ty } : rest;
|
|
246
|
+
const lampEase = 1 - Math.pow(1 - LAMP_EASE, dt * 60);
|
|
247
|
+
lamp.x += (goal.x - lamp.x) * lampEase;
|
|
248
|
+
lamp.y += (goal.y - lamp.y) * lampEase;
|
|
249
|
+
|
|
250
|
+
// About a second to the new colours however fast the screen draws.
|
|
251
|
+
const ease = 1 - Math.exp(-dt / COLOR_EASE);
|
|
252
|
+
for (let i = 0; i < colours.length; i++) colours[i] += (target[i] - colours[i]) * ease;
|
|
253
|
+
|
|
254
|
+
sim.step(dt);
|
|
255
|
+
},
|
|
256
|
+
uniforms({ gl, uniform, still }) {
|
|
257
|
+
if (!sim) return;
|
|
258
|
+
gl.uniform1i(uniform('uDye'), 0);
|
|
259
|
+
const [tx, ty] = sim.bindDye(0);
|
|
260
|
+
gl.uniform1f(uniform('uTime'), still ? STILL_TIME : clock);
|
|
261
|
+
gl.uniform3fv(uniform('uPal'), colours.subarray(0, 9));
|
|
262
|
+
gl.uniform3fv(uniform('uGlint'), colours.subarray(9, 12));
|
|
263
|
+
gl.uniform2f(uniform('uDyeTxl'), tx, ty);
|
|
264
|
+
const light = still ? rest : lamp;
|
|
265
|
+
gl.uniform2f(uniform('uLight'), light.x, light.y);
|
|
266
|
+
gl.uniform1f(uniform('uGain'), gain);
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
if (!stage) {
|
|
271
|
+
// No WebGL, or no float render target: the colours, still.
|
|
272
|
+
el.style.background = inkFallback(settings.colors);
|
|
273
|
+
const unwatch = watchColors(el, (next) => {
|
|
274
|
+
el.style.background = inkFallback(three(next.colors));
|
|
275
|
+
});
|
|
276
|
+
return () => {
|
|
277
|
+
stop();
|
|
278
|
+
unwatch();
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// New colours: the ink heads for them. Those set before it started are
|
|
283
|
+
// its colours from the first frame; a still field takes them at once.
|
|
284
|
+
let started = false;
|
|
285
|
+
const unwatch = watchColors(el, (next) => {
|
|
286
|
+
glint = next.glint ?? glint;
|
|
287
|
+
target = channels(three(next.colors), glint);
|
|
288
|
+
if (!started || stage.still) colours.set(target);
|
|
289
|
+
if (stage.still && started) stage.redraw();
|
|
290
|
+
});
|
|
291
|
+
started = true;
|
|
292
|
+
if (stage.still) stage.redraw();
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Drops ink where the element is pressed.
|
|
296
|
+
*
|
|
297
|
+
* @param {PointerEvent} e The press.
|
|
298
|
+
*/
|
|
299
|
+
const press = (e) => {
|
|
300
|
+
const at = pressAt(el, e);
|
|
301
|
+
if (at) sim?.drop(at.x, at.y);
|
|
302
|
+
};
|
|
303
|
+
const drops = settings.drops && !stage.still;
|
|
304
|
+
if (drops) document.addEventListener('pointerdown', press, { passive: true });
|
|
305
|
+
|
|
306
|
+
return () => {
|
|
307
|
+
stop();
|
|
308
|
+
unwatch();
|
|
309
|
+
if (drops) document.removeEventListener('pointerdown', press);
|
|
310
|
+
};
|
|
311
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface InkConfig {
|
|
4
|
+
/** The ink's colours, as CSS hex, looped through as it is laid. Default the palette's, or Peacock's. */
|
|
5
|
+
colors?: string[];
|
|
6
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
|
|
7
|
+
palette?: PaletteName | (string & {});
|
|
8
|
+
/** How much ink a stroke leaves, 0 for none (strokes still push). Default 0.35. */
|
|
9
|
+
trail?: number;
|
|
10
|
+
/** Whether a click or tap drops a bloom of ink. Default true. */
|
|
11
|
+
drops?: boolean;
|
|
12
|
+
/** How hard the pointer pushes. Default 1. */
|
|
13
|
+
force?: number;
|
|
14
|
+
/** Swirl kept alive between strokes. Default 28. */
|
|
15
|
+
curl?: number;
|
|
16
|
+
/** How fast the ink fades, per second. Default 0.55. */
|
|
17
|
+
fade?: number;
|
|
18
|
+
/** How much the ink spreads and softens as it moves, 0–1. Default 0.35. */
|
|
19
|
+
diffusion?: number;
|
|
20
|
+
/** The size of a stroke's splats and of a drop, 1 as standard. */
|
|
21
|
+
radius?: number;
|
|
22
|
+
/** How fast the motion dies away, per second (lower flows on longer). Default 0.25. */
|
|
23
|
+
momentum?: number;
|
|
24
|
+
/** The ink's strongest cover, 0–1. Default 1. */
|
|
25
|
+
opacity?: number;
|
|
26
|
+
/** Clicks on these are theirs, not the ink's. Default: links, buttons, form fields and `[data-effect-ignore]`. */
|
|
27
|
+
ignore?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Starts ink in `el`'s canvas (transparent where there is none). Returns a function that stops it. */
|
|
31
|
+
export function ink(el: HTMLElement, config: InkConfig): () => void;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/ink
|
|
3
|
+
*
|
|
4
|
+
* Ink in water, over whatever is behind it: the fluid of ./fluid.js drawn
|
|
5
|
+
* on a transparent canvas. The pointer stirs it and trails ink, a click
|
|
6
|
+
* drops a bloom that bursts open and curls; where there is no ink the
|
|
7
|
+
* canvas is clear, so it lays over a hero, a card or an image.
|
|
8
|
+
*
|
|
9
|
+
* The colours change by setting the element's `data-colors` attribute to
|
|
10
|
+
* JSON `{ "colors": [hex, …] }` (see `setColors` in the package index).
|
|
11
|
+
*
|
|
12
|
+
* Needs WebGL 2; without it (and with prefers-reduced-motion) the canvas
|
|
13
|
+
* is removed and nothing is drawn.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { createStage } from './stage.js';
|
|
17
|
+
import { parseColor } from './color.js';
|
|
18
|
+
import { prefersReducedMotion } from './device.js';
|
|
19
|
+
import { watchColors } from './mount.js';
|
|
20
|
+
import { createFluid, stir } from './fluid.js';
|
|
21
|
+
import { PALETTES, pickColors } from './palettes.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {object} InkConfig
|
|
25
|
+
* @property {string[]} [colors] The ink's colours, as CSS hex, looped through as it is laid (default the palette's, or Peacock's).
|
|
26
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
|
|
27
|
+
* @property {number} [trail=0.35] How much ink a stroke leaves, 0 for none (strokes still push).
|
|
28
|
+
* @property {boolean} [drops=true] Whether a click or tap drops a bloom of ink.
|
|
29
|
+
* @property {number} [force=1] How hard the pointer pushes.
|
|
30
|
+
* @property {number} [curl=28] Swirl kept alive between strokes.
|
|
31
|
+
* @property {number} [fade=0.55] How fast the ink fades, per second.
|
|
32
|
+
* @property {number} [diffusion=0.35] How much the ink spreads and softens as it moves, 0–1.
|
|
33
|
+
* @property {number} [radius=1] The size of a stroke's splats and of a drop, 1 as standard.
|
|
34
|
+
* @property {number} [momentum=0.25] How fast the motion dies away, per second (lower flows on longer).
|
|
35
|
+
* @property {number} [opacity=1] The ink's strongest cover, 0–1.
|
|
36
|
+
* @property {string} [ignore] Clicks on these are theirs, not the ink's (default: links, buttons, form fields, `[data-effect-ignore]`).
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/** Shows the ink: its colour at full strength, as opaque as there is ink. */
|
|
40
|
+
const SHOW = `
|
|
41
|
+
precision highp float;
|
|
42
|
+
uniform vec2 uRes;
|
|
43
|
+
uniform sampler2D uDye;
|
|
44
|
+
uniform float uOpacity;
|
|
45
|
+
void main() {
|
|
46
|
+
vec3 dye = texture2D(uDye, gl_FragCoord.xy / uRes).rgb;
|
|
47
|
+
float ink = max(dye.r, max(dye.g, dye.b));
|
|
48
|
+
float cover = (1.0 - exp(-ink * 2.5)) * uOpacity;
|
|
49
|
+
// Premultiplied, as the canvas composites.
|
|
50
|
+
gl_FragColor = vec4(dye / max(ink, 1e-4) * cover, cover);
|
|
51
|
+
}
|
|
52
|
+
`;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Starts ink on an element: a transparent canvas inside it, filling it.
|
|
56
|
+
*
|
|
57
|
+
* @param {HTMLElement} el The element (its first canvas is drawn into).
|
|
58
|
+
* @param {InkConfig} config The settings.
|
|
59
|
+
* @returns {() => void} Stops it.
|
|
60
|
+
*/
|
|
61
|
+
export function ink(el, config) {
|
|
62
|
+
// Ink is motion or nothing: with reduced motion there is nothing to show.
|
|
63
|
+
if (prefersReducedMotion()) {
|
|
64
|
+
el.querySelector('canvas')?.remove();
|
|
65
|
+
return () => {};
|
|
66
|
+
}
|
|
67
|
+
/** @type {import('./fluid.js').Fluid | null} */
|
|
68
|
+
let fluid = null;
|
|
69
|
+
/** @type {ReturnType<typeof stir> | null} */
|
|
70
|
+
let stirrer = null;
|
|
71
|
+
|
|
72
|
+
const { stage, stop } = createStage(el, {
|
|
73
|
+
fragment: SHOW,
|
|
74
|
+
scale: 0.5,
|
|
75
|
+
webgl1: false,
|
|
76
|
+
setup({ gl, uniform, program }) {
|
|
77
|
+
fluid = gl instanceof WebGL2RenderingContext ? createFluid(gl, { curl: config.curl, inkFade: config.fade, diffusion: config.diffusion ?? 0.35, velocityFade: config.momentum }) : null;
|
|
78
|
+
if (!fluid) return false;
|
|
79
|
+
gl.useProgram(program);
|
|
80
|
+
gl.uniform1i(uniform('uDye'), 3);
|
|
81
|
+
gl.uniform1f(uniform('uOpacity'), config.opacity ?? 1);
|
|
82
|
+
const s = stir(fluid, el, {
|
|
83
|
+
colors: pickColors(config, [...PALETTES.Peacock.colors]).map(parseColor),
|
|
84
|
+
trail: config.trail ?? 0.35,
|
|
85
|
+
drops: config.drops,
|
|
86
|
+
force: config.force,
|
|
87
|
+
radius: config.radius,
|
|
88
|
+
ignore: config.ignore,
|
|
89
|
+
});
|
|
90
|
+
stirrer = s;
|
|
91
|
+
return () => s.destroy();
|
|
92
|
+
},
|
|
93
|
+
resize({ canvas }) {
|
|
94
|
+
fluid?.resize(canvas.width / canvas.height);
|
|
95
|
+
},
|
|
96
|
+
update(_stage, _now, dt) {
|
|
97
|
+
stirrer?.update(dt);
|
|
98
|
+
if (dt > 0) fluid?.step(dt);
|
|
99
|
+
fluid?.bind(3, 4);
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
if (!stage) return stop;
|
|
103
|
+
|
|
104
|
+
const unwatch = watchColors(el, (next) => stirrer?.setColors(next.colors.map(parseColor)));
|
|
105
|
+
return () => {
|
|
106
|
+
stop();
|
|
107
|
+
unwatch();
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GL } from './gl.js';
|
|
2
|
+
|
|
3
|
+
/** Instanced drawing calls, the same on WebGL 1 and 2. */
|
|
4
|
+
export interface Instancing {
|
|
5
|
+
/** How many instances share each value of an attribute (0: it advances every vertex). */
|
|
6
|
+
divisor(location: number, divisor: number): void;
|
|
7
|
+
/** Draws `count` vertices from `first`, `instances` times. */
|
|
8
|
+
draw(mode: number, first: number, count: number, instances: number): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Instanced drawing for a context: WebGL 2's own, or ANGLE_instanced_arrays on WebGL 1; null without either. */
|
|
12
|
+
export function instancing(gl: GL): Instancing | null;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/instancing
|
|
3
|
+
*
|
|
4
|
+
* Instanced drawing on either WebGL: built into WebGL 2, through the
|
|
5
|
+
* ANGLE_instanced_arrays extension on WebGL 1. One draw puts the same mesh
|
|
6
|
+
* down many times, each copy reading its own values from attributes that
|
|
7
|
+
* advance once an instance.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {object} Instancing
|
|
12
|
+
* @property {(location: number, divisor: number) => void} divisor How many
|
|
13
|
+
* instances share each value of an attribute (0: it advances every vertex).
|
|
14
|
+
* @property {(mode: number, first: number, count: number, instances: number) => void} draw
|
|
15
|
+
* Draws `count` vertices from `first`, `instances` times.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Instanced drawing for a context, where it has it.
|
|
20
|
+
*
|
|
21
|
+
* @param {WebGLRenderingContext | WebGL2RenderingContext} gl The context.
|
|
22
|
+
* @returns {Instancing | null} The calls, or null where there is no instancing.
|
|
23
|
+
*/
|
|
24
|
+
export function instancing(gl) {
|
|
25
|
+
if (typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext) {
|
|
26
|
+
return {
|
|
27
|
+
divisor: (location, divisor) => gl.vertexAttribDivisor(location, divisor),
|
|
28
|
+
draw: (mode, first, count, instances) => gl.drawArraysInstanced(mode, first, count, instances),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const ext = gl.getExtension('ANGLE_instanced_arrays');
|
|
32
|
+
if (!ext) return null;
|
|
33
|
+
return {
|
|
34
|
+
divisor: (location, divisor) => ext.vertexAttribDivisorANGLE(location, divisor),
|
|
35
|
+
draw: (mode, first, count, instances) => ext.drawArraysInstancedANGLE(mode, first, count, instances),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type Vec3 = [number, number, number];
|
|
2
|
+
|
|
3
|
+
/** True isometric elevation, about 35.26°: the three axes foreshorten equally. */
|
|
4
|
+
export const ISO_PITCH: number;
|
|
5
|
+
|
|
6
|
+
export interface IsoCameraOptions {
|
|
7
|
+
/** The point at the middle of the view. Default the origin. */
|
|
8
|
+
target?: Vec3;
|
|
9
|
+
/** Heading the camera looks from, radians: 0 from +z, π/2 from +x. Default π/4. */
|
|
10
|
+
yaw?: number;
|
|
11
|
+
/** Elevation, radians: 0 level, π/2 straight down. Default ISO_PITCH. */
|
|
12
|
+
pitch?: number;
|
|
13
|
+
/** The world height the view spans, top to bottom. Default 10. */
|
|
14
|
+
height?: number;
|
|
15
|
+
/** The view's width over its height. Default 1. */
|
|
16
|
+
aspect?: number;
|
|
17
|
+
/** How far either side of the target, along the view, is kept. Default 100. */
|
|
18
|
+
depth?: number;
|
|
19
|
+
/** Moves the picture across the screen, in clip units. Default [0, 0]. */
|
|
20
|
+
shift?: [number, number];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface IsoCamera {
|
|
24
|
+
/** World to clip space, column-major (for `uniformMatrix4fv`). */
|
|
25
|
+
matrix: Float32Array;
|
|
26
|
+
/** The unit direction from the target towards the camera. */
|
|
27
|
+
eye: Vec3;
|
|
28
|
+
/** Where a world point lands, in clip space. */
|
|
29
|
+
project(p: Vec3): [number, number, number];
|
|
30
|
+
/** The world point at height `h` (default 0) under clip position x, y. */
|
|
31
|
+
ground(x: number, y: number, h?: number): Vec3;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Makes an orthographic isometric camera (y-up world, WebGL clip space). */
|
|
35
|
+
export function isoCamera(options?: IsoCameraOptions): IsoCamera;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/iso-camera
|
|
3
|
+
*
|
|
4
|
+
* An orthographic camera for isometric scenes: it looks down on a target
|
|
5
|
+
* from a compass heading (`yaw`) and an elevation (`pitch`), with no
|
|
6
|
+
* perspective, so parallel edges stay parallel and a grid reads as the
|
|
7
|
+
* familiar diamond. Gives the matrix a vertex shader multiplies by,
|
|
8
|
+
* where a point lands on screen, and which point of the ground lies under
|
|
9
|
+
* a screen position (for picking).
|
|
10
|
+
*
|
|
11
|
+
* The world is y-up. Clip space is WebGL's: x and y −1 to 1, z −1 nearest.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** True isometric elevation, about 35.26°: the three axes foreshorten equally. */
|
|
15
|
+
export const ISO_PITCH = Math.atan(1 / Math.SQRT2);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {[number, number, number]} Vec3
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {object} IsoCameraOptions
|
|
23
|
+
* @property {Vec3} [target=[0, 0, 0]] The point at the middle of the view.
|
|
24
|
+
* @property {number} [yaw=π/4] Heading the camera looks from, radians: 0 from +z, π/2 from +x.
|
|
25
|
+
* @property {number} [pitch=ISO_PITCH] Elevation, radians: 0 level, π/2 straight down.
|
|
26
|
+
* @property {number} [height=10] The world height the view spans, top to bottom.
|
|
27
|
+
* @property {number} [aspect=1] The view's width over its height.
|
|
28
|
+
* @property {number} [depth=100] How far either side of the target, along the view, is kept.
|
|
29
|
+
* @property {[number, number]} [shift=[0, 0]] Moves the picture across the screen, in clip units.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {object} IsoCamera
|
|
34
|
+
* @property {Float32Array} matrix World to clip space, column-major (for `uniformMatrix4fv`).
|
|
35
|
+
* @property {Vec3} eye The unit direction from the target towards the camera.
|
|
36
|
+
* @property {(p: Vec3) => [number, number, number]} project Where a world point lands, in clip space.
|
|
37
|
+
* @property {(x: number, y: number, h?: number) => Vec3} ground The world point at height `h` (default 0) under clip position x, y.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Makes an isometric camera.
|
|
42
|
+
*
|
|
43
|
+
* @param {IsoCameraOptions} [options] Where it looks from and what it frames.
|
|
44
|
+
* @returns {IsoCamera} The camera.
|
|
45
|
+
*/
|
|
46
|
+
export function isoCamera({ target = [0, 0, 0], yaw = Math.PI / 4, pitch = ISO_PITCH, height = 10, aspect = 1, depth = 100, shift = [0, 0] } = {}) {
|
|
47
|
+
const cp = Math.cos(pitch);
|
|
48
|
+
/** @type {Vec3} */
|
|
49
|
+
const d = [Math.sin(yaw) * cp, Math.sin(pitch), Math.cos(yaw) * cp];
|
|
50
|
+
// Right is the view direction crossed with up; up on screen completes the frame.
|
|
51
|
+
const rl = Math.hypot(d[2], d[0]) || 1;
|
|
52
|
+
/** @type {Vec3} */
|
|
53
|
+
const r = [d[2] / rl, 0, -d[0] / rl];
|
|
54
|
+
/** @type {Vec3} */
|
|
55
|
+
const u = [r[1] * -d[2] - r[2] * -d[1], r[2] * -d[0] - r[0] * -d[2], r[0] * -d[1] - r[1] * -d[0]];
|
|
56
|
+
const hh = height / 2;
|
|
57
|
+
const hw = hh * aspect;
|
|
58
|
+
/**
|
|
59
|
+
* The dot product.
|
|
60
|
+
*
|
|
61
|
+
* @param {Vec3} a The first.
|
|
62
|
+
* @param {Vec3} b The second.
|
|
63
|
+
* @returns {number} a · b.
|
|
64
|
+
*/
|
|
65
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
66
|
+
const rows = [
|
|
67
|
+
[r[0] / hw, r[1] / hw, r[2] / hw, -dot(r, target) / hw + shift[0]],
|
|
68
|
+
[u[0] / hh, u[1] / hh, u[2] / hh, -dot(u, target) / hh + shift[1]],
|
|
69
|
+
[-d[0] / depth, -d[1] / depth, -d[2] / depth, dot(d, target) / depth],
|
|
70
|
+
[0, 0, 0, 1],
|
|
71
|
+
];
|
|
72
|
+
const matrix = new Float32Array(16);
|
|
73
|
+
for (let c = 0; c < 4; c++) for (let row = 0; row < 4; row++) matrix[c * 4 + row] = rows[row][c];
|
|
74
|
+
return {
|
|
75
|
+
matrix,
|
|
76
|
+
eye: d,
|
|
77
|
+
project: (p) => /** @type {Vec3} */ ([0, 1, 2].map((i) => rows[i][0] * p[0] + rows[i][1] * p[1] + rows[i][2] * p[2] + rows[i][3])),
|
|
78
|
+
ground(x, y, h = 0) {
|
|
79
|
+
const a = (x - shift[0]) * hw;
|
|
80
|
+
const b = (y - shift[1]) * hh;
|
|
81
|
+
const k = (h - target[1] - u[1] * b) / (d[1] || 1e-6);
|
|
82
|
+
return /** @type {Vec3} */ ([0, 1, 2].map((i) => target[i] + r[i] * a + u[i] * b + d[i] * k));
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Car, Job, Traffic } from './traffic.js';
|
|
2
|
+
import type { Roads } from './roads.js';
|
|
3
|
+
|
|
4
|
+
/** Floats an instance: place (x, z, heading, lift), paint (r, g, b, taxi: 1, car: 0, neither: −1), state (brake, squash, scale, fade). */
|
|
5
|
+
export const INSTANCE: number;
|
|
6
|
+
/** How much larger than its model a pin is drawn. */
|
|
7
|
+
export const PIN_SCALE: number;
|
|
8
|
+
/** How much larger than its model a person is drawn. */
|
|
9
|
+
export const PERSON_SCALE: number;
|
|
10
|
+
|
|
11
|
+
/** An eased drop with one small bounce: 0 at the start, 1 landed. */
|
|
12
|
+
export function bounce(t: number): number;
|
|
13
|
+
|
|
14
|
+
/** Writes the cars' instances into `out`, INSTANCE floats a car; taxis wear the accent. */
|
|
15
|
+
export function carInstances(cars: Car[], look: { accent: number[]; neutrals: number[][] }, out: Float32Array): void;
|
|
16
|
+
|
|
17
|
+
/** The people's instances: a rider by each pin, and people on the zebras where they are crossing. */
|
|
18
|
+
export function peopleInstances(traffic: Traffic, roads: Roads): number[];
|
|
19
|
+
|
|
20
|
+
/** A pin on show. */
|
|
21
|
+
export interface PinState {
|
|
22
|
+
job: Job;
|
|
23
|
+
scale: number;
|
|
24
|
+
lift: number;
|
|
25
|
+
fade: number;
|
|
26
|
+
pulse: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The pins to draw: dropping and bobbing while their taxi comes, popping away as it picks up or is sent elsewhere. */
|
|
30
|
+
export function pinStates(jobs: Job[], clock: number): PinState[];
|