@arraypress/effects-astro 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +85 -0
- package/COMPONENTS.md +1264 -0
- package/LICENSE +139 -0
- package/README.md +197 -2
- package/package.json +60 -4
- package/src/components/AsciiRain.astro +36 -0
- package/src/components/BorderSweep.astro +39 -0
- package/src/components/Caustics.astro +39 -0
- package/src/components/ColorPanels.astro +62 -0
- package/src/components/Cursor.astro +97 -0
- package/src/components/DotGrid.astro +34 -0
- package/src/components/DotSurface.astro +35 -0
- package/src/components/EffectCanvas.astro +152 -0
- package/src/components/FitText.astro +35 -0
- package/src/components/FlutedGlass.astro +36 -0
- package/src/components/Glass.astro +69 -0
- package/src/components/GlassGlyph.astro +38 -0
- package/src/components/GlowField.astro +177 -0
- package/src/components/Grain.astro +98 -0
- package/src/components/HaloRing.astro +38 -0
- package/src/components/HighlightSweep.astro +74 -0
- package/src/components/ImageRipple.astro +56 -0
- package/src/components/Ink.astro +37 -0
- package/src/components/InkField.astro +45 -0
- package/src/components/IsoCity.astro +54 -0
- package/src/components/LightField.astro +40 -0
- package/src/components/LiquidMetal.astro +41 -0
- package/src/components/Magnetic.astro +52 -0
- package/src/components/Marquee.astro +216 -0
- package/src/components/MosaicReveal.astro +196 -0
- package/src/components/NebulaLogo.astro +38 -0
- package/src/components/NeonSwarm.astro +38 -0
- package/src/components/Ocean.astro +40 -0
- package/src/components/OutlineWordmark.astro +222 -0
- package/src/components/PaperBackground.astro +40 -0
- package/src/components/PaperShader.astro +55 -0
- package/src/components/Parallax.astro +49 -0
- package/src/components/ParticleLogo.astro +37 -0
- package/src/components/PixelGlitchText.astro +70 -0
- package/src/components/PointCloud.astro +37 -0
- package/src/components/ProductStack.astro +298 -0
- package/src/components/PulseMark.astro +189 -0
- package/src/components/RadiantGlass.astro +38 -0
- package/src/components/RainOnGlass.astro +71 -0
- package/src/components/Reveal.astro +115 -0
- package/src/components/ScrambleText.astro +52 -0
- package/src/components/ScrollReveal.astro +38 -0
- package/src/components/ShaderBackground.astro +55 -0
- package/src/components/SignalLines.astro +41 -0
- package/src/components/SilkRibbon.astro +37 -0
- package/src/components/SpotlightText.astro +131 -0
- package/src/components/Tilt.astro +79 -0
- package/src/components/Topography.astro +38 -0
- package/src/components/Typewriter.astro +66 -0
- package/src/components/VariableType.astro +59 -0
- package/src/components/VoxelLogo.astro +46 -0
- package/src/components/WaveBars.astro +41 -0
- package/src/components/WorldRoutes.astro +41 -0
- package/src/components/style.js +19 -0
- package/src/index.d.ts +143 -0
- package/src/index.js +105 -0
- package/src/runtime/ascii-rain.d.ts +53 -0
- package/src/runtime/ascii-rain.js +355 -0
- package/src/runtime/audio-meter.d.ts +65 -0
- package/src/runtime/audio-meter.js +282 -0
- package/src/runtime/audio.d.ts +26 -0
- package/src/runtime/audio.js +453 -0
- package/src/runtime/blur.d.ts +2 -0
- package/src/runtime/blur.js +56 -0
- package/src/runtime/canvas2d.d.ts +23 -0
- package/src/runtime/canvas2d.js +105 -0
- package/src/runtime/caustics.d.ts +40 -0
- package/src/runtime/caustics.js +286 -0
- package/src/runtime/color.d.ts +33 -0
- package/src/runtime/color.js +213 -0
- package/src/runtime/curl.d.ts +5 -0
- package/src/runtime/curl.js +81 -0
- package/src/runtime/cursor.d.ts +9 -0
- package/src/runtime/cursor.js +80 -0
- package/src/runtime/device.d.ts +14 -0
- package/src/runtime/device.js +73 -0
- package/src/runtime/dot-grid.d.ts +27 -0
- package/src/runtime/dot-grid.js +245 -0
- package/src/runtime/dot-surface.d.ts +46 -0
- package/src/runtime/dot-surface.js +429 -0
- package/src/runtime/feedback.d.ts +18 -0
- package/src/runtime/feedback.js +131 -0
- package/src/runtime/fit-text.d.ts +5 -0
- package/src/runtime/fit-text.js +60 -0
- package/src/runtime/fluid.d.ts +55 -0
- package/src/runtime/fluid.js +504 -0
- package/src/runtime/fluted-glass.d.ts +27 -0
- package/src/runtime/fluted-glass.js +157 -0
- package/src/runtime/gl.d.ts +19 -0
- package/src/runtime/gl.js +104 -0
- package/src/runtime/glass-glyph.d.ts +49 -0
- package/src/runtime/glass-glyph.js +355 -0
- package/src/runtime/glass-sheen.d.ts +2 -0
- package/src/runtime/glass-sheen.js +30 -0
- package/src/runtime/glsl.d.ts +17 -0
- package/src/runtime/glsl.js +78 -0
- package/src/runtime/glyphs.d.ts +22 -0
- package/src/runtime/glyphs.js +107 -0
- package/src/runtime/halo-ring.d.ts +47 -0
- package/src/runtime/halo-ring.js +334 -0
- package/src/runtime/height-field.d.ts +50 -0
- package/src/runtime/height-field.js +176 -0
- package/src/runtime/image-ripple.d.ts +25 -0
- package/src/runtime/image-ripple.js +353 -0
- package/src/runtime/image.d.ts +11 -0
- package/src/runtime/image.js +81 -0
- package/src/runtime/in-view.d.ts +6 -0
- package/src/runtime/in-view.js +36 -0
- package/src/runtime/ink-field/shaders.js +258 -0
- package/src/runtime/ink-field/sim.js +582 -0
- package/src/runtime/ink-field.d.ts +39 -0
- package/src/runtime/ink-field.js +311 -0
- package/src/runtime/ink.d.ts +31 -0
- package/src/runtime/ink.js +109 -0
- package/src/runtime/instancing.d.ts +12 -0
- package/src/runtime/instancing.js +37 -0
- package/src/runtime/iso-camera.d.ts +35 -0
- package/src/runtime/iso-camera.js +85 -0
- package/src/runtime/iso-city/figures.d.ts +30 -0
- package/src/runtime/iso-city/figures.js +136 -0
- package/src/runtime/iso-city/look.d.ts +34 -0
- package/src/runtime/iso-city/look.js +105 -0
- package/src/runtime/iso-city/plan.d.ts +59 -0
- package/src/runtime/iso-city/plan.js +463 -0
- package/src/runtime/iso-city/roads.d.ts +64 -0
- package/src/runtime/iso-city/roads.js +320 -0
- package/src/runtime/iso-city/scene.d.ts +27 -0
- package/src/runtime/iso-city/scene.js +432 -0
- package/src/runtime/iso-city/shaders.d.ts +6 -0
- package/src/runtime/iso-city/shaders.js +233 -0
- package/src/runtime/iso-city/shadows.d.ts +13 -0
- package/src/runtime/iso-city/shadows.js +148 -0
- package/src/runtime/iso-city/traffic.d.ts +80 -0
- package/src/runtime/iso-city/traffic.js +457 -0
- package/src/runtime/iso-city.d.ts +37 -0
- package/src/runtime/iso-city.js +458 -0
- package/src/runtime/lens-map.d.ts +11 -0
- package/src/runtime/lens-map.js +146 -0
- package/src/runtime/light-field.d.ts +29 -0
- package/src/runtime/light-field.js +292 -0
- package/src/runtime/liquid-glass.d.ts +48 -0
- package/src/runtime/liquid-glass.js +423 -0
- package/src/runtime/liquid-metal/beads.js +217 -0
- package/src/runtime/liquid-metal/field.js +88 -0
- package/src/runtime/liquid-metal/look.js +64 -0
- package/src/runtime/liquid-metal/shaders.js +255 -0
- package/src/runtime/liquid-metal/wave.js +53 -0
- package/src/runtime/liquid-metal.d.ts +46 -0
- package/src/runtime/liquid-metal.js +366 -0
- package/src/runtime/loop.d.ts +7 -0
- package/src/runtime/loop.js +112 -0
- package/src/runtime/magnetic.d.ts +9 -0
- package/src/runtime/magnetic.js +46 -0
- package/src/runtime/marquee.d.ts +2 -0
- package/src/runtime/marquee.js +27 -0
- package/src/runtime/mesh.d.ts +30 -0
- package/src/runtime/mesh.js +217 -0
- package/src/runtime/mosaic-reveal.d.ts +34 -0
- package/src/runtime/mosaic-reveal.js +504 -0
- package/src/runtime/motion.d.ts +2 -0
- package/src/runtime/motion.js +21 -0
- package/src/runtime/mount.d.ts +13 -0
- package/src/runtime/mount.js +120 -0
- package/src/runtime/nebula-logo.d.ts +31 -0
- package/src/runtime/nebula-logo.js +282 -0
- package/src/runtime/neon-swarm.d.ts +31 -0
- package/src/runtime/neon-swarm.js +258 -0
- package/src/runtime/ocean/presets.d.ts +54 -0
- package/src/runtime/ocean/presets.js +184 -0
- package/src/runtime/ocean/shader.d.ts +14 -0
- package/src/runtime/ocean/shader.js +389 -0
- package/src/runtime/ocean/view.d.ts +24 -0
- package/src/runtime/ocean/view.js +98 -0
- package/src/runtime/ocean/waves.d.ts +29 -0
- package/src/runtime/ocean/waves.js +119 -0
- package/src/runtime/ocean.d.ts +31 -0
- package/src/runtime/ocean.js +173 -0
- package/src/runtime/palettes.d.ts +37 -0
- package/src/runtime/palettes.js +75 -0
- package/src/runtime/paper-look.d.ts +42 -0
- package/src/runtime/paper-look.js +91 -0
- package/src/runtime/paper-looks.d.ts +17 -0
- package/src/runtime/paper-looks.js +21 -0
- package/src/runtime/paper.d.ts +37 -0
- package/src/runtime/paper.js +303 -0
- package/src/runtime/particle-logo.d.ts +31 -0
- package/src/runtime/particle-logo.js +196 -0
- package/src/runtime/ping-pong.d.ts +43 -0
- package/src/runtime/ping-pong.js +175 -0
- package/src/runtime/pixel-glitch.d.ts +17 -0
- package/src/runtime/pixel-glitch.js +287 -0
- package/src/runtime/point-cloud.d.ts +33 -0
- package/src/runtime/point-cloud.js +287 -0
- package/src/runtime/pointer.d.ts +27 -0
- package/src/runtime/pointer.js +128 -0
- package/src/runtime/points.d.ts +25 -0
- package/src/runtime/points.js +130 -0
- package/src/runtime/pools.d.ts +2 -0
- package/src/runtime/pools.js +25 -0
- package/src/runtime/product-stack.d.ts +24 -0
- package/src/runtime/product-stack.js +210 -0
- package/src/runtime/radiant-glass.d.ts +29 -0
- package/src/runtime/radiant-glass.js +282 -0
- package/src/runtime/rain-on-glass/backdrop.d.ts +22 -0
- package/src/runtime/rain-on-glass/backdrop.js +185 -0
- package/src/runtime/rain-on-glass/shader.d.ts +2 -0
- package/src/runtime/rain-on-glass/shader.js +168 -0
- package/src/runtime/rain-on-glass/wipe.d.ts +16 -0
- package/src/runtime/rain-on-glass/wipe.js +65 -0
- package/src/runtime/rain-on-glass.d.ts +27 -0
- package/src/runtime/rain-on-glass.js +210 -0
- package/src/runtime/ramp.d.ts +2 -0
- package/src/runtime/ramp.js +30 -0
- package/src/runtime/random.d.ts +2 -0
- package/src/runtime/random.js +22 -0
- package/src/runtime/scroll-reveal.d.ts +22 -0
- package/src/runtime/scroll-reveal.js +73 -0
- package/src/runtime/shader-background.d.ts +42 -0
- package/src/runtime/shader-background.js +261 -0
- package/src/runtime/shader-backgrounds/band-lens.js +72 -0
- package/src/runtime/shader-backgrounds/common.js +53 -0
- package/src/runtime/shader-backgrounds/grain-gradient.js +87 -0
- package/src/runtime/shader-backgrounds/grid.js +33 -0
- package/src/runtime/shader-backgrounds/mesh-gradient.js +74 -0
- package/src/runtime/shader-backgrounds/silk.js +57 -0
- package/src/runtime/shader-backgrounds/waves.js +31 -0
- package/src/runtime/shape.d.ts +42 -0
- package/src/runtime/shape.js +313 -0
- package/src/runtime/silk-ribbon.d.ts +23 -0
- package/src/runtime/silk-ribbon.js +206 -0
- package/src/runtime/slideshow.d.ts +27 -0
- package/src/runtime/slideshow.js +234 -0
- package/src/runtime/spotlight.d.ts +9 -0
- package/src/runtime/spotlight.js +97 -0
- package/src/runtime/stack-poses.d.ts +53 -0
- package/src/runtime/stack-poses.js +273 -0
- package/src/runtime/stage.d.ts +47 -0
- package/src/runtime/stage.js +187 -0
- package/src/runtime/swarm.d.ts +49 -0
- package/src/runtime/swarm.js +199 -0
- package/src/runtime/text-motion.d.ts +41 -0
- package/src/runtime/text-motion.js +165 -0
- package/src/runtime/text.d.ts +11 -0
- package/src/runtime/text.js +90 -0
- package/src/runtime/tilt.d.ts +19 -0
- package/src/runtime/tilt.js +116 -0
- package/src/runtime/topography.d.ts +71 -0
- package/src/runtime/topography.js +650 -0
- package/src/runtime/trackers.d.ts +21 -0
- package/src/runtime/trackers.js +76 -0
- package/src/runtime/variable-type.d.ts +43 -0
- package/src/runtime/variable-type.js +358 -0
- package/src/runtime/voxel-logo.d.ts +31 -0
- package/src/runtime/voxel-logo.js +237 -0
- package/src/runtime/waves.d.ts +70 -0
- package/src/runtime/waves.js +799 -0
- package/src/runtime/world-labels.d.ts +29 -0
- package/src/runtime/world-labels.js +59 -0
- package/src/runtime/world-land.d.ts +8 -0
- package/src/runtime/world-land.js +105 -0
- package/src/runtime/world-map.d.ts +69 -0
- package/src/runtime/world-map.js +308 -0
- package/src/runtime/world-plan.d.ts +101 -0
- package/src/runtime/world-plan.js +333 -0
- package/src/runtime/world-routes.d.ts +75 -0
- package/src/runtime/world-routes.js +673 -0
- package/src/styles/border-sweep.css +82 -0
- package/src/styles/glass.css +104 -0
- package/src/styles/motion.css +218 -0
- package/src/styles/reveal.css +51 -0
- package/src/vendor/paper/LICENSE +201 -0
- package/src/vendor/paper/NOTICE +5 -0
- package/src/vendor/paper/README.md +8 -0
- package/src/vendor/paper/get-shader-color-from-string.js +100 -0
- package/src/vendor/paper/get-shader-noise-texture.js +17 -0
- package/src/vendor/paper/presets.js +3678 -0
- package/src/vendor/paper/shader-sizing.js +37 -0
- package/src/vendor/paper/shader-utils.js +130 -0
- package/src/vendor/paper/shaders/color-panels.js +223 -0
- package/src/vendor/paper/shaders/dithering.js +270 -0
- package/src/vendor/paper/shaders/dot-grid.js +109 -0
- package/src/vendor/paper/shaders/dot-orbit.js +122 -0
- package/src/vendor/paper/shaders/fluted-glass.js +360 -0
- package/src/vendor/paper/shaders/gem-smoke.js +572 -0
- package/src/vendor/paper/shaders/god-rays.js +132 -0
- package/src/vendor/paper/shaders/grain-gradient.js +300 -0
- package/src/vendor/paper/shaders/halftone-cmyk.js +294 -0
- package/src/vendor/paper/shaders/halftone-dots.js +320 -0
- package/src/vendor/paper/shaders/heatmap.js +383 -0
- package/src/vendor/paper/shaders/image-dithering.js +198 -0
- package/src/vendor/paper/shaders/lens-distortion.js +275 -0
- package/src/vendor/paper/shaders/liquid-metal.js +632 -0
- package/src/vendor/paper/shaders/mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/metaballs.js +110 -0
- package/src/vendor/paper/shaders/neuro-noise.js +76 -0
- package/src/vendor/paper/shaders/paper-texture.js +526 -0
- package/src/vendor/paper/shaders/perlin-noise.js +166 -0
- package/src/vendor/paper/shaders/pulsing-border.js +238 -0
- package/src/vendor/paper/shaders/simplex-noise.js +91 -0
- package/src/vendor/paper/shaders/smoke-ring.js +132 -0
- package/src/vendor/paper/shaders/spiral.js +82 -0
- package/src/vendor/paper/shaders/static-mesh-gradient.js +130 -0
- package/src/vendor/paper/shaders/static-radial-gradient.js +187 -0
- package/src/vendor/paper/shaders/swirl.js +103 -0
- package/src/vendor/paper/shaders/voronoi.js +138 -0
- package/src/vendor/paper/shaders/warp.js +129 -0
- package/src/vendor/paper/shaders/water.js +121 -0
- package/src/vendor/paper/shaders/waves.js +65 -0
- package/src/vendor/paper/vertex-shader.js +159 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/dot-surface
|
|
3
|
+
*
|
|
4
|
+
* A halftone sea: a regular grid of round dots lying on a slowly rolling
|
|
5
|
+
* sheet, seen in perspective from a little above. Columns lean and bunch
|
|
6
|
+
* with the swells and rows close up into the distance. Each dot is sized
|
|
7
|
+
* and lit by how the light catches the sheet where it lies, so a crest
|
|
8
|
+
* turned towards the light is a run of large bright dots and a trough is
|
|
9
|
+
* a field of pinpoints. The sheet fades out towards its far edge and the
|
|
10
|
+
* sides; where there are no dots the canvas is transparent.
|
|
11
|
+
*
|
|
12
|
+
* The sheet is a few long Gerstner swells (each point rides a small orbit,
|
|
13
|
+
* so crests gather the dots and troughs spread them), summed with
|
|
14
|
+
* analytic slopes for exact lighting. The pointer raises a soft swell
|
|
15
|
+
* where it rests; a click sends a ring out across the sheet.
|
|
16
|
+
*
|
|
17
|
+
* WebGL point sprites, one draw a frame: every dot is placed, lit and
|
|
18
|
+
* sized on the GPU, crisp and anti-aliased at any pixel ratio, sized in
|
|
19
|
+
* CSS px. Phones get a coarser grid. With prefers-reduced-motion, one
|
|
20
|
+
* still frame.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { createStage } from './stage.js';
|
|
24
|
+
import { createPoints, GLSL_PROJECT } from './points.js';
|
|
25
|
+
import { GLSL_OKLAB, parseColor, toOklab } from './color.js';
|
|
26
|
+
import { GLSL_QUIET, quietUniform } from './glsl.js';
|
|
27
|
+
import { canHover, isMobileGpu } from './device.js';
|
|
28
|
+
import { pointerIn, pressAt } from './pointer.js';
|
|
29
|
+
import { watchColors } from './mount.js';
|
|
30
|
+
import { pickColors } from './palettes.js';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {object} DotSurfaceConfig
|
|
34
|
+
* @property {string} [color='#ff8a1f'] The dots' colour.
|
|
35
|
+
* @property {string[]} [colors] A gradient instead (up to five), blended in Oklab, from near to far or left to right (see `gradient`).
|
|
36
|
+
* @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors` or `color`.
|
|
37
|
+
* @property {'depth' | 'width'} [gradient='depth'] Which way `colors` runs: near to far, or left to right.
|
|
38
|
+
* @property {number} [pitch=10] Space between dots, CSS px, at the middle of the sheet (nearer is wider, further closer).
|
|
39
|
+
* @property {number} [dot=6] The largest dot, CSS px, at the middle of the sheet.
|
|
40
|
+
* @property {number} [amplitude=1] How tall the swells are: 1 a gentle roll, 2 a heavy sea, 0 flat.
|
|
41
|
+
* @property {number} [speed=1] How fast the sheet rolls; 0 holds it still.
|
|
42
|
+
* @property {number} [tilt=42] The camera's angle above the sheet, degrees (10 grazing, 70 nearly overhead).
|
|
43
|
+
* @property {[number, number, number]} [light=[-0.5, 0.6, -0.6]] Where the light comes from: x right, y up, z into the distance.
|
|
44
|
+
* @property {boolean} [interactive=true] Whether the pointer raises a swell and a click sends a ring.
|
|
45
|
+
* @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
|
|
46
|
+
* An area kept dim behind copy: centre (0–1 from the top left), radius (a
|
|
47
|
+
* share of the height) and depth (0–1). The middle with `{}`.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/** Most colours a gradient takes. */
|
|
51
|
+
export const MAX_COLORS = 5;
|
|
52
|
+
/** Most click rings alive at once. */
|
|
53
|
+
const MAX_RINGS = 8;
|
|
54
|
+
/** Seconds a ring lasts. */
|
|
55
|
+
const RING_LIFE = 3.2;
|
|
56
|
+
/** How fast a ring runs out, sheet units a second (the frame is 2 units tall). */
|
|
57
|
+
const RING_SPEED = 0.6;
|
|
58
|
+
/** Where the sheet's far edge sits on screen when flat: clip-space y, 1 the top. */
|
|
59
|
+
const FAR_EDGE = 0.3;
|
|
60
|
+
/** Where its near edge sits: below the bottom, so it never shows. */
|
|
61
|
+
const NEAR_EDGE = -1.2;
|
|
62
|
+
/** The furthest the sheet reaches, sheet units, however low the camera. */
|
|
63
|
+
const MAX_FAR = 2.2;
|
|
64
|
+
/** The default colour: a warm orange. */
|
|
65
|
+
const COLOR = '#ff8a1f';
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* How far into the distance a point on the flat sheet lies, given where it
|
|
69
|
+
* lands on screen: GLSL_PROJECT's perspective (w = 1 + 0.45 z) undone for
|
|
70
|
+
* the plane y = 0, turned `tilt` towards the camera.
|
|
71
|
+
*
|
|
72
|
+
* @param {number} y Clip-space height on screen, -1 (bottom) to 1.
|
|
73
|
+
* @param {number} tilt The camera's angle above the sheet, radians.
|
|
74
|
+
* @returns {number} Depth on the sheet (larger is further), or Infinity
|
|
75
|
+
* when that height is at or above the horizon.
|
|
76
|
+
*/
|
|
77
|
+
export function sheetDepth(y, tilt) {
|
|
78
|
+
const s = Math.sin(tilt);
|
|
79
|
+
const below = s - 0.45 * Math.cos(tilt) * y;
|
|
80
|
+
return below > 1e-6 ? y / below : Infinity;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Where a point on the screen lies on the flat sheet: the inverse of
|
|
85
|
+
* `projectPoint([x, 0, z], 0, -tilt, aspect)`.
|
|
86
|
+
*
|
|
87
|
+
* @param {number} x Across the element, 0 (left) to 1.
|
|
88
|
+
* @param {number} y Up it, 0 (bottom) to 1, as `pointerIn` gives it.
|
|
89
|
+
* @param {number} tilt The camera's angle above the sheet, radians.
|
|
90
|
+
* @param {number} aspect The element's width over height.
|
|
91
|
+
* @returns {{ x: number, z: number } | null} Sheet coordinates, or null above the horizon.
|
|
92
|
+
*/
|
|
93
|
+
export function sheetAt(x, y, tilt, aspect) {
|
|
94
|
+
const z = sheetDepth(y * 2 - 1, tilt);
|
|
95
|
+
if (!Number.isFinite(z)) return null;
|
|
96
|
+
const w = 1 + 0.45 * Math.cos(tilt) * z;
|
|
97
|
+
return { x: (x * 2 - 1) * aspect * w, z };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The sheet's grid as particle data: every dot on a square grid `spacing`
|
|
102
|
+
* apart from just below the frame to the far edge, kept only where the flat
|
|
103
|
+
* sheet would land on screen (with a margin for the swells). Each dot is
|
|
104
|
+
* its place on the sheet (x, z), how far back it is (0 nearest, 1 the far
|
|
105
|
+
* edge) and how far across the screen it lands (0 left, 1 right).
|
|
106
|
+
*
|
|
107
|
+
* @param {number} aspect The element's width over height.
|
|
108
|
+
* @param {number} spacing Grid spacing, sheet units (the frame is 2 tall).
|
|
109
|
+
* @param {number} tilt The camera's angle above the sheet, radians.
|
|
110
|
+
* @returns {Float32Array} Four floats a dot.
|
|
111
|
+
*/
|
|
112
|
+
export function sheetGrid(aspect, spacing, tilt) {
|
|
113
|
+
const near = sheetDepth(NEAR_EDGE, tilt);
|
|
114
|
+
const far = Math.min(MAX_FAR, sheetDepth(FAR_EDGE, tilt));
|
|
115
|
+
const k = 0.45 * Math.cos(tilt);
|
|
116
|
+
const out = [];
|
|
117
|
+
const rows = Math.max(2, Math.round((far - near) / spacing) + 1);
|
|
118
|
+
for (let r = 0; r < rows; r++) {
|
|
119
|
+
const z = near + r * spacing;
|
|
120
|
+
const w = 1 + k * z;
|
|
121
|
+
// Columns centred on the middle, wide enough to cover the frame at this depth.
|
|
122
|
+
const half = Math.ceil((aspect * w * 1.12) / spacing);
|
|
123
|
+
for (let c = -half; c <= half; c++) {
|
|
124
|
+
const x = c * spacing;
|
|
125
|
+
out.push(x, z, r / (rows - 1), (x / (aspect * w)) * 0.5 + 0.5);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return new Float32Array(out);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* A gradient as the shader takes it: each colour in Oklab, padded to
|
|
133
|
+
* MAX_COLORS by repeating the last.
|
|
134
|
+
*
|
|
135
|
+
* @param {string[]} colors CSS hex colours, at least one; past MAX_COLORS are dropped.
|
|
136
|
+
* @returns {Float32Array} Three channels a colour, MAX_COLORS colours.
|
|
137
|
+
* @throws {TypeError} When a colour is not a hex colour.
|
|
138
|
+
*/
|
|
139
|
+
export function gradientUniform(colors) {
|
|
140
|
+
const list = colors.slice(0, MAX_COLORS);
|
|
141
|
+
return new Float32Array(Array.from({ length: MAX_COLORS }, (_, i) => toOklab(parseColor(list[Math.min(i, list.length - 1)]))).flat());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Each dot: placed on the rolling sheet, lit, sized and coloured. `p` is
|
|
146
|
+
* (x, z, depth 0–1, across 0–1) from `sheetGrid`.
|
|
147
|
+
*/
|
|
148
|
+
const VERTEX = `
|
|
149
|
+
precision highp float;
|
|
150
|
+
attribute vec4 p;
|
|
151
|
+
uniform vec2 uRes;
|
|
152
|
+
uniform float uTime;
|
|
153
|
+
uniform float uRatio;
|
|
154
|
+
uniform vec2 uShift;
|
|
155
|
+
uniform float uTilt;
|
|
156
|
+
uniform float uAmp;
|
|
157
|
+
uniform float uDot;
|
|
158
|
+
uniform float uMaxPoint;
|
|
159
|
+
uniform vec3 uLight;
|
|
160
|
+
uniform vec3 uInk[${MAX_COLORS}];
|
|
161
|
+
uniform float uInks;
|
|
162
|
+
uniform float uAcross;
|
|
163
|
+
uniform vec4 uPointer;
|
|
164
|
+
uniform vec4 uRings[${MAX_RINGS}];
|
|
165
|
+
varying vec3 vColour;
|
|
166
|
+
varying float vAlpha;
|
|
167
|
+
varying float vSize;
|
|
168
|
+
${GLSL_PROJECT}
|
|
169
|
+
${GLSL_OKLAB}
|
|
170
|
+
${GLSL_QUIET}
|
|
171
|
+
|
|
172
|
+
// One Gerstner swell: direction, wavelength, height (a share of the whole)
|
|
173
|
+
// and how fast it runs. Adds its height and slope to h, and its orbit to d.
|
|
174
|
+
void swell(vec2 at, vec2 dir, float len, float amp, float rate, float phase, inout vec3 h, inout vec2 d) {
|
|
175
|
+
float k = 6.2831853 / len;
|
|
176
|
+
float a = dot(at, dir) * k - uTime * rate + phase;
|
|
177
|
+
float s = sin(a), c = cos(a);
|
|
178
|
+
h += vec3(amp * s, amp * k * c * dir);
|
|
179
|
+
// The orbit: towards the crest as it passes, so crests gather dots.
|
|
180
|
+
d += dir * (amp * 0.8 * c);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// The gradient's colour at t, 0–1, in Oklab.
|
|
184
|
+
vec3 ink(float t) {
|
|
185
|
+
float x = clamp(t, 0.0, 1.0) * (uInks - 1.0);
|
|
186
|
+
float i = min(floor(x), max(uInks - 2.0, 0.0));
|
|
187
|
+
vec3 a = uInk[0], b = uInk[0];
|
|
188
|
+
for (int n = 0; n < ${MAX_COLORS - 1}; n++) {
|
|
189
|
+
if (float(n) == i) { a = uInk[n]; b = uInk[n + 1]; }
|
|
190
|
+
}
|
|
191
|
+
return uInks > 1.0 ? mix(a, b, x - i) : a;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
void main() {
|
|
195
|
+
vec2 at = p.xy;
|
|
196
|
+
// The swells: h is (height, slope across, slope back), in units of uAmp.
|
|
197
|
+
vec3 h = vec3(0.0);
|
|
198
|
+
vec2 d = vec2(0.0);
|
|
199
|
+
swell(at, vec2(0.55, 0.84), 1.55, 0.40, 0.24, 0.0, h, d);
|
|
200
|
+
swell(at, vec2(0.96, -0.28), 2.3, 0.30, 0.17, 1.7, h, d);
|
|
201
|
+
swell(at, vec2(-0.5, 0.87), 1.05, 0.19, 0.29, 4.1, h, d);
|
|
202
|
+
swell(at, vec2(0.12, -0.99), 0.72, 0.11, 0.36, 2.3, h, d);
|
|
203
|
+
h *= uAmp;
|
|
204
|
+
d *= uAmp;
|
|
205
|
+
float lift = 0.0;
|
|
206
|
+
float glow = 0.0;
|
|
207
|
+
|
|
208
|
+
// The pointer's swell: a soft mound with a faint ring round it.
|
|
209
|
+
vec2 toP = at - uPointer.xy;
|
|
210
|
+
float dp = length(toP);
|
|
211
|
+
float mound = exp(-dp * dp / (uPointer.w * uPointer.w)) * uPointer.z;
|
|
212
|
+
float ring = 1.0 + 0.25 * cos(dp * 14.0 - uTime * 2.2);
|
|
213
|
+
h.x += mound * 0.08 * ring;
|
|
214
|
+
h.yz += toP * (-2.0 / (uPointer.w * uPointer.w)) * mound * 0.08;
|
|
215
|
+
lift += mound * 0.22;
|
|
216
|
+
|
|
217
|
+
// The click rings: a crest running outward, widening and fading.
|
|
218
|
+
for (int n = 0; n < ${MAX_RINGS}; n++) {
|
|
219
|
+
vec4 r = uRings[n];
|
|
220
|
+
if (r.w <= 0.0) continue;
|
|
221
|
+
vec2 off = at - r.xy;
|
|
222
|
+
float dist = length(off) + 1e-4;
|
|
223
|
+
float radius = r.z * ${RING_SPEED.toFixed(2)};
|
|
224
|
+
float width = 0.09 + r.z * 0.05;
|
|
225
|
+
float o = (dist - radius) / width;
|
|
226
|
+
if (abs(o) > 3.0) continue;
|
|
227
|
+
float g = exp(-o * o) * r.w;
|
|
228
|
+
h.x += g * 0.035;
|
|
229
|
+
h.yz += (off / dist) * (-2.0 * o / width) * g * 0.035;
|
|
230
|
+
// Dots are pushed out ahead of the crest and drawn back behind it: the grid visibly bends as it passes.
|
|
231
|
+
d += (off / dist) * (-o * g * 0.03);
|
|
232
|
+
glow += g;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Lighting: how far the slope turns the sheet towards the light, from flat.
|
|
236
|
+
vec3 n = normalize(vec3(-h.y, 1.0, -h.z));
|
|
237
|
+
vec3 L = normalize(uLight);
|
|
238
|
+
float lit = 0.48 + (dot(n, L) - L.y) * 2.1 + h.x * 2.0 + lift;
|
|
239
|
+
float b = min(1.0, smoothstep(0.0, 1.0, lit) + glow * 0.5);
|
|
240
|
+
|
|
241
|
+
// Place it: on the sheet, turned towards the camera, in perspective.
|
|
242
|
+
vec3 v = vec3(at.x + d.x, h.x, at.y + d.y);
|
|
243
|
+
v = rotateX(v, -uTilt);
|
|
244
|
+
float aspect = uRes.x / uRes.y;
|
|
245
|
+
vec4 pos = project(v, aspect);
|
|
246
|
+
gl_Position = pos + vec4(uShift * pos.w, 0.0, 0.0);
|
|
247
|
+
vec2 ndc = pos.xy / pos.w;
|
|
248
|
+
|
|
249
|
+
// Fading out: into the distance, towards the sides, and in the quiet area.
|
|
250
|
+
float fade = (1.0 - smoothstep(0.4, 1.0, p.z)) * (1.0 - smoothstep(0.62, 1.04, abs(ndc.x)));
|
|
251
|
+
float quiet = mix(1.0 - uQuiet.w, 1.0, quietOpen(vec2(ndc.x * aspect, ndc.y) * 0.5));
|
|
252
|
+
fade *= quiet;
|
|
253
|
+
|
|
254
|
+
// Size: from a pinpoint in the troughs to the full dot on a lit crest,
|
|
255
|
+
// nearer dots larger. Below a pixel and a half the sprite stays that
|
|
256
|
+
// size and fades instead, so pinpoints stay round rather than flicker.
|
|
257
|
+
float size = uDot * mix(0.2, 1.0, b * (0.4 + 0.6 * b)) * (1.0 + 0.3 * min(glow, 1.0)) * mix(0.55, 1.0, fade) / pos.w * uRatio;
|
|
258
|
+
float shown = max(size, 1.5 * uRatio);
|
|
259
|
+
vSize = min(shown, uMaxPoint - 1.0);
|
|
260
|
+
gl_PointSize = vSize + 1.0;
|
|
261
|
+
vAlpha = mix(0.3, 1.0, b) * fade * (size / shown) * (size / shown);
|
|
262
|
+
|
|
263
|
+
// Colour: the gradient, lightened a touch on the brightest crests.
|
|
264
|
+
vec3 lab = ink(mix(p.z, p.w, uAcross));
|
|
265
|
+
lab.x += 0.07 * b * b + 0.1 * min(glow, 1.0);
|
|
266
|
+
vColour = fromOklab(lab);
|
|
267
|
+
}
|
|
268
|
+
`;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* A crisp round dot `vSize` px across, anti-aliased over one pixel. Drawn
|
|
272
|
+
* with the MAX blend where the context has it, so overlapping dots merge.
|
|
273
|
+
*/
|
|
274
|
+
const DOT = `
|
|
275
|
+
precision highp float;
|
|
276
|
+
varying vec3 vColour;
|
|
277
|
+
varying float vAlpha;
|
|
278
|
+
varying float vSize;
|
|
279
|
+
void main() {
|
|
280
|
+
float r = length(gl_PointCoord - 0.5) * (vSize + 1.0);
|
|
281
|
+
float a = clamp(vSize * 0.5 - r + 0.5, 0.0, 1.0) * vAlpha;
|
|
282
|
+
gl_FragColor = vec4(vColour * a, a);
|
|
283
|
+
}
|
|
284
|
+
`;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The stage's own full-screen pass has nothing to paint (the canvas is
|
|
288
|
+
* transparent and cleared each frame), so its triangle sits beyond the far
|
|
289
|
+
* plane and is clipped away: the dots are drawn after it.
|
|
290
|
+
*/
|
|
291
|
+
const NOTHING = {
|
|
292
|
+
vertex: 'attribute vec2 p; void main() { gl_Position = vec4(p, 2.0, 1.0); }',
|
|
293
|
+
fragment: 'precision mediump float; void main() { gl_FragColor = vec4(0.0); }',
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Starts the dot surface on an element (its first canvas is drawn into).
|
|
298
|
+
*
|
|
299
|
+
* @param {HTMLElement} el The element.
|
|
300
|
+
* @param {DotSurfaceConfig} config The settings.
|
|
301
|
+
* @returns {() => void} Stops it.
|
|
302
|
+
*/
|
|
303
|
+
export function dotSurface(el, config) {
|
|
304
|
+
const tilt = ((config.tilt ?? 42) * Math.PI) / 180;
|
|
305
|
+
const pitch = (config.pitch ?? 10) * (isMobileGpu() ? 1.3 : 1);
|
|
306
|
+
const speed = config.speed ?? 1;
|
|
307
|
+
const interactive = config.interactive !== false;
|
|
308
|
+
const light = config.light ?? [-0.5, 0.6, -0.6];
|
|
309
|
+
const start = !config.colors?.length && config.color ? [config.color] : pickColors(config, [COLOR]);
|
|
310
|
+
let ink = gradientUniform(start);
|
|
311
|
+
let inks = Math.min(MAX_COLORS, start.length);
|
|
312
|
+
/** @type {import('./points.js').Points | null} */
|
|
313
|
+
let points = null;
|
|
314
|
+
let maxPoint = 64;
|
|
315
|
+
/** The MAX blend equation, where the context has it. @type {number | null} */
|
|
316
|
+
let lighter = null;
|
|
317
|
+
// The sheet's own clock: it advances with `speed`, so pausing and resuming do not
|
|
318
|
+
// jump. It starts where the swells have built a lit ridge (the still frame too).
|
|
319
|
+
let time = 7;
|
|
320
|
+
/** The pointer's swell: where on the sheet it is heading (tx, tz), where it is (x, z), and how strong (on, 0–1). */
|
|
321
|
+
const swell = { tx: 0, tz: 0, x: 0, z: 0, on: 0 };
|
|
322
|
+
/** @type {{ x: number, z: number, t0: number }[]} */
|
|
323
|
+
const rings = [];
|
|
324
|
+
const ringData = new Float32Array(MAX_RINGS * 4);
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Follows the pointer: the swell eases towards it, and fades when it leaves.
|
|
328
|
+
*
|
|
329
|
+
* @param {number} aspect The canvas's width over height.
|
|
330
|
+
* @param {number} dt Seconds since the last frame.
|
|
331
|
+
*/
|
|
332
|
+
const follow = (aspect, dt) => {
|
|
333
|
+
const p = pointerIn(el);
|
|
334
|
+
const at = p.inside ? sheetAt(p.x, p.y, tilt, aspect) : null;
|
|
335
|
+
if (at) {
|
|
336
|
+
if (swell.on < 0.01) Object.assign(swell, { x: at.x, z: at.z });
|
|
337
|
+
swell.tx = at.x;
|
|
338
|
+
swell.tz = at.z;
|
|
339
|
+
}
|
|
340
|
+
const k = 1 - Math.exp(-dt * 3);
|
|
341
|
+
swell.x += (swell.tx - swell.x) * k;
|
|
342
|
+
swell.z += (swell.tz - swell.z) * k;
|
|
343
|
+
swell.on += ((at ? 1 : 0) - swell.on) * (1 - Math.exp(-dt * 2));
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* The live rings as `uRings` takes them: place, age and strength.
|
|
348
|
+
*
|
|
349
|
+
* @returns {Float32Array} Four floats a ring, zeros for none.
|
|
350
|
+
*/
|
|
351
|
+
const ringUniform = () => {
|
|
352
|
+
const now = performance.now() / 1000;
|
|
353
|
+
while (rings.length && now - rings[0].t0 > RING_LIFE) rings.shift();
|
|
354
|
+
ringData.fill(0);
|
|
355
|
+
rings.forEach((r, i) => {
|
|
356
|
+
const age = now - r.t0;
|
|
357
|
+
const k = 1 - age / RING_LIFE;
|
|
358
|
+
ringData[i * 4] = r.x;
|
|
359
|
+
ringData[i * 4 + 1] = r.z;
|
|
360
|
+
ringData[i * 4 + 2] = age;
|
|
361
|
+
ringData[i * 4 + 3] = k * k;
|
|
362
|
+
});
|
|
363
|
+
return ringData;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const { stage, stop } = createStage(el, {
|
|
367
|
+
...NOTHING,
|
|
368
|
+
setup({ gl }) {
|
|
369
|
+
points = createPoints(gl, VERTEX, DOT);
|
|
370
|
+
if (!points) return false;
|
|
371
|
+
maxPoint = gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE)[1];
|
|
372
|
+
lighter = 'MAX' in gl ? gl.MAX : (gl.getExtension('EXT_blend_minmax')?.MAX_EXT ?? null);
|
|
373
|
+
},
|
|
374
|
+
resize({ canvas, ratio }) {
|
|
375
|
+
const height = canvas.height / ratio;
|
|
376
|
+
points?.upload(sheetGrid(canvas.width / canvas.height, pitch / (height / 2), tilt));
|
|
377
|
+
},
|
|
378
|
+
after({ gl, canvas, ratio, still }, _now, dt) {
|
|
379
|
+
if (!points) return;
|
|
380
|
+
const aspect = canvas.width / canvas.height;
|
|
381
|
+
if (!still) {
|
|
382
|
+
time += dt * speed;
|
|
383
|
+
if (interactive && canHover()) follow(aspect, dt);
|
|
384
|
+
}
|
|
385
|
+
const u = points.program.uniform;
|
|
386
|
+
// Where dots crowd on a crest they merge rather than add up to a hot spot.
|
|
387
|
+
if (lighter) gl.blendEquation(lighter);
|
|
388
|
+
points.passes(0, () => {
|
|
389
|
+
gl.uniform2f(u('uRes'), canvas.width, canvas.height);
|
|
390
|
+
gl.uniform1f(u('uTime'), time);
|
|
391
|
+
gl.uniform1f(u('uRatio'), ratio);
|
|
392
|
+
gl.uniform1f(u('uTilt'), tilt);
|
|
393
|
+
gl.uniform1f(u('uAmp'), 0.16 * (config.amplitude ?? 1));
|
|
394
|
+
gl.uniform1f(u('uDot'), config.dot ?? 6);
|
|
395
|
+
gl.uniform1f(u('uMaxPoint'), maxPoint);
|
|
396
|
+
gl.uniform3f(u('uLight'), light[0], light[1], light[2]);
|
|
397
|
+
gl.uniform3fv(u('uInk'), ink);
|
|
398
|
+
gl.uniform1f(u('uInks'), inks);
|
|
399
|
+
gl.uniform1f(u('uAcross'), config.gradient === 'width' ? 1 : 0);
|
|
400
|
+
gl.uniform4f(u('uPointer'), swell.x, swell.z, swell.on, 0.36);
|
|
401
|
+
gl.uniform4fv(u('uRings'), ringUniform());
|
|
402
|
+
gl.uniform4f(u('uQuiet'), ...quietUniform(config.quiet, aspect));
|
|
403
|
+
});
|
|
404
|
+
gl.blendEquation(gl.FUNC_ADD);
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
if (!stage) return stop;
|
|
408
|
+
|
|
409
|
+
/** @param {PointerEvent} e The press: a ring from where it landed on the sheet. */
|
|
410
|
+
const press = (e) => {
|
|
411
|
+
const at = pressAt(el, e);
|
|
412
|
+
const spot = at && sheetAt(at.x, at.y, tilt, el.clientWidth / (el.clientHeight || 1));
|
|
413
|
+
if (!spot) return;
|
|
414
|
+
rings.push({ ...spot, t0: performance.now() / 1000 });
|
|
415
|
+
while (rings.length > MAX_RINGS) rings.shift();
|
|
416
|
+
};
|
|
417
|
+
if (interactive && !stage.still) document.addEventListener('pointerdown', press, { passive: true });
|
|
418
|
+
|
|
419
|
+
const unwatch = watchColors(el, (next) => {
|
|
420
|
+
ink = gradientUniform(next.colors);
|
|
421
|
+
inks = Math.min(MAX_COLORS, next.colors.length);
|
|
422
|
+
if (stage.still) stage.redraw();
|
|
423
|
+
});
|
|
424
|
+
return () => {
|
|
425
|
+
stop();
|
|
426
|
+
unwatch();
|
|
427
|
+
document.removeEventListener('pointerdown', press);
|
|
428
|
+
};
|
|
429
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { GL } from './gl.js';
|
|
2
|
+
|
|
3
|
+
/** A picture that fades each frame while new things are drawn into it: trails. */
|
|
4
|
+
export interface Feedback {
|
|
5
|
+
/** Sizes it for a canvas of that many pixels (at its `scale`), cleared. */
|
|
6
|
+
resize(width: number, height: number): void;
|
|
7
|
+
/** One frame: the last times `fade` (0 clears, 1 keeps all), then `add` draws on top into it. Leaves the canvas bound. */
|
|
8
|
+
draw(fade: number, add: () => void): void;
|
|
9
|
+
/** The latest frame. */
|
|
10
|
+
texture(): WebGLTexture;
|
|
11
|
+
/** Its size in pixels. */
|
|
12
|
+
size(): { width: number; height: number };
|
|
13
|
+
/** Frees its textures, framebuffers and program. */
|
|
14
|
+
dispose(): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Makes a feedback buffer, `scale` of the canvas's size, in half floats where possible with `precise`; null if its program does not compile. Uses texture unit 0 while drawing. */
|
|
18
|
+
export function createFeedback(gl: GL, options?: { scale?: number; precise?: boolean }): Feedback | null;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/feedback
|
|
3
|
+
*
|
|
4
|
+
* A picture that fades a little every frame while new things are drawn
|
|
5
|
+
* into it, so whatever moves leaves a trail: particles streak, the path a
|
|
6
|
+
* pointer took lingers and dies away. Two textures take turns: each frame
|
|
7
|
+
* the last is copied into the other, dimmed, and the effect draws on top.
|
|
8
|
+
*
|
|
9
|
+
* Kept in half floats where the GPU can draw into them (`precise`), so long
|
|
10
|
+
* fades stay smooth; otherwise in bytes, where a small floor is taken off
|
|
11
|
+
* as well, or the dimming would stall on the last faint level and leave a
|
|
12
|
+
* stain. Uses texture unit 0 while it draws; effects keep their own
|
|
13
|
+
* textures on other units.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { createProgram, bindTriangle, VERTEX } from './gl.js';
|
|
17
|
+
|
|
18
|
+
/** The fade: the last frame, dimmed and floored. */
|
|
19
|
+
const FADE = `
|
|
20
|
+
precision highp float;
|
|
21
|
+
uniform sampler2D uLast;
|
|
22
|
+
uniform float uFade;
|
|
23
|
+
uniform float uFloor;
|
|
24
|
+
varying vec2 vUv;
|
|
25
|
+
void main() {
|
|
26
|
+
gl_FragColor = max(texture2D(uLast, vUv) * uFade - uFloor, 0.0);
|
|
27
|
+
}
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {object} Feedback
|
|
32
|
+
* @property {(width: number, height: number) => void} resize Sizes it for a
|
|
33
|
+
* canvas of that many pixels (at its `scale`), cleared.
|
|
34
|
+
* @property {(fade: number, add: () => void) => void} draw One frame: the
|
|
35
|
+
* last, times `fade` (0 clears, 1 keeps everything), then `add` draws on
|
|
36
|
+
* top with the target bound and its viewport set. Leaves the canvas bound.
|
|
37
|
+
* @property {() => WebGLTexture} texture The latest frame.
|
|
38
|
+
* @property {() => { width: number, height: number }} size Its size in pixels.
|
|
39
|
+
* @property {() => void} dispose Frees its textures, framebuffers and program.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Makes a feedback buffer.
|
|
44
|
+
*
|
|
45
|
+
* @param {WebGLRenderingContext | WebGL2RenderingContext} gl The context.
|
|
46
|
+
* @param {object} [options]
|
|
47
|
+
* @param {number} [options.scale=1] Its size as a share of the canvas's.
|
|
48
|
+
* @param {boolean} [options.precise=false] Half floats where they can be drawn into.
|
|
49
|
+
* @returns {Feedback | null} The buffer, or null when its program does not compile.
|
|
50
|
+
*/
|
|
51
|
+
export function createFeedback(gl, { scale = 1, precise = false } = {}) {
|
|
52
|
+
const fade = createProgram(gl, VERTEX, FADE);
|
|
53
|
+
if (!fade) return null;
|
|
54
|
+
const triangle = bindTriangle(gl);
|
|
55
|
+
const gl2 = typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext ? gl : null;
|
|
56
|
+
let half = !!(precise && gl2 && (gl2.getExtension('EXT_color_buffer_float') || gl2.getExtension('EXT_color_buffer_half_float')));
|
|
57
|
+
const targets = [0, 1].map(() => ({
|
|
58
|
+
texture: /** @type {WebGLTexture} */ (gl.createTexture()),
|
|
59
|
+
fbo: /** @type {WebGLFramebuffer} */ (gl.createFramebuffer()),
|
|
60
|
+
}));
|
|
61
|
+
let width = 1;
|
|
62
|
+
let height = 1;
|
|
63
|
+
let current = 0;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Allocates both textures at the current size, in half floats or bytes,
|
|
67
|
+
* and clears them.
|
|
68
|
+
*
|
|
69
|
+
* @returns {boolean} Whether the GPU can draw into them.
|
|
70
|
+
*/
|
|
71
|
+
const allocate = () => {
|
|
72
|
+
for (const { texture, fbo } of targets) {
|
|
73
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
74
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
75
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
76
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
77
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
78
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
79
|
+
if (half && gl2) gl2.texImage2D(gl.TEXTURE_2D, 0, gl2.RGBA16F, width, height, 0, gl.RGBA, gl2.HALF_FLOAT, null);
|
|
80
|
+
else gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
|
81
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
|
82
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
|
|
83
|
+
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) return false;
|
|
84
|
+
gl.viewport(0, 0, width, height);
|
|
85
|
+
gl.clearColor(0, 0, 0, 0);
|
|
86
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
resize(canvasWidth, canvasHeight) {
|
|
93
|
+
width = Math.max(1, Math.round(canvasWidth * scale));
|
|
94
|
+
height = Math.max(1, Math.round(canvasHeight * scale));
|
|
95
|
+
// Half floats that cannot be drawn into after all: bytes instead.
|
|
96
|
+
if (!allocate() && half) {
|
|
97
|
+
half = false;
|
|
98
|
+
allocate();
|
|
99
|
+
}
|
|
100
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
101
|
+
},
|
|
102
|
+
draw(amount, add) {
|
|
103
|
+
const last = targets[current];
|
|
104
|
+
const next = targets[1 - current];
|
|
105
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, next.fbo);
|
|
106
|
+
gl.viewport(0, 0, width, height);
|
|
107
|
+
gl.disable(gl.BLEND);
|
|
108
|
+
gl.useProgram(fade.program);
|
|
109
|
+
bindTriangle(gl, triangle);
|
|
110
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
111
|
+
gl.bindTexture(gl.TEXTURE_2D, last.texture);
|
|
112
|
+
gl.uniform1i(fade.uniform('uLast'), 0);
|
|
113
|
+
gl.uniform1f(fade.uniform('uFade'), amount);
|
|
114
|
+
gl.uniform1f(fade.uniform('uFloor'), half ? 0.0004 : 1.5 / 255);
|
|
115
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
116
|
+
add();
|
|
117
|
+
current = 1 - current;
|
|
118
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
119
|
+
},
|
|
120
|
+
texture: () => targets[current].texture,
|
|
121
|
+
size: () => ({ width, height }),
|
|
122
|
+
dispose() {
|
|
123
|
+
for (const { texture, fbo } of targets) {
|
|
124
|
+
gl.deleteTexture(texture);
|
|
125
|
+
gl.deleteFramebuffer(fbo);
|
|
126
|
+
}
|
|
127
|
+
gl.deleteBuffer(triangle);
|
|
128
|
+
gl.deleteProgram(fade.program);
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Fits one element's single line of text to its parent's content width, once. */
|
|
2
|
+
export function fit(el: HTMLElement): void;
|
|
3
|
+
|
|
4
|
+
/** Keeps an element's text fitted to its parent's width (on resize and font load). Returns a function that stops it. */
|
|
5
|
+
export function fitText(el: HTMLElement): () => void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/fit-text
|
|
3
|
+
*
|
|
4
|
+
* One line of text sized to exactly span its parent's content width,
|
|
5
|
+
* whatever the text and the window: a footer wordmark that runs edge to
|
|
6
|
+
* edge. A viewport-based CSS size cannot do it (it does not know how long
|
|
7
|
+
* the text is, and stops growing at its cap).
|
|
8
|
+
*
|
|
9
|
+
* The text is measured with a Range (the text itself, not its box) at a
|
|
10
|
+
* reference size and scaled from there. It refits when the parent's width
|
|
11
|
+
* changes and once web fonts have loaded (a fallback font is a different
|
|
12
|
+
* width). The CSS size stays as the fallback without script.
|
|
13
|
+
*
|
|
14
|
+
* From the SampleGrid (WaveGrid) theme.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { textRange } from './text.js';
|
|
18
|
+
|
|
19
|
+
/** The size the text is measured at, in px. */
|
|
20
|
+
const REFERENCE = 100;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Fits one element's text to its parent's content box.
|
|
24
|
+
*
|
|
25
|
+
* @param {HTMLElement} el The element whose text should fill the line.
|
|
26
|
+
*/
|
|
27
|
+
export function fit(el) {
|
|
28
|
+
const parent = el.parentElement;
|
|
29
|
+
if (!parent) return;
|
|
30
|
+
const cs = getComputedStyle(parent);
|
|
31
|
+
const available = parent.clientWidth - parseFloat(cs.paddingLeft) - parseFloat(cs.paddingRight);
|
|
32
|
+
if (!(available > 0)) return;
|
|
33
|
+
el.style.fontSize = `${REFERENCE}px`;
|
|
34
|
+
const natural = textRange(el)?.getBoundingClientRect().width ?? 0;
|
|
35
|
+
if (!(natural > 0)) return;
|
|
36
|
+
// A hair under the exact fit, so rounding never wraps or clips the last letter.
|
|
37
|
+
el.style.fontSize = `${Math.floor(((REFERENCE * available) / natural) * 0.995 * 100) / 100}px`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Keeps an element's text fitted to its parent's width.
|
|
42
|
+
*
|
|
43
|
+
* @param {HTMLElement} el The element.
|
|
44
|
+
* @returns {() => void} Stops refitting.
|
|
45
|
+
*/
|
|
46
|
+
export function fitText(el) {
|
|
47
|
+
fit(el);
|
|
48
|
+
document.fonts?.ready.then(() => fit(el)).catch(() => {});
|
|
49
|
+
// Only a width change refits: the refit itself changes the parent's
|
|
50
|
+
// height, and reacting to that would loop.
|
|
51
|
+
let width = -1;
|
|
52
|
+
const observer = new ResizeObserver(([entry]) => {
|
|
53
|
+
const w = Math.round(entry.contentRect.width);
|
|
54
|
+
if (w === width) return;
|
|
55
|
+
width = w;
|
|
56
|
+
fit(el);
|
|
57
|
+
});
|
|
58
|
+
if (el.parentElement) observer.observe(el.parentElement);
|
|
59
|
+
return () => observer.disconnect();
|
|
60
|
+
}
|