Shaders / Examples / Background

Background

The grain behind a hero or a banner: whole pixels, alive, never a blur.

Drawn with
WebGL
Chain
field → pixelate → ordered → grid → palette
Code
@forgeron/shaders: builtInPresets()["role-background"]
field source ·
/01 → What it is
  • Fractal noise through the 8×8 Bayer threshold, in the background, ink and line tones; one fragment shader for the four fields.
  • Five looks: the quiet field, the globe, the ember field (the one place it turns orange), the dissolve bands and the CPU dither field of the sub-sites.
  • Animates only on screen; one still frame under reduced motion; blank where WebGL is missing.
/02 → Where Forgeron uses it
SiteWhereSettings
forgeron.shHome hero, behind the village (.saga-bg)data-shader="field" data-px="4" data-accent="off" · opacity 6 %, screen
forgeron.shHome, the closing call to action (.cta-glow)data-shader="ember" data-px="8"
forgeron.shPricing, the network block (.net-art)data-shader="globe" data-px="4"
forgeron.shPricing, the closing banddata-shader="dissolve" data-px="8"
ui, icon and font galleriesThe home heroescanvas[data-dither] data-px="4" (5 on the font specimen)
/03 → Versions
  • field source ·
    /01 Liveevery setting of its field source
  • /02 Hero, as shownfield · accent off · opacity 0.06, screen
  • /03 Globepixel 4
  • /04 Emberpixel 8
  • /05 Dissolvepixel 8
  • /06 Dither fieldheat on the CPU · pixel 4
  • /07 Dither field, greystones={["--bg", "--line-strong", "--ink-dim"]}
/04 → Use it
/01 → React
import { DitherField, FieldShader } from "@forgeron/ui";

// The canvas fills its box: size the box.
<div style={{ position: "relative", height: 320 }}>
  <FieldShader mode="field" pixel={4} accent={false} />
</div>

<DitherField pixel={4} />
/02 → forgeron.sh (Astro, @forgeron/blueprint)
<canvas class="saga-bg" data-shader="field" data-px="4" data-accent="off" aria-hidden="true"></canvas>

<style>
  .saga-bg { opacity: 0.06; mix-blend-mode: screen; }
</style>
/03 → Preset "role-background" (builtInPresets())
{
  "$schema": "https://shaders.forgeron.sh/schema/preset.v1.json",
  "version": 1,
  "name": "role-background",
  "source": {
    "type": "field",
    "mode": "field"
  },
  "passes": [
    {
      "type": "pixelate",
      "size": 4
    },
    {
      "type": "ordered",
      "matrix": 8,
      "centered": false,
      "origin": "bottom",
      "steps": [
        [
          0,
          1
        ]
      ]
    },
    {
      "type": "grid",
      "gap": 0.16,
      "min": 4,
      "tone": 0
    },
    {
      "type": "palette",
      "tones": [
        {
          "var": "--bg",
          "fallback": "#0a0a0a"
        },
        {
          "var": "--ink",
          "fallback": "#ededed"
        },
        {
          "var": "--accent",
          "fallback": "#ff4f1f"
        }
      ],
      "transparent": []
    }
  ]
}
/04 → Plain DOM
import { builtInPresets } from "@forgeron/shaders";
import { mountPreset } from "@forgeron/shaders/dom";

// WebGL2: every step has GLSL. Validated first.
const handle = mountPreset(canvas, builtInPresets()["role-background"]);
// later: handle?.destroy();
/05 → Static, at build time
import { builtInPresets, chainOf, runCpu, toRgba } from "@forgeron/shaders";

// The CPU side of every step: RGBA bytes for a PNG, or tone paths for an SVG (toSvgPath).
const rgba = toRgba(runCpu(chainOf(builtInPresets()["role-background"]), 320, 200, { time: 1.7 }));
API → Options
OptionTypeDefaultDescription
mode"ember" | "globe" | "dissolve" | "field"—Which field (<FieldShader>).
pixelnumber4Cell side in CSS pixels. From 4, cells keep a one-pixel gap.
accentbooleantruefalse draws the brightest cells in ink instead of the ember.
tones[string, string, string]["--bg", "--line-strong", "--accent"]<DitherField>: background, low and high tones, tokens or colours.
pointerbooleantrue<FieldShader>: follows the pointer over the canvas's parent.
stillboolean—One frame at t = 0. Without it, the reader's prefers-reduced-motion decides.