@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,650 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/topography
|
|
3
|
+
*
|
|
4
|
+
* A survey map that never sits still: the contour lines of a slowly
|
|
5
|
+
* evolving landscape, drifting as its hills rise and settle. Every fifth
|
|
6
|
+
* line is an index line, drawn heavier, as on a printed map; where the
|
|
7
|
+
* lines crowd on a steep slope the fine ones fade first, so cliffs read as
|
|
8
|
+
* bands of index lines rather than solid ink. Under the pointer the ground
|
|
9
|
+
* swells a little and its lines glow. Optional elevation numbers sit on a
|
|
10
|
+
* few index lines, the line broken around them, and travel with them.
|
|
11
|
+
*
|
|
12
|
+
* One fragment shader: the height is four octaves of value noise with
|
|
13
|
+
* analytic gradients, so each line is exactly `thickness` CSS px wide at
|
|
14
|
+
* any slope or pixel ratio, and anti-aliased over one pixel. The noise
|
|
15
|
+
* reads a small lattice texture of random values, one fetch an octave, and
|
|
16
|
+
* the same lattice is read on the CPU to place the numbers on the lines.
|
|
17
|
+
*
|
|
18
|
+
* Opaque: it is the whole background of what it fills. Without WebGL the
|
|
19
|
+
* canvas is removed and `background` shows. With prefers-reduced-motion,
|
|
20
|
+
* one still frame.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { createStage } from './stage.js';
|
|
24
|
+
import { GLSL_OKLAB, parseColor, toOklab } from './color.js';
|
|
25
|
+
import { GLSL_QUIET, quietUniform } from './glsl.js';
|
|
26
|
+
import { GLSL_RAMP } from './ramp.js';
|
|
27
|
+
import { gradientUniform, MAX_COLORS } from './dot-surface.js';
|
|
28
|
+
import { canHover } from './device.js';
|
|
29
|
+
import { pointerIn } from './pointer.js';
|
|
30
|
+
import { watchColors } from './mount.js';
|
|
31
|
+
import { pickColors } from './palettes.js';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {object} TopographyConfig
|
|
35
|
+
* @property {string[]} [colors] The lines' colour, or a gradient by elevation from the valleys to the peaks (up to five), blended in Oklab. Default a pale blue.
|
|
36
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), as a gradient by elevation, when there are no `colors`.
|
|
37
|
+
* @property {string} [background='#0c1a2b'] The ground the lines are drawn on.
|
|
38
|
+
* @property {number} [density=24] About how many lines cross the screen from top to bottom, 6–60.
|
|
39
|
+
* @property {number} [thickness=1] The lines' width, CSS px, 0.5–3; index lines are twice as wide.
|
|
40
|
+
* @property {number} [major=5] Every n-th line is an index line, drawn heavier, 0–10; 0 for none.
|
|
41
|
+
* @property {number} [scale=1] How large the hills are, 0.4–3.
|
|
42
|
+
* @property {number} [speed=1] How fast the land drifts, 0–4; 0 holds it still.
|
|
43
|
+
* @property {number} [glow=1] How strongly the lines glow under the pointer, 0–2; 0 for none.
|
|
44
|
+
* @property {boolean} [interactive=true] Whether the pointer raises the ground and lights its lines.
|
|
45
|
+
* @property {boolean} [labels=false] Small elevation numbers on a few index lines.
|
|
46
|
+
* @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
|
|
47
|
+
* An area kept dim behind copy: centre (0–1 from the top left), radius (a
|
|
48
|
+
* share of the height) and depth (0–1). The middle with `{}`.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/** The lattice's size: random values on a square this many cells a side, repeating. */
|
|
52
|
+
export const LATTICE = 64;
|
|
53
|
+
/** Lattice cells across the screen's height for the largest octave, at `scale` 1. */
|
|
54
|
+
const BASE = 1.3;
|
|
55
|
+
/** Most elevation numbers on screen. */
|
|
56
|
+
export const MAX_LABELS = 6;
|
|
57
|
+
/** Metres between two lines, for the numbers. */
|
|
58
|
+
const METRES = 20;
|
|
59
|
+
/** The elevation the numbers count from, metres. */
|
|
60
|
+
const DATUM = 400;
|
|
61
|
+
/** Index lines' width over the fine lines'. */
|
|
62
|
+
const MAJOR_WIDTH = 2;
|
|
63
|
+
/** The numbers' size, CSS px. */
|
|
64
|
+
const LABEL_SIZE = 10.5;
|
|
65
|
+
/** The default ground and line: a night-blue chart in pale ink. */
|
|
66
|
+
const BACKGROUND = '#0c1a2b';
|
|
67
|
+
const COLOR = '#c9dcec';
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The octaves: amplitude, then the rows of the matrix that scales and turns
|
|
71
|
+
* the plane for it (lattice cells a uv unit at zoom 1), then its offset and
|
|
72
|
+
* how far it drifts a second. Each drifts its own way, so the hills change
|
|
73
|
+
* shape rather than slide.
|
|
74
|
+
*
|
|
75
|
+
* @type {{ amp: number, rows: [number, number, number, number], offset: [number, number], drift: [number, number] }[]}
|
|
76
|
+
*/
|
|
77
|
+
export const OCTAVES = [
|
|
78
|
+
[0.5, 1, 0, [0, 0], 0.012, 0.4],
|
|
79
|
+
[0.27, 2.03, 0.9, [13.7, 7.1], 0.02, 2.6],
|
|
80
|
+
[0.14, 4.11, 1.85, [29.3, 41.9], 0.028, 4.4],
|
|
81
|
+
[0.07, 8.3, 2.7, [5.9, 23.3], 0.036, 5.8],
|
|
82
|
+
].map(([amp, freq, turn, offset, rate, heading]) => ({
|
|
83
|
+
amp: Number(amp),
|
|
84
|
+
rows: /** @type {[number, number, number, number]} */ ([Math.cos(Number(turn)), -Math.sin(Number(turn)), Math.sin(Number(turn)), Math.cos(Number(turn))].map((v) => v * Number(freq))),
|
|
85
|
+
offset: /** @type {[number, number]} */ (offset),
|
|
86
|
+
drift: /** @type {[number, number]} */ ([Math.cos(Number(heading)) * Number(rate), Math.sin(Number(heading)) * Number(rate)]),
|
|
87
|
+
}));
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The height's working range, valleys to peaks: the gradient runs across
|
|
91
|
+
* it. The field reaches a little past both ends now and then.
|
|
92
|
+
*/
|
|
93
|
+
export const LOW = 0.22;
|
|
94
|
+
export const HIGH = 0.75;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The mean steepness of the field up the screen, height units a uv unit at
|
|
98
|
+
* zoom 1, so `density` can be lines across the screen whatever `scale`.
|
|
99
|
+
* Measured over a large area (see tests/topography.test.js).
|
|
100
|
+
*/
|
|
101
|
+
export const MEAN_SLOPE = 0.373;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The lattice as the shader reads it: a seeded random value at every
|
|
105
|
+
* cell, each texel holding its own and its three neighbours' (right, up,
|
|
106
|
+
* up-right), so one fetch gives a cell's four corners.
|
|
107
|
+
*
|
|
108
|
+
* @param {number} [seed=0x7a3d91] The seed.
|
|
109
|
+
* @returns {Uint8Array} RGBA, LATTICE × LATTICE texels.
|
|
110
|
+
*/
|
|
111
|
+
export function latticeData(seed = 0x7a3d91) {
|
|
112
|
+
let s = seed >>> 0;
|
|
113
|
+
/**
|
|
114
|
+
* mulberry32: the next value of a small seeded generator.
|
|
115
|
+
*
|
|
116
|
+
* @returns {number} 0–255.
|
|
117
|
+
*/
|
|
118
|
+
const next = () => {
|
|
119
|
+
s = (s + 0x6d2b79f5) >>> 0;
|
|
120
|
+
let t = Math.imul(s ^ (s >>> 15), 1 | s);
|
|
121
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
122
|
+
return ((t ^ (t >>> 14)) >>> 0) % 256;
|
|
123
|
+
};
|
|
124
|
+
const values = Uint8Array.from({ length: LATTICE * LATTICE }, next);
|
|
125
|
+
/**
|
|
126
|
+
* The value at a cell, wrapping.
|
|
127
|
+
*
|
|
128
|
+
* @param {number} x Column.
|
|
129
|
+
* @param {number} y Row.
|
|
130
|
+
* @returns {number} 0–255.
|
|
131
|
+
*/
|
|
132
|
+
const at = (x, y) => values[(y % LATTICE) * LATTICE + (x % LATTICE)];
|
|
133
|
+
const out = new Uint8Array(LATTICE * LATTICE * 4);
|
|
134
|
+
for (let y = 0; y < LATTICE; y++) {
|
|
135
|
+
for (let x = 0; x < LATTICE; x++) {
|
|
136
|
+
out.set([at(x, y), at(x + 1, y), at(x, y + 1), at(x + 1, y + 1)], (y * LATTICE + x) * 4);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Each octave's offset at a moment: where it started plus how far it has
|
|
144
|
+
* drifted, as the shader's `uDrift` takes them.
|
|
145
|
+
*
|
|
146
|
+
* @param {number} time The land's clock, seconds.
|
|
147
|
+
* @returns {Float32Array} Two floats an octave.
|
|
148
|
+
*/
|
|
149
|
+
export function driftAt(time) {
|
|
150
|
+
return new Float32Array(OCTAVES.flatMap(({ offset, drift }) => [offset[0] + drift[0] * time, offset[1] + drift[1] * time]));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Value noise with its gradient, from the lattice: quintic between the
|
|
155
|
+
* four corners, as the shader's `noised`.
|
|
156
|
+
*
|
|
157
|
+
* @param {Uint8Array} lattice From `latticeData`.
|
|
158
|
+
* @param {number} x Lattice units.
|
|
159
|
+
* @param {number} y Lattice units.
|
|
160
|
+
* @returns {[number, number, number]} The value (0–1) and its slope along x and y.
|
|
161
|
+
*/
|
|
162
|
+
function noised(lattice, x, y) {
|
|
163
|
+
const ix = Math.floor(x);
|
|
164
|
+
const iy = Math.floor(y);
|
|
165
|
+
const fx = x - ix;
|
|
166
|
+
const fy = y - iy;
|
|
167
|
+
const ux = fx * fx * fx * (fx * (fx * 6 - 15) + 10);
|
|
168
|
+
const uy = fy * fy * fy * (fy * (fy * 6 - 15) + 10);
|
|
169
|
+
const dx = 30 * fx * fx * (fx * (fx - 2) + 1);
|
|
170
|
+
const dy = 30 * fy * fy * (fy * (fy - 2) + 1);
|
|
171
|
+
const i = ((((iy % LATTICE) + LATTICE) % LATTICE) * LATTICE + (((ix % LATTICE) + LATTICE) % LATTICE)) * 4;
|
|
172
|
+
const [a, b, c, d] = [lattice[i] / 255, lattice[i + 1] / 255, lattice[i + 2] / 255, lattice[i + 3] / 255];
|
|
173
|
+
const k = a - b - c + d;
|
|
174
|
+
return [a + (b - a) * ux + (c - a) * uy + k * ux * uy, dx * (b - a + k * uy), dy * (c - a + k * ux)];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The land's height at a point, and its slope: the CPU's copy of the
|
|
179
|
+
* shader's `field`, so the numbers land on the lines.
|
|
180
|
+
*
|
|
181
|
+
* @param {Uint8Array} lattice From `latticeData`.
|
|
182
|
+
* @param {Float32Array | number[]} drift From `driftAt`.
|
|
183
|
+
* @param {number} zoom Lattice cells a uv unit for the largest octave.
|
|
184
|
+
* @param {number} x Across, uv (the screen's height is 1, 0 its middle).
|
|
185
|
+
* @param {number} y Up, uv.
|
|
186
|
+
* @returns {[number, number, number]} Height (about 0–1) and its slope along x and y, a uv unit.
|
|
187
|
+
*/
|
|
188
|
+
export function fieldAt(lattice, drift, zoom, x, y) {
|
|
189
|
+
const px = x * zoom;
|
|
190
|
+
const py = y * zoom;
|
|
191
|
+
let h = 0;
|
|
192
|
+
let gx = 0;
|
|
193
|
+
let gy = 0;
|
|
194
|
+
OCTAVES.forEach(({ amp, rows: [a, b, c, d] }, k) => {
|
|
195
|
+
const [n, nx, ny] = noised(lattice, a * px + b * py + drift[k * 2], c * px + d * py + drift[k * 2 + 1]);
|
|
196
|
+
h += amp * n;
|
|
197
|
+
gx += amp * (nx * a + ny * c);
|
|
198
|
+
gy += amp * (nx * b + ny * d);
|
|
199
|
+
});
|
|
200
|
+
return [h, gx * zoom, gy * zoom];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Walks a point onto a contour: a few Newton steps along the slope.
|
|
205
|
+
*
|
|
206
|
+
* @param {(x: number, y: number) => [number, number, number]} height The land: height and slope at a point.
|
|
207
|
+
* @param {number} x Where to start, uv.
|
|
208
|
+
* @param {number} y Where to start, uv.
|
|
209
|
+
* @param {number} level The contour's height.
|
|
210
|
+
* @param {number} [steps=3] Steps to take.
|
|
211
|
+
* @returns {{ x: number, y: number, gx: number, gy: number, miss: number } | null} The point, the slope
|
|
212
|
+
* there and how far off the contour it still is (uv), or null on flat ground.
|
|
213
|
+
*/
|
|
214
|
+
export function onContour(height, x, y, level, steps = 3) {
|
|
215
|
+
let px = x;
|
|
216
|
+
let py = y;
|
|
217
|
+
for (let i = 0; i <= steps; i++) {
|
|
218
|
+
const [h, gx, gy] = height(px, py);
|
|
219
|
+
const g2 = gx * gx + gy * gy;
|
|
220
|
+
if (g2 < 1e-6) return null;
|
|
221
|
+
if (i === steps) return { x: px, y: py, gx, gy, miss: Math.abs(h - level) / Math.sqrt(g2) };
|
|
222
|
+
const k = (h - level) / g2;
|
|
223
|
+
px -= k * gx;
|
|
224
|
+
py -= k * gy;
|
|
225
|
+
}
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A number's digits as the shader takes them: up to four, -1 for none.
|
|
231
|
+
*
|
|
232
|
+
* @param {number} value A whole number, 0–9999.
|
|
233
|
+
* @returns {[number, number, number, number]} The digits, left to right.
|
|
234
|
+
*/
|
|
235
|
+
export function digitsOf(value) {
|
|
236
|
+
const text = String(Math.max(0, Math.min(9999, Math.round(value))));
|
|
237
|
+
return /** @type {[number, number, number, number]} */ ([0, 1, 2, 3].map((i) => (i < text.length ? Number(text[i]) : -1)));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The point `i` of the Halton sequence in bases 2 and 3: evenly spread
|
|
242
|
+
* places to try a number, across a 0–1 square.
|
|
243
|
+
*
|
|
244
|
+
* @param {number} i Its index, from 1.
|
|
245
|
+
* @returns {[number, number]} The point.
|
|
246
|
+
*/
|
|
247
|
+
export function halton(i) {
|
|
248
|
+
/**
|
|
249
|
+
* The radical inverse of `i` in a base.
|
|
250
|
+
*
|
|
251
|
+
* @param {number} base The base.
|
|
252
|
+
* @returns {number} 0–1.
|
|
253
|
+
*/
|
|
254
|
+
const inverse = (base) => {
|
|
255
|
+
let f = 1;
|
|
256
|
+
let r = 0;
|
|
257
|
+
for (let n = i; n > 0; n = Math.floor(n / base)) {
|
|
258
|
+
f /= base;
|
|
259
|
+
r += f * (n % base);
|
|
260
|
+
}
|
|
261
|
+
return r;
|
|
262
|
+
};
|
|
263
|
+
return [inverse(2), inverse(3)];
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const FRAG = `
|
|
267
|
+
precision highp float;
|
|
268
|
+
uniform vec2 uRes;
|
|
269
|
+
uniform float uRatio;
|
|
270
|
+
uniform float uZoom;
|
|
271
|
+
uniform vec2 uDrift[${OCTAVES.length}];
|
|
272
|
+
uniform float uLevels;
|
|
273
|
+
uniform float uMajor;
|
|
274
|
+
uniform float uWidth;
|
|
275
|
+
uniform vec3 uGround;
|
|
276
|
+
uniform vec4 uPointer;
|
|
277
|
+
uniform float uRise;
|
|
278
|
+
uniform float uGlow;
|
|
279
|
+
uniform sampler2D uLattice;
|
|
280
|
+
uniform sampler2D uAtlas;
|
|
281
|
+
uniform vec4 uLab[${MAX_LABELS}];
|
|
282
|
+
uniform vec4 uLabDigits[${MAX_LABELS}];
|
|
283
|
+
uniform vec2 uLabMeta[${MAX_LABELS}];
|
|
284
|
+
uniform float uLabs;
|
|
285
|
+
uniform vec2 uCell;
|
|
286
|
+
${GLSL_OKLAB}
|
|
287
|
+
${GLSL_RAMP}
|
|
288
|
+
${GLSL_QUIET}
|
|
289
|
+
|
|
290
|
+
// Value noise and its gradient: one fetch gives the cell's four corners.
|
|
291
|
+
vec3 noised(vec2 p) {
|
|
292
|
+
vec2 i = floor(p);
|
|
293
|
+
vec2 f = p - i;
|
|
294
|
+
vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
|
|
295
|
+
vec2 du = 30.0 * f * f * (f * (f - 2.0) + 1.0);
|
|
296
|
+
vec4 c = texture2D(uLattice, (i + 0.5) / ${LATTICE.toFixed(1)});
|
|
297
|
+
float k = c.x - c.y - c.z + c.w;
|
|
298
|
+
return vec3(c.x + (c.y - c.x) * u.x + (c.z - c.x) * u.y + k * u.x * u.y, du * vec2(c.y - c.x + k * u.y, c.z - c.x + k * u.x));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// The land: height and its slope, a uv unit.
|
|
302
|
+
vec3 field(vec2 uv) {
|
|
303
|
+
vec2 p = uv * uZoom;
|
|
304
|
+
vec3 h = vec3(0.0);
|
|
305
|
+
vec3 n;
|
|
306
|
+
${OCTAVES.map(({ amp, rows: [a, b, c, d] }, k) => {
|
|
307
|
+
const r0 = `vec2(${a.toFixed(6)}, ${b.toFixed(6)})`;
|
|
308
|
+
const r1 = `vec2(${c.toFixed(6)}, ${d.toFixed(6)})`;
|
|
309
|
+
return ` n = noised(vec2(dot(p, ${r0}), dot(p, ${r1})) + uDrift[${k}]);
|
|
310
|
+
h += ${amp.toFixed(4)} * vec3(n.x, n.y * ${r0} + n.z * ${r1});`;
|
|
311
|
+
}).join('\n')}
|
|
312
|
+
h.yz *= uZoom;
|
|
313
|
+
return h;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Coverage of a line d px from its middle, w px wide, anti-aliased over a pixel.
|
|
317
|
+
float line(float d, float w) {
|
|
318
|
+
return clamp(max(w, 1.0) * 0.5 + 0.5 - d, 0.0, 1.0) * min(w, 1.0);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
void main() {
|
|
322
|
+
vec2 uv = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;
|
|
323
|
+
vec3 h = field(uv);
|
|
324
|
+
|
|
325
|
+
// The pointer: a low mound under it, and a glow a little wider.
|
|
326
|
+
vec2 toP = uv - uPointer.xy;
|
|
327
|
+
float r2 = dot(toP, toP) / (uPointer.w * uPointer.w);
|
|
328
|
+
float mound = exp(-r2) * uPointer.z * uRise;
|
|
329
|
+
h.x += mound;
|
|
330
|
+
h.yz += toP * (-2.0 / (uPointer.w * uPointer.w)) * mound;
|
|
331
|
+
float glow = exp(-r2 * 0.55) * uPointer.z * uGlow;
|
|
332
|
+
|
|
333
|
+
// Distance to the nearest line and the nearest index line, in px.
|
|
334
|
+
float slope = max(length(h.yz) / uRes.y * uLevels, 1e-6);
|
|
335
|
+
float spacing = 1.0 / slope;
|
|
336
|
+
float x = h.x * uLevels;
|
|
337
|
+
float d = abs(x - floor(x + 0.5)) * spacing;
|
|
338
|
+
float w = uWidth * (1.0 + 0.45 * min(glow, 1.0));
|
|
339
|
+
float fine = line(d, w) * mix(0.42, 0.75, min(glow, 1.0));
|
|
340
|
+
// Fine lines fade where they crowd closer than a few px.
|
|
341
|
+
fine *= smoothstep(w + 1.0, w + 4.0 * uRatio, spacing);
|
|
342
|
+
float index = 0.0;
|
|
343
|
+
if (uMajor > 0.5) {
|
|
344
|
+
float xm = x / uMajor;
|
|
345
|
+
float dm = abs(xm - floor(xm + 0.5)) * spacing * uMajor;
|
|
346
|
+
float wm = w * ${MAJOR_WIDTH.toFixed(1)};
|
|
347
|
+
index = line(dm, wm) * mix(0.8, 1.0, min(glow, 1.0)) * smoothstep(wm + 1.0, wm + 4.0 * uRatio, spacing * uMajor);
|
|
348
|
+
}
|
|
349
|
+
// A soft halo round the lines where they glow.
|
|
350
|
+
float halo = glow * 0.22 * exp(-d / (2.5 * uRatio)) * smoothstep(2.0, 8.0 * uRatio, spacing);
|
|
351
|
+
|
|
352
|
+
// Breaks in the lines for the numbers, and the numbers.
|
|
353
|
+
float gap = 0.0;
|
|
354
|
+
float text = 0.0;
|
|
355
|
+
for (int i = 0; i < ${MAX_LABELS}; i++) {
|
|
356
|
+
if (float(i) >= uLabs) break;
|
|
357
|
+
vec4 l = uLab[i];
|
|
358
|
+
vec2 meta = uLabMeta[i];
|
|
359
|
+
vec2 o = gl_FragCoord.xy - l.xy;
|
|
360
|
+
vec2 q = vec2(dot(o, l.zw), dot(o, vec2(-l.w, l.z)));
|
|
361
|
+
float hw = meta.y * uCell.x * 0.5;
|
|
362
|
+
vec2 box = abs(q) - vec2(hw + uCell.y * 0.4, uCell.y * 0.42);
|
|
363
|
+
gap = max(gap, (1.0 - smoothstep(-uRatio, uRatio, max(box.x, box.y))) * meta.x);
|
|
364
|
+
if (abs(q.x) < hw && abs(q.y) < uCell.y * 0.5) {
|
|
365
|
+
float cx = (q.x + hw) / uCell.x;
|
|
366
|
+
float digit = dot(uLabDigits[i], vec4(equal(vec4(floor(cx)), vec4(0.0, 1.0, 2.0, 3.0))));
|
|
367
|
+
text = max(text, texture2D(uAtlas, vec2((digit + fract(cx)) / 10.0, 0.5 - q.y / uCell.y)).a * meta.x);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
float t = (h.x - ${LOW.toFixed(3)}) / ${(HIGH - LOW).toFixed(3)};
|
|
372
|
+
vec3 ink = ramp(t);
|
|
373
|
+
// Lit lines move away from the ground: lighter on a dark map, deeper on paper.
|
|
374
|
+
float away = ink.x > uGround.x ? 1.0 : -1.0;
|
|
375
|
+
ink.x += away * 0.1 * min(glow, 1.0);
|
|
376
|
+
// The ground shades a touch with height, and warms under the glow.
|
|
377
|
+
vec3 ground = uGround;
|
|
378
|
+
ground.x += (clamp(t, 0.0, 1.0) - 0.5) * 0.03 * away;
|
|
379
|
+
ground = mix(ground, ink, 0.06 * min(glow, 1.0));
|
|
380
|
+
|
|
381
|
+
float calm = mix(1.0 - uQuiet.w, 1.0, quietOpen(uv));
|
|
382
|
+
float ink1 = clamp(max(fine, index) * (1.0 - gap) + halo, 0.0, 1.0) * calm;
|
|
383
|
+
vec3 col = mix(ground, ink, ink1);
|
|
384
|
+
col = mix(col, ink, text * 0.9 * calm);
|
|
385
|
+
gl_FragColor = vec4(fromOklab(col), 1.0);
|
|
386
|
+
}
|
|
387
|
+
`;
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Draws the digits 0–9 side by side, white on clear, for the shader to
|
|
391
|
+
* cut the numbers from: one cell a digit, `size` px tall.
|
|
392
|
+
*
|
|
393
|
+
* @param {number} size The type's size, device px.
|
|
394
|
+
* @returns {{ canvas: HTMLCanvasElement, cell: [number, number] }} The strip and a cell's size, px.
|
|
395
|
+
*/
|
|
396
|
+
function digitStrip(size) {
|
|
397
|
+
const canvas = document.createElement('canvas');
|
|
398
|
+
const ctx = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
|
|
399
|
+
const font = `600 ${size}px system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif`;
|
|
400
|
+
ctx.font = font;
|
|
401
|
+
const widest = Math.max(...[...'0123456789'].map((c) => ctx.measureText(c).width));
|
|
402
|
+
const cell = /** @type {[number, number]} */ ([Math.ceil(widest + size * 0.08), Math.ceil(size * 1.3)]);
|
|
403
|
+
canvas.width = cell[0] * 10;
|
|
404
|
+
canvas.height = cell[1];
|
|
405
|
+
ctx.font = font;
|
|
406
|
+
ctx.fillStyle = '#fff';
|
|
407
|
+
ctx.textAlign = 'center';
|
|
408
|
+
ctx.textBaseline = 'middle';
|
|
409
|
+
for (let i = 0; i < 10; i++) ctx.fillText(String(i), cell[0] * (i + 0.5), cell[1] * 0.54);
|
|
410
|
+
return { canvas, cell };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Starts the topography on an element: an opaque canvas inside it, filling it.
|
|
415
|
+
*
|
|
416
|
+
* @param {HTMLElement} el The element (its first canvas is drawn into).
|
|
417
|
+
* @param {TopographyConfig} config The settings.
|
|
418
|
+
* @returns {() => void} Stops it.
|
|
419
|
+
*/
|
|
420
|
+
export function topography(el, config) {
|
|
421
|
+
const speed = config.speed ?? 1;
|
|
422
|
+
const interactive = config.interactive !== false;
|
|
423
|
+
const major = Math.max(0, Math.round(config.major ?? 5));
|
|
424
|
+
const step = major || 5;
|
|
425
|
+
const zoom = BASE / (config.scale ?? 1);
|
|
426
|
+
const levels = (config.density ?? 24) / (MEAN_SLOPE * zoom);
|
|
427
|
+
const rise = 2.2 / levels;
|
|
428
|
+
const ground = toOklab(parseColor(config.background ?? BACKGROUND));
|
|
429
|
+
const start = pickColors(config, [COLOR]);
|
|
430
|
+
let ramp = gradientUniform(start);
|
|
431
|
+
let ramps = Math.min(MAX_COLORS, start.length);
|
|
432
|
+
const lattice = latticeData();
|
|
433
|
+
// The land's own clock: it advances with `speed`, so pausing does not jump.
|
|
434
|
+
let time = 40;
|
|
435
|
+
let drift = driftAt(time);
|
|
436
|
+
/** The pointer's mound: where it is heading and where it is (uv), and how strong (0–1). */
|
|
437
|
+
const mound = { tx: 0, ty: 0, x: 0, y: 0, on: 0, r: 0.2 };
|
|
438
|
+
/** The numbers on screen, by slot: where (uv), which line, how far faded in, and which way the line runs. @type {({ x: number, y: number, level: number, alpha: number, tx: number, ty: number } | null)[]} */
|
|
439
|
+
const slots = [];
|
|
440
|
+
let tried = 0;
|
|
441
|
+
let cell = [8, 13];
|
|
442
|
+
/** @type {WebGLTexture | null} */
|
|
443
|
+
let atlas = null;
|
|
444
|
+
const lab = new Float32Array(MAX_LABELS * 4);
|
|
445
|
+
const labDigits = new Float32Array(MAX_LABELS * 4).fill(-1);
|
|
446
|
+
const labMeta = new Float32Array(MAX_LABELS * 2);
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* The land as it is now, with the pointer's mound.
|
|
450
|
+
*
|
|
451
|
+
* @param {number} x Across, uv.
|
|
452
|
+
* @param {number} y Up, uv.
|
|
453
|
+
* @returns {[number, number, number]} Height and slope.
|
|
454
|
+
*/
|
|
455
|
+
const height = (x, y) => {
|
|
456
|
+
const f = fieldAt(lattice, drift, zoom, x, y);
|
|
457
|
+
if (mound.on < 1e-3) return f;
|
|
458
|
+
const dx = x - mound.x;
|
|
459
|
+
const dy = y - mound.y;
|
|
460
|
+
const m = Math.exp(-(dx * dx + dy * dy) / (mound.r * mound.r)) * mound.on * rise;
|
|
461
|
+
const k = (-2 / (mound.r * mound.r)) * m;
|
|
462
|
+
return [f[0] + m, f[1] + dx * k, f[2] + dy * k];
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Follows the pointer: the mound eases towards it, and fades when it leaves.
|
|
467
|
+
*
|
|
468
|
+
* @param {number} aspect The canvas's width over height.
|
|
469
|
+
* @param {number} dt Seconds since the last frame.
|
|
470
|
+
*/
|
|
471
|
+
const follow = (aspect, dt) => {
|
|
472
|
+
const p = pointerIn(el);
|
|
473
|
+
if (p.inside) {
|
|
474
|
+
mound.tx = (p.x - 0.5) * aspect;
|
|
475
|
+
mound.ty = p.y - 0.5;
|
|
476
|
+
if (mound.on < 0.01) Object.assign(mound, { x: mound.tx, y: mound.ty });
|
|
477
|
+
}
|
|
478
|
+
const k = 1 - Math.exp(-dt * 4);
|
|
479
|
+
mound.x += (mound.tx - mound.x) * k;
|
|
480
|
+
mound.y += (mound.ty - mound.y) * k;
|
|
481
|
+
mound.on += ((p.inside ? 1 : 0) - mound.on) * (1 - Math.exp(-dt * 2.5));
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Which way a contour runs at a point: across the slope, turned so type
|
|
486
|
+
* set along it stands upright.
|
|
487
|
+
*
|
|
488
|
+
* @param {{ gx: number, gy: number }} at The slope there.
|
|
489
|
+
* @returns {{ tx: number, ty: number }} The direction, a unit vector pointing rightwards.
|
|
490
|
+
*/
|
|
491
|
+
const along = ({ gx, gy }) => {
|
|
492
|
+
const k = (gy >= 0 ? 1 : -1) / Math.hypot(gx, gy);
|
|
493
|
+
return { tx: gy * k, ty: -gx * k };
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Whether a number fits where it is: inside the frame, out of the quiet
|
|
498
|
+
* area and away from the others, on a line straight enough over its
|
|
499
|
+
* length, with room between its neighbours.
|
|
500
|
+
*
|
|
501
|
+
* @param {{ x: number, y: number, gx: number, gy: number, miss: number }} at The point on the line.
|
|
502
|
+
* @param {number} level The line's height.
|
|
503
|
+
* @param {number} slot Its slot, so it is not kept from itself.
|
|
504
|
+
* @param {number} aspect Width over height.
|
|
505
|
+
* @param {number} px The canvas's height, device px.
|
|
506
|
+
* @param {[number, number, number, number]} quiet The quiet area, as `uQuiet`.
|
|
507
|
+
* @returns {boolean} True when it fits.
|
|
508
|
+
*/
|
|
509
|
+
const fits = (at, level, slot, aspect, px, quiet) => {
|
|
510
|
+
const g = Math.hypot(at.gx, at.gy);
|
|
511
|
+
const half = (cell[0] * 2 + cell[1]) / px;
|
|
512
|
+
if (at.miss * px > 0.5) return false;
|
|
513
|
+
if (Math.abs(at.x) > aspect / 2 - half - 0.04 || Math.abs(at.y) > 0.5 - half - 0.04) return false;
|
|
514
|
+
if (quiet[2] > 0 && Math.hypot(at.x - quiet[0], at.y - quiet[1]) < quiet[2] * 0.85) return false;
|
|
515
|
+
if (px / (levels * g) < cell[1]) return false;
|
|
516
|
+
for (let i = 0; i < slots.length; i++) {
|
|
517
|
+
const o = slots[i];
|
|
518
|
+
if (i !== slot && o && Math.hypot(o.x - at.x, o.y - at.y) < 0.3) return false;
|
|
519
|
+
}
|
|
520
|
+
// Steeper than about 55 degrees, the type would be hard to read.
|
|
521
|
+
const tx = -at.gy / g;
|
|
522
|
+
const ty = at.gx / g;
|
|
523
|
+
if (Math.abs(ty) > 0.82) return false;
|
|
524
|
+
for (const e of [-half, half]) {
|
|
525
|
+
const [h, gx, gy] = height(at.x + tx * e, at.y + ty * e);
|
|
526
|
+
if ((Math.abs(h - level) / Math.hypot(gx, gy)) * px > cell[1] * 0.4) return false;
|
|
527
|
+
}
|
|
528
|
+
return true;
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Moves the numbers with their lines, fades out any that no longer fit
|
|
533
|
+
* and finds new places for them, then writes the label uniforms.
|
|
534
|
+
*
|
|
535
|
+
* @param {number} aspect Width over height.
|
|
536
|
+
* @param {number} px The canvas's height, device px.
|
|
537
|
+
* @param {number} dt Seconds since the last frame; 0 settles them at once (a still frame).
|
|
538
|
+
* @returns {number} How many slots there are.
|
|
539
|
+
*/
|
|
540
|
+
const place = (aspect, px, dt) => {
|
|
541
|
+
const width = px * aspect;
|
|
542
|
+
const count = Math.max(2, Math.min(MAX_LABELS, Math.round((el.clientWidth * el.clientHeight) / 180000)));
|
|
543
|
+
const quiet = quietUniform(config.quiet, aspect);
|
|
544
|
+
const ease = dt ? 1 - Math.exp(-dt * 2) : 1;
|
|
545
|
+
for (let i = 0; i < count; i++) {
|
|
546
|
+
let s = slots[i] ?? null;
|
|
547
|
+
if (s) {
|
|
548
|
+
const at = onContour(height, s.x, s.y, s.level / levels, 2);
|
|
549
|
+
const ok = Boolean(at && fits(at, s.level / levels, i, aspect, px, quiet));
|
|
550
|
+
if (at && ok) Object.assign(s, { x: at.x, y: at.y, ...along(at) });
|
|
551
|
+
s.alpha += ((ok ? 1 : 0) - s.alpha) * ease;
|
|
552
|
+
if (!ok && s.alpha < 0.02) s = slots[i] = null;
|
|
553
|
+
}
|
|
554
|
+
// An empty slot looks for a place: a few tries a frame, more for a still frame.
|
|
555
|
+
for (let k = 0; !s && k < (dt ? 2 : 40); k++) {
|
|
556
|
+
const [hx, hy] = halton(++tried);
|
|
557
|
+
const x = (hx - 0.5) * aspect;
|
|
558
|
+
const y = hy - 0.5;
|
|
559
|
+
const level = Math.round((height(x, y)[0] * levels) / step) * step;
|
|
560
|
+
const at = onContour(height, x, y, level / levels);
|
|
561
|
+
if (at && fits(at, level / levels, i, aspect, px, quiet)) s = slots[i] = { x: at.x, y: at.y, level, alpha: dt ? 0 : 1, ...along(at) };
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
slots.length = Math.min(slots.length, count);
|
|
565
|
+
slots.forEach((s, i) => {
|
|
566
|
+
const digits = s ? digitsOf(DATUM + s.level * METRES) : [-1, -1, -1, -1];
|
|
567
|
+
lab.set(s ? [s.x * px + width / 2, s.y * px + px / 2, s.tx, s.ty] : [0, 0, 1, 0], i * 4);
|
|
568
|
+
labDigits.set(digits, i * 4);
|
|
569
|
+
labMeta.set([s ? s.alpha : 0, digits.filter((d) => d >= 0).length], i * 2);
|
|
570
|
+
});
|
|
571
|
+
return slots.length;
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
const { stage, stop } = createStage(el, {
|
|
575
|
+
fragment: FRAG,
|
|
576
|
+
alpha: false,
|
|
577
|
+
still: speed === 0 && !interactive,
|
|
578
|
+
setup({ gl, uniform }) {
|
|
579
|
+
const grid = gl.createTexture();
|
|
580
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
581
|
+
gl.bindTexture(gl.TEXTURE_2D, grid);
|
|
582
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
583
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, LATTICE, LATTICE, 0, gl.RGBA, gl.UNSIGNED_BYTE, lattice);
|
|
584
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
585
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
586
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
|
|
587
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
|
|
588
|
+
gl.uniform1i(uniform('uLattice'), 0);
|
|
589
|
+
gl.uniform1i(uniform('uAtlas'), 1);
|
|
590
|
+
gl.uniform1f(uniform('uZoom'), zoom);
|
|
591
|
+
gl.uniform1f(uniform('uLevels'), levels);
|
|
592
|
+
gl.uniform1f(uniform('uMajor'), major);
|
|
593
|
+
gl.uniform1f(uniform('uRise'), rise);
|
|
594
|
+
gl.uniform1f(uniform('uGlow'), interactive ? (config.glow ?? 1) : 0);
|
|
595
|
+
gl.uniform3fv(uniform('uGround'), ground);
|
|
596
|
+
return () => {
|
|
597
|
+
gl.deleteTexture(grid);
|
|
598
|
+
if (atlas) gl.deleteTexture(atlas);
|
|
599
|
+
};
|
|
600
|
+
},
|
|
601
|
+
resize({ gl, uniform, ratio }) {
|
|
602
|
+
gl.uniform1f(uniform('uRatio'), ratio);
|
|
603
|
+
gl.uniform1f(uniform('uWidth'), (config.thickness ?? 1) * ratio);
|
|
604
|
+
if (!config.labels) return;
|
|
605
|
+
const strip = digitStrip(Math.round(LABEL_SIZE * ratio));
|
|
606
|
+
cell = strip.cell;
|
|
607
|
+
atlas ??= gl.createTexture();
|
|
608
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
609
|
+
gl.bindTexture(gl.TEXTURE_2D, atlas);
|
|
610
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, strip.canvas);
|
|
611
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
612
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
613
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
614
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
615
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
616
|
+
gl.uniform2f(uniform('uCell'), cell[0], cell[1]);
|
|
617
|
+
},
|
|
618
|
+
uniforms({ gl, uniform, canvas, still }, _now, dt) {
|
|
619
|
+
const aspect = canvas.width / canvas.height;
|
|
620
|
+
if (!still) {
|
|
621
|
+
time += dt * speed;
|
|
622
|
+
drift = driftAt(time);
|
|
623
|
+
if (interactive && canHover()) follow(aspect, dt);
|
|
624
|
+
}
|
|
625
|
+
gl.uniform2fv(uniform('uDrift'), drift);
|
|
626
|
+
gl.uniform4f(uniform('uPointer'), mound.x, mound.y, mound.on, mound.r);
|
|
627
|
+
gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, aspect));
|
|
628
|
+
gl.uniform3fv(uniform('uRamp'), ramp);
|
|
629
|
+
gl.uniform1f(uniform('uRamps'), ramps);
|
|
630
|
+
const labs = config.labels ? place(aspect, canvas.height, still ? 0 : dt) : 0;
|
|
631
|
+
gl.uniform1f(uniform('uLabs'), labs);
|
|
632
|
+
if (labs) {
|
|
633
|
+
gl.uniform4fv(uniform('uLab'), lab);
|
|
634
|
+
gl.uniform4fv(uniform('uLabDigits'), labDigits);
|
|
635
|
+
gl.uniform2fv(uniform('uLabMeta'), labMeta);
|
|
636
|
+
}
|
|
637
|
+
},
|
|
638
|
+
});
|
|
639
|
+
if (!stage) return stop;
|
|
640
|
+
|
|
641
|
+
const unwatch = watchColors(el, (next) => {
|
|
642
|
+
ramp = gradientUniform(next.colors);
|
|
643
|
+
ramps = Math.min(MAX_COLORS, next.colors.length);
|
|
644
|
+
if (stage.still) stage.redraw();
|
|
645
|
+
});
|
|
646
|
+
return () => {
|
|
647
|
+
stop();
|
|
648
|
+
unwatch();
|
|
649
|
+
};
|
|
650
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** A box in CSS px from the element's top left. */
|
|
2
|
+
export interface TrackerBox {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
w: number;
|
|
6
|
+
h: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Makes `count` tracker boxes (`kind` 'box', bracketed, or 'marker', a
|
|
11
|
+
* crosshair) in an effect's element. `place(i, box, label?)` eases box `i`
|
|
12
|
+
* towards `box` (null fades it out); `destroy` removes them.
|
|
13
|
+
*/
|
|
14
|
+
export function createTrackers(
|
|
15
|
+
el: HTMLElement,
|
|
16
|
+
count: number,
|
|
17
|
+
options?: { kind?: 'box' | 'marker' },
|
|
18
|
+
): {
|
|
19
|
+
place(i: number, box: TrackerBox | null, label?: string): void;
|
|
20
|
+
destroy(): void;
|
|
21
|
+
};
|