@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,55 @@
|
|
|
1
|
+
import type { Rgb } from './color.js';
|
|
2
|
+
|
|
3
|
+
export interface FluidOptions {
|
|
4
|
+
/** Swirl kept alive between strokes (vorticity confinement). Default 28. */
|
|
5
|
+
curl?: number;
|
|
6
|
+
/** How fast motion dies away, per second. Default 0.25. */
|
|
7
|
+
velocityFade?: number;
|
|
8
|
+
/** How fast the dye fades, per second. Default 0.55. */
|
|
9
|
+
inkFade?: number;
|
|
10
|
+
/** How much the dye spreads and softens as it moves, 0–1. Default 0. */
|
|
11
|
+
diffusion?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface Fluid {
|
|
15
|
+
/**
|
|
16
|
+
* Pushes the fluid at (x, y) (0–1 across, 0–1 up) by (dx, dy), in
|
|
17
|
+
* velocity-grid cells a second, and adds `dye` ([0, 0, 0] for none).
|
|
18
|
+
* `radius` is a share of the height (default 0.0025).
|
|
19
|
+
*/
|
|
20
|
+
splat(x: number, y: number, dx: number, dy: number, dye: Rgb, radius?: number): void;
|
|
21
|
+
/** Moves the fluid on by `dt` seconds. */
|
|
22
|
+
step(dt: number): void;
|
|
23
|
+
/** Binds the dye and the velocity to texture units for another shader. */
|
|
24
|
+
bind(dyeUnit: number, velocityUnit: number): void;
|
|
25
|
+
/** The target's width over height, so splats stay round. */
|
|
26
|
+
resize(aspect: number): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A Stable Fluids solve on a WebGL 2 context; null where half-float targets cannot be drawn to. */
|
|
30
|
+
export function createFluid(gl: WebGL2RenderingContext, options?: FluidOptions): Fluid | null;
|
|
31
|
+
|
|
32
|
+
export interface StirOptions {
|
|
33
|
+
/** The ink's palette, looped through as it is laid. */
|
|
34
|
+
colors: Rgb[];
|
|
35
|
+
/** How hard the pointer pushes the fluid. Default 1. */
|
|
36
|
+
force?: number;
|
|
37
|
+
/** How much ink a stroke leaves, 0 for none. Default 0.16. */
|
|
38
|
+
trail?: number;
|
|
39
|
+
/** Whether a click or tap drops a bloom of ink. Default true. */
|
|
40
|
+
drops?: boolean;
|
|
41
|
+
/** The size of a stroke's splats and of a drop, 1 as standard. */
|
|
42
|
+
radius?: number;
|
|
43
|
+
/** Clicks on these are theirs, not the fluid's. Default: links, buttons, form fields and `[data-effect-ignore]`. */
|
|
44
|
+
ignore?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Connects a fluid to the pointer over `el`. Call `update(dt)` each frame
|
|
49
|
+
* before `fluid.step`; `setColors` changes the ink; `destroy` stops listening.
|
|
50
|
+
*/
|
|
51
|
+
export function stir(fluid: Fluid, el: HTMLElement, options: StirOptions): {
|
|
52
|
+
update(dt: number): void;
|
|
53
|
+
setColors(colors: Rgb[]): void;
|
|
54
|
+
destroy(): void;
|
|
55
|
+
};
|
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/fluid
|
|
3
|
+
*
|
|
4
|
+
* A real fluid, simulated on the GPU: Jos Stam's Stable Fluids, after Pavel
|
|
5
|
+
* Dobryakov's WebGL-Fluid-Simulation (MIT). WebGL 2 with half-float render
|
|
6
|
+
* targets, which every current browser has.
|
|
7
|
+
*
|
|
8
|
+
* Two fields live in textures across the element: the velocity of the
|
|
9
|
+
* fluid and the dye it carries. Each step swirls the velocity (vorticity
|
|
10
|
+
* confinement keeps eddies alive after a stroke ends), makes it
|
|
11
|
+
* incompressible (a pressure solve), then moves both fields along it and
|
|
12
|
+
* lets them fade. `splat` pushes the fluid and drops dye at a point.
|
|
13
|
+
*
|
|
14
|
+
* `stir` connects a fluid to the pointer: strokes push it and leave a trace
|
|
15
|
+
* of ink, clicks drop a bloom that bursts open and curls. Ink colours loop
|
|
16
|
+
* through a palette, blended in Oklab.
|
|
17
|
+
*
|
|
18
|
+
* `step` and `splat` leave their own program, framebuffer and viewport
|
|
19
|
+
* bound: whoever draws the fluid restores theirs before drawing. Every
|
|
20
|
+
* program here reads its one attribute, `p`, at location 0.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const fluid = createFluid(gl);
|
|
24
|
+
* fluid?.splat(0.5, 0.5, 300, 0, [0.4, 0.1, 0.9]);
|
|
25
|
+
* fluid?.step(dt);
|
|
26
|
+
* fluid?.bind(3, 4); // dye on unit 3, velocity on unit 4, for your shader
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { createProgram, bindTriangle } from './gl.js';
|
|
30
|
+
import { paletteAt } from './color.js';
|
|
31
|
+
import { trail, pressAt, CLICK_IGNORE } from './pointer.js';
|
|
32
|
+
import { canHover } from './device.js';
|
|
33
|
+
|
|
34
|
+
/** The velocity grid, cells a side: the motion is smooth, so it can be coarse. */
|
|
35
|
+
const SIM = 128;
|
|
36
|
+
/** The dye grid, cells a side: finer, as the dye is what is seen. */
|
|
37
|
+
const DYE = 512;
|
|
38
|
+
const PRESSURE_ITERATIONS = 20;
|
|
39
|
+
/** Pressure carried into the next solve, a share of the last. */
|
|
40
|
+
const PRESSURE = 0.8;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @typedef {import('./color.js').Rgb} Rgb
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {object} FluidOptions
|
|
48
|
+
* @property {number} [curl=28] Swirl kept alive between strokes (vorticity confinement).
|
|
49
|
+
* @property {number} [velocityFade=0.25] How fast motion dies away, per second.
|
|
50
|
+
* @property {number} [inkFade=0.55] How fast the dye fades, per second.
|
|
51
|
+
* @property {number} [diffusion=0] How much the dye spreads and softens as it moves, 0–1.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @typedef {object} Fluid
|
|
56
|
+
* @property {(x: number, y: number, dx: number, dy: number, dye: Rgb, radius?: number) => void} splat
|
|
57
|
+
* Pushes the fluid at a point (x 0–1 across, y 0–1 up) by (dx, dy), in
|
|
58
|
+
* velocity-grid cells a second, and adds dye there ([0, 0, 0] for none).
|
|
59
|
+
* `radius` is the splat's size, a share of the height (default 0.0025).
|
|
60
|
+
* @property {(dt: number) => void} step Moves the fluid on by `dt` seconds.
|
|
61
|
+
* @property {(dyeUnit: number, velocityUnit: number) => void} bind Binds the
|
|
62
|
+
* dye and the velocity to texture units, for another shader to sample.
|
|
63
|
+
* @property {(aspect: number) => void} resize Tells it the target's width
|
|
64
|
+
* over height, so splats stay round.
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
/** The vertex shader every pass shares: `vUv` and its four neighbours, one texel away. */
|
|
68
|
+
const VERT = `
|
|
69
|
+
precision highp float;
|
|
70
|
+
attribute vec2 p;
|
|
71
|
+
uniform vec2 texel;
|
|
72
|
+
varying vec2 vUv, vL, vR, vT, vB;
|
|
73
|
+
void main() {
|
|
74
|
+
vUv = p * 0.5 + 0.5;
|
|
75
|
+
vL = vUv - vec2(texel.x, 0.0);
|
|
76
|
+
vR = vUv + vec2(texel.x, 0.0);
|
|
77
|
+
vT = vUv + vec2(0.0, texel.y);
|
|
78
|
+
vB = vUv - vec2(0.0, texel.y);
|
|
79
|
+
gl_Position = vec4(p, 0.0, 1.0);
|
|
80
|
+
}
|
|
81
|
+
`;
|
|
82
|
+
|
|
83
|
+
/** The fragment shaders, one per pass. */
|
|
84
|
+
const PASSES = {
|
|
85
|
+
splat: `
|
|
86
|
+
precision highp float;
|
|
87
|
+
varying vec2 vUv;
|
|
88
|
+
uniform sampler2D target;
|
|
89
|
+
uniform float aspect;
|
|
90
|
+
uniform vec3 colour;
|
|
91
|
+
uniform vec2 point;
|
|
92
|
+
uniform float radius;
|
|
93
|
+
void main() {
|
|
94
|
+
vec2 d = vUv - point;
|
|
95
|
+
d.x *= aspect;
|
|
96
|
+
vec3 base = texture2D(target, vUv).xyz;
|
|
97
|
+
gl_FragColor = vec4(base + exp(-dot(d, d) / radius) * colour, 1.0);
|
|
98
|
+
}`,
|
|
99
|
+
advect: `
|
|
100
|
+
precision highp float;
|
|
101
|
+
varying vec2 vUv;
|
|
102
|
+
uniform sampler2D velocity;
|
|
103
|
+
uniform sampler2D source;
|
|
104
|
+
uniform vec2 velocityTexel;
|
|
105
|
+
uniform float dt;
|
|
106
|
+
uniform float dissipation;
|
|
107
|
+
void main() {
|
|
108
|
+
vec2 from = vUv - dt * texture2D(velocity, vUv).xy * velocityTexel;
|
|
109
|
+
gl_FragColor = texture2D(source, from) / (1.0 + dissipation * dt);
|
|
110
|
+
}`,
|
|
111
|
+
curl: `
|
|
112
|
+
precision highp float;
|
|
113
|
+
varying vec2 vUv, vL, vR, vT, vB;
|
|
114
|
+
uniform sampler2D velocity;
|
|
115
|
+
void main() {
|
|
116
|
+
float l = texture2D(velocity, vL).y;
|
|
117
|
+
float r = texture2D(velocity, vR).y;
|
|
118
|
+
float t = texture2D(velocity, vT).x;
|
|
119
|
+
float b = texture2D(velocity, vB).x;
|
|
120
|
+
gl_FragColor = vec4(0.5 * (r - l - t + b), 0.0, 0.0, 1.0);
|
|
121
|
+
}`,
|
|
122
|
+
vorticity: `
|
|
123
|
+
precision highp float;
|
|
124
|
+
varying vec2 vUv, vL, vR, vT, vB;
|
|
125
|
+
uniform sampler2D velocity;
|
|
126
|
+
uniform sampler2D curl;
|
|
127
|
+
uniform float strength;
|
|
128
|
+
uniform float dt;
|
|
129
|
+
void main() {
|
|
130
|
+
float l = texture2D(curl, vL).x;
|
|
131
|
+
float r = texture2D(curl, vR).x;
|
|
132
|
+
float t = texture2D(curl, vT).x;
|
|
133
|
+
float b = texture2D(curl, vB).x;
|
|
134
|
+
float c = texture2D(curl, vUv).x;
|
|
135
|
+
vec2 force = 0.5 * vec2(abs(t) - abs(b), abs(r) - abs(l));
|
|
136
|
+
force /= length(force) + 1e-4;
|
|
137
|
+
force *= strength * c;
|
|
138
|
+
force.y *= -1.0;
|
|
139
|
+
vec2 v = texture2D(velocity, vUv).xy + force * dt;
|
|
140
|
+
gl_FragColor = vec4(clamp(v, -1000.0, 1000.0), 0.0, 1.0);
|
|
141
|
+
}`,
|
|
142
|
+
divergence: `
|
|
143
|
+
precision highp float;
|
|
144
|
+
varying vec2 vUv, vL, vR, vT, vB;
|
|
145
|
+
uniform sampler2D velocity;
|
|
146
|
+
void main() {
|
|
147
|
+
float l = texture2D(velocity, vL).x;
|
|
148
|
+
float r = texture2D(velocity, vR).x;
|
|
149
|
+
float t = texture2D(velocity, vT).y;
|
|
150
|
+
float b = texture2D(velocity, vB).y;
|
|
151
|
+
vec2 c = texture2D(velocity, vUv).xy;
|
|
152
|
+
// The walls reflect: what would flow out comes back.
|
|
153
|
+
if (vL.x < 0.0) l = -c.x;
|
|
154
|
+
if (vR.x > 1.0) r = -c.x;
|
|
155
|
+
if (vT.y > 1.0) t = -c.y;
|
|
156
|
+
if (vB.y < 0.0) b = -c.y;
|
|
157
|
+
gl_FragColor = vec4(0.5 * (r - l + t - b), 0.0, 0.0, 1.0);
|
|
158
|
+
}`,
|
|
159
|
+
diffuse: `
|
|
160
|
+
precision highp float;
|
|
161
|
+
varying vec2 vUv, vL, vR, vT, vB;
|
|
162
|
+
uniform sampler2D source;
|
|
163
|
+
uniform float amount;
|
|
164
|
+
void main() {
|
|
165
|
+
vec4 c = texture2D(source, vUv);
|
|
166
|
+
vec4 around = (texture2D(source, vL) + texture2D(source, vR) + texture2D(source, vT) + texture2D(source, vB)) * 0.25;
|
|
167
|
+
gl_FragColor = mix(c, around, amount);
|
|
168
|
+
}`,
|
|
169
|
+
scale: `
|
|
170
|
+
precision highp float;
|
|
171
|
+
varying vec2 vUv;
|
|
172
|
+
uniform sampler2D source;
|
|
173
|
+
uniform float value;
|
|
174
|
+
void main() { gl_FragColor = value * texture2D(source, vUv); }`,
|
|
175
|
+
pressure: `
|
|
176
|
+
precision highp float;
|
|
177
|
+
varying vec2 vUv, vL, vR, vT, vB;
|
|
178
|
+
uniform sampler2D pressure;
|
|
179
|
+
uniform sampler2D divergence;
|
|
180
|
+
void main() {
|
|
181
|
+
float l = texture2D(pressure, vL).x;
|
|
182
|
+
float r = texture2D(pressure, vR).x;
|
|
183
|
+
float t = texture2D(pressure, vT).x;
|
|
184
|
+
float b = texture2D(pressure, vB).x;
|
|
185
|
+
float div = texture2D(divergence, vUv).x;
|
|
186
|
+
gl_FragColor = vec4((l + r + b + t - div) * 0.25, 0.0, 0.0, 1.0);
|
|
187
|
+
}`,
|
|
188
|
+
gradient: `
|
|
189
|
+
precision highp float;
|
|
190
|
+
varying vec2 vUv, vL, vR, vT, vB;
|
|
191
|
+
uniform sampler2D pressure;
|
|
192
|
+
uniform sampler2D velocity;
|
|
193
|
+
void main() {
|
|
194
|
+
float l = texture2D(pressure, vL).x;
|
|
195
|
+
float r = texture2D(pressure, vR).x;
|
|
196
|
+
float t = texture2D(pressure, vT).x;
|
|
197
|
+
float b = texture2D(pressure, vB).x;
|
|
198
|
+
vec2 v = texture2D(velocity, vUv).xy - vec2(r - l, t - b);
|
|
199
|
+
gl_FragColor = vec4(v, 0.0, 1.0);
|
|
200
|
+
}`,
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Sets a fluid up on a WebGL 2 context.
|
|
205
|
+
*
|
|
206
|
+
* @param {WebGL2RenderingContext} gl The context, shared with whatever draws the fluid.
|
|
207
|
+
* @param {FluidOptions} [options] Its character.
|
|
208
|
+
* @returns {Fluid | null} The fluid, or null where half-float targets cannot be drawn to.
|
|
209
|
+
*/
|
|
210
|
+
export function createFluid(gl, { curl = 28, velocityFade = 0.25, inkFade = 0.55, diffusion = 0 } = {}) {
|
|
211
|
+
if (!gl.getExtension('EXT_color_buffer_float') && !gl.getExtension('EXT_color_buffer_half_float')) return null;
|
|
212
|
+
|
|
213
|
+
/** @type {Record<string, import('./gl.js').Program>} */
|
|
214
|
+
const passes = {};
|
|
215
|
+
for (const [name, fragment] of Object.entries(PASSES)) {
|
|
216
|
+
const pass = createProgram(gl, VERT, fragment);
|
|
217
|
+
if (!pass) return null;
|
|
218
|
+
passes[name] = pass;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Makes one half-float target, cleared.
|
|
223
|
+
*
|
|
224
|
+
* @param {number} size Cells a side.
|
|
225
|
+
* @returns {{ texture: WebGLTexture, fbo: WebGLFramebuffer, size: number } | null}
|
|
226
|
+
* The target, or null if it cannot be drawn to.
|
|
227
|
+
*/
|
|
228
|
+
const target = (size) => {
|
|
229
|
+
const texture = /** @type {WebGLTexture} */ (gl.createTexture());
|
|
230
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
231
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
232
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
233
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
234
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
235
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
236
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, size, size, 0, gl.RGBA, gl.HALF_FLOAT, null);
|
|
237
|
+
const fbo = /** @type {WebGLFramebuffer} */ (gl.createFramebuffer());
|
|
238
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
|
239
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
|
|
240
|
+
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) return null;
|
|
241
|
+
gl.viewport(0, 0, size, size);
|
|
242
|
+
gl.clearColor(0, 0, 0, 1);
|
|
243
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
244
|
+
return { texture, fbo, size };
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Makes a read/write pair of targets: read from one, write the other, swap.
|
|
249
|
+
*
|
|
250
|
+
* @param {number} size Cells a side.
|
|
251
|
+
* @returns {{ read: NonNullable<ReturnType<typeof target>>, write: NonNullable<ReturnType<typeof target>>, swap: () => void } | null}
|
|
252
|
+
* The pair, or null if a target cannot be drawn to.
|
|
253
|
+
*/
|
|
254
|
+
const pair = (size) => {
|
|
255
|
+
const a = target(size);
|
|
256
|
+
const b = target(size);
|
|
257
|
+
if (!a || !b) return null;
|
|
258
|
+
const p = {
|
|
259
|
+
read: a,
|
|
260
|
+
write: b,
|
|
261
|
+
swap() {
|
|
262
|
+
[p.read, p.write] = [p.write, p.read];
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
return p;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const velocity = pair(SIM);
|
|
269
|
+
const dye = pair(DYE);
|
|
270
|
+
const pressure = pair(SIM);
|
|
271
|
+
const divergence = target(SIM);
|
|
272
|
+
const curlField = target(SIM);
|
|
273
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
274
|
+
if (!velocity || !dye || !pressure || !divergence || !curlField) return null;
|
|
275
|
+
|
|
276
|
+
const triangle = bindTriangle(gl);
|
|
277
|
+
let aspect = 1;
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Runs one pass into a target.
|
|
281
|
+
*
|
|
282
|
+
* @param {string} name The pass.
|
|
283
|
+
* @param {{ fbo: WebGLFramebuffer, size: number }} into Where it draws.
|
|
284
|
+
* @param {(u: (n: string) => WebGLUniformLocation | null, tex: (n: string, t: { texture: WebGLTexture }) => void) => void} set
|
|
285
|
+
* Sets its uniforms; `tex` binds a target's texture to a sampler.
|
|
286
|
+
*/
|
|
287
|
+
const run = (name, into, set) => {
|
|
288
|
+
const { program, uniform } = passes[name];
|
|
289
|
+
gl.useProgram(program);
|
|
290
|
+
let unit = 0;
|
|
291
|
+
set(uniform, (n, t) => {
|
|
292
|
+
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
293
|
+
gl.bindTexture(gl.TEXTURE_2D, t.texture);
|
|
294
|
+
gl.uniform1i(uniform(n), unit++);
|
|
295
|
+
});
|
|
296
|
+
gl.uniform2f(uniform('texel'), 1 / into.size, 1 / into.size);
|
|
297
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, into.fbo);
|
|
298
|
+
gl.viewport(0, 0, into.size, into.size);
|
|
299
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Adds a gaussian of a value to one field.
|
|
304
|
+
*
|
|
305
|
+
* @param {NonNullable<ReturnType<typeof pair>>} field The field.
|
|
306
|
+
* @param {number} x Across, 0–1.
|
|
307
|
+
* @param {number} y Up, 0–1.
|
|
308
|
+
* @param {Rgb} value What is added at the centre.
|
|
309
|
+
* @param {number} radius Its size.
|
|
310
|
+
*/
|
|
311
|
+
const stamp = (field, x, y, value, radius) => {
|
|
312
|
+
run('splat', field.write, (u, tex) => {
|
|
313
|
+
tex('target', field.read);
|
|
314
|
+
gl.uniform1f(u('aspect'), aspect);
|
|
315
|
+
gl.uniform2f(u('point'), x, y);
|
|
316
|
+
gl.uniform3f(u('colour'), value[0], value[1], value[2]);
|
|
317
|
+
gl.uniform1f(u('radius'), radius);
|
|
318
|
+
});
|
|
319
|
+
field.swap();
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Advects one field along the velocity, fading it.
|
|
324
|
+
*
|
|
325
|
+
* @param {NonNullable<ReturnType<typeof pair>>} field The field.
|
|
326
|
+
* @param {number} dt Seconds.
|
|
327
|
+
* @param {number} fade Its fade, per second.
|
|
328
|
+
*/
|
|
329
|
+
const advect = (field, dt, fade) => {
|
|
330
|
+
run('advect', field.write, (u, tex) => {
|
|
331
|
+
tex('velocity', velocity.read);
|
|
332
|
+
tex('source', field.read);
|
|
333
|
+
gl.uniform2f(u('velocityTexel'), 1 / SIM, 1 / SIM);
|
|
334
|
+
gl.uniform1f(u('dt'), dt);
|
|
335
|
+
gl.uniform1f(u('dissipation'), fade);
|
|
336
|
+
});
|
|
337
|
+
field.swap();
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
return {
|
|
341
|
+
splat(x, y, dx, dy, colour, radius = 0.0025) {
|
|
342
|
+
bindTriangle(gl, triangle);
|
|
343
|
+
// Wide targets stretch the field across: keep the splat round.
|
|
344
|
+
const r = radius * Math.max(aspect, 1);
|
|
345
|
+
stamp(velocity, x, y, [dx, dy, 0], r);
|
|
346
|
+
if (colour[0] || colour[1] || colour[2]) stamp(dye, x, y, colour, r);
|
|
347
|
+
},
|
|
348
|
+
|
|
349
|
+
step(dt) {
|
|
350
|
+
bindTriangle(gl, triangle);
|
|
351
|
+
gl.disable(gl.BLEND);
|
|
352
|
+
run('curl', curlField, (_, tex) => tex('velocity', velocity.read));
|
|
353
|
+
run('vorticity', velocity.write, (u, tex) => {
|
|
354
|
+
tex('velocity', velocity.read);
|
|
355
|
+
tex('curl', curlField);
|
|
356
|
+
gl.uniform1f(u('strength'), curl);
|
|
357
|
+
gl.uniform1f(u('dt'), dt);
|
|
358
|
+
});
|
|
359
|
+
velocity.swap();
|
|
360
|
+
run('divergence', divergence, (_, tex) => tex('velocity', velocity.read));
|
|
361
|
+
run('scale', pressure.write, (u, tex) => {
|
|
362
|
+
tex('source', pressure.read);
|
|
363
|
+
gl.uniform1f(u('value'), PRESSURE);
|
|
364
|
+
});
|
|
365
|
+
pressure.swap();
|
|
366
|
+
for (let i = 0; i < PRESSURE_ITERATIONS; i++) {
|
|
367
|
+
run('pressure', pressure.write, (_, tex) => {
|
|
368
|
+
tex('pressure', pressure.read);
|
|
369
|
+
tex('divergence', divergence);
|
|
370
|
+
});
|
|
371
|
+
pressure.swap();
|
|
372
|
+
}
|
|
373
|
+
run('gradient', velocity.write, (_, tex) => {
|
|
374
|
+
tex('pressure', pressure.read);
|
|
375
|
+
tex('velocity', velocity.read);
|
|
376
|
+
});
|
|
377
|
+
velocity.swap();
|
|
378
|
+
advect(velocity, dt, velocityFade);
|
|
379
|
+
advect(dye, dt, inkFade);
|
|
380
|
+
// Diffusion: the dye bleeds into its neighbours, by the second.
|
|
381
|
+
if (diffusion > 0) {
|
|
382
|
+
run('diffuse', dye.write, (u, tex) => {
|
|
383
|
+
tex('source', dye.read);
|
|
384
|
+
gl.uniform1f(u('amount'), Math.min(1, diffusion * dt * 30));
|
|
385
|
+
});
|
|
386
|
+
dye.swap();
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
|
|
390
|
+
bind(dyeUnit, velocityUnit) {
|
|
391
|
+
gl.activeTexture(gl.TEXTURE0 + dyeUnit);
|
|
392
|
+
gl.bindTexture(gl.TEXTURE_2D, dye.read.texture);
|
|
393
|
+
gl.activeTexture(gl.TEXTURE0 + velocityUnit);
|
|
394
|
+
gl.bindTexture(gl.TEXTURE_2D, velocity.read.texture);
|
|
395
|
+
},
|
|
396
|
+
|
|
397
|
+
resize(a) {
|
|
398
|
+
aspect = a;
|
|
399
|
+
},
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* @typedef {object} StirOptions
|
|
405
|
+
* @property {Rgb[]} colors The ink's palette, looped through as it is laid.
|
|
406
|
+
* @property {number} [force=1] How hard the pointer pushes the fluid.
|
|
407
|
+
* @property {number} [trail=0.16] How much ink a stroke leaves, 0 for none.
|
|
408
|
+
* @property {boolean} [drops=true] Whether a click or tap drops a bloom of ink.
|
|
409
|
+
* @property {number} [radius=1] The size of a stroke's splats and of a drop, 1 as standard.
|
|
410
|
+
* @property {string} [ignore] Clicks on these are theirs, not the fluid's (default: links, buttons,
|
|
411
|
+
* form fields and `[data-effect-ignore]`).
|
|
412
|
+
*/
|
|
413
|
+
|
|
414
|
+
/** How hard a stroke pushes at force 1: velocity added per element-size of travel. */
|
|
415
|
+
const PUSH = 5000;
|
|
416
|
+
/** Ink laid per 1% of the element's size travelled, at trail 1. */
|
|
417
|
+
const INK_PER_STEP = 0.3;
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Connects a fluid to the pointer over an element: strokes push it and leave
|
|
421
|
+
* a trace of ink (with a hovering pointer), clicks and taps drop a bloom.
|
|
422
|
+
*
|
|
423
|
+
* @param {Fluid} fluid The fluid.
|
|
424
|
+
* @param {HTMLElement} el The element it covers.
|
|
425
|
+
* @param {StirOptions} options How it is stirred.
|
|
426
|
+
* @returns {{ update: (dt: number) => void, setColors: (colors: Rgb[]) => void, destroy: () => void }}
|
|
427
|
+
* `update(dt)` lays the strokes since the last frame (call it before
|
|
428
|
+
* `fluid.step`); `setColors` changes the ink's palette; `destroy` stops listening.
|
|
429
|
+
*/
|
|
430
|
+
export function stir(fluid, el, options) {
|
|
431
|
+
const {
|
|
432
|
+
force = 1,
|
|
433
|
+
trail: inkTrail = 0.16,
|
|
434
|
+
drops = true,
|
|
435
|
+
radius = 1,
|
|
436
|
+
ignore = CLICK_IGNORE,
|
|
437
|
+
} = options;
|
|
438
|
+
let colors = options.colors;
|
|
439
|
+
let along = Math.random();
|
|
440
|
+
const strokes = canHover() ? trail(el) : null;
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* The next colour of ink, a step further round the palette.
|
|
444
|
+
*
|
|
445
|
+
* @param {number} strength How much dye, 1 for a full drop.
|
|
446
|
+
* @returns {Rgb} The dye.
|
|
447
|
+
*/
|
|
448
|
+
const ink = (strength) => {
|
|
449
|
+
along = (along + 0.006) % 1;
|
|
450
|
+
const c = paletteAt(colors, along);
|
|
451
|
+
return [c[0] * strength, c[1] * strength, c[2] * strength];
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* A drop of ink: a bloom of colour, and a ring of pushes outward with a
|
|
456
|
+
* turn to them, so it bursts open and curls.
|
|
457
|
+
*
|
|
458
|
+
* @param {PointerEvent} e The press.
|
|
459
|
+
*/
|
|
460
|
+
const drop = (e) => {
|
|
461
|
+
const at = pressAt(el, e, ignore);
|
|
462
|
+
if (!at) return;
|
|
463
|
+
const { x, y } = at;
|
|
464
|
+
const box = el.getBoundingClientRect();
|
|
465
|
+
const aspect = box.width / box.height;
|
|
466
|
+
fluid.splat(x, y, 0, 0, ink(1.1), 0.01 * radius);
|
|
467
|
+
const spin = Math.random() < 0.5 ? 1 : -1;
|
|
468
|
+
for (let k = 0; k < 8; k++) {
|
|
469
|
+
const a = (k / 8) * Math.PI * 2;
|
|
470
|
+
const cx = Math.cos(a);
|
|
471
|
+
const cy = Math.sin(a);
|
|
472
|
+
const vx = (cx - spin * cy * 0.7) * 520 * force;
|
|
473
|
+
const vy = (cy + spin * cx * 0.7) * 520 * force;
|
|
474
|
+
fluid.splat(x + (cx * 0.03 * radius) / aspect, y + cy * 0.03 * radius, vx / aspect, vy, [0, 0, 0], 0.003 * radius);
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
if (drops) document.addEventListener('pointerdown', drop);
|
|
478
|
+
|
|
479
|
+
return {
|
|
480
|
+
update(dt) {
|
|
481
|
+
if (!strokes) return;
|
|
482
|
+
const time = dt || 1 / 60;
|
|
483
|
+
const points = strokes.read(time);
|
|
484
|
+
if (!points.length) return;
|
|
485
|
+
// Ink and push go by the distance travelled, shared between the
|
|
486
|
+
// stroke's splats, so a fast flick is a ribbon rather than a pile-up
|
|
487
|
+
// and the amount is the same whatever the frame rate.
|
|
488
|
+
const travel = Math.hypot(points[0].dx, points[0].dy) * time;
|
|
489
|
+
const each = travel / points.length;
|
|
490
|
+
for (const p of points) {
|
|
491
|
+
/** @type {Rgb} */
|
|
492
|
+
const dye = inkTrail > 0 ? ink(inkTrail * INK_PER_STEP * (each / 0.01)) : [0, 0, 0];
|
|
493
|
+
const push = (PUSH * force * time) / points.length;
|
|
494
|
+
fluid.splat(p.x, p.y, p.dx * push, p.dy * push, dye, 0.0025 * radius);
|
|
495
|
+
}
|
|
496
|
+
},
|
|
497
|
+
setColors(next) {
|
|
498
|
+
colors = next;
|
|
499
|
+
},
|
|
500
|
+
destroy() {
|
|
501
|
+
document.removeEventListener('pointerdown', drop);
|
|
502
|
+
},
|
|
503
|
+
};
|
|
504
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { PaletteName } from './palettes.js';
|
|
2
|
+
|
|
3
|
+
export interface FlutedGlassConfig {
|
|
4
|
+
/** The blooms' colours, the one that follows the pointer first. Default indigo, violet, pink. */
|
|
5
|
+
colors?: string[];
|
|
6
|
+
/** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
|
|
7
|
+
palette?: PaletteName | (string & {});
|
|
8
|
+
/** The ground behind the glass. Default `#f4f4f5`. */
|
|
9
|
+
background?: string;
|
|
10
|
+
/** The flutes' slant, degrees from vertical. Default -35. */
|
|
11
|
+
angle?: number;
|
|
12
|
+
/** Flutes across the longer side. Default 9. */
|
|
13
|
+
frequency?: number;
|
|
14
|
+
/** 0: flat flutes with sharp seams; 1: a gentle curve all across. Default 0.5. */
|
|
15
|
+
softness?: number;
|
|
16
|
+
/** How hard each flute bends what is behind it. Default 1.5. */
|
|
17
|
+
refraction?: number;
|
|
18
|
+
/** How far the colours part at the seams; 0 for none. Default 0.6. */
|
|
19
|
+
dispersion?: number;
|
|
20
|
+
/** The highlight along each flute; 0 for none. Default 0.35. */
|
|
21
|
+
highlight?: number;
|
|
22
|
+
/** How strong the blooms are. Default 1. */
|
|
23
|
+
bloom?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Starts fluted glass in `el`'s canvas. Returns a function that stops it. */
|
|
27
|
+
export function flutedGlass(el: HTMLElement, config: FlutedGlassConfig): () => void;
|