Dither field

Slow clouds of tone drawn the old way, as patterns of small squares, like a picture on an early screen.

Made of squares.

Drifting clouds of tone, drawn as patterns of small squares.

Options

Screen
Colour

Code

import { useEffect, useRef } from "react";
import "./dither-field.css";

const FRAGMENT = `precision highp float;
uniform vec2 u_size;
uniform float u_time;
uniform vec3 u_colour;
#define PX (1.0 / u_size.y)

float hash(vec2 p) {
  return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
float noise(vec2 p) {
  vec2 i = floor(p);
  vec2 f = fract(p);
  f = f * f * (3.0 - 2.0 * f);
  return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
  float value = 0.0;
  float weight = 0.5;
  for (int i = 0; i < 5; i++) {
    value += weight * noise(p);
    p = p * 2.02 + vec2(17.1, 9.2);
    weight *= 0.5;
  }
  return value;
}
float grain() {
  return hash(gl_FragCoord.xy * 0.731 + floor(u_time * 12.0) * 1.37) - 0.5;
}

float bayer2(vec2 a) {
  a = mod(a, 2.0);
  return 2.0 * a.x + 3.0 * a.y - 4.0 * a.x * a.y;
}
float bayer4(vec2 a) {
  return (4.0 * bayer2(a) + bayer2(floor(a / 2.0)) + 0.5) / 16.0;
}

float scene(vec2 uv, float aspect, float t) {
  float size = max(3.0, floor(u_size.y / 140.0));
  vec2 grid = gl_FragCoord.xy / size;
  vec2 cell = floor(grid);
  vec2 centre = cell * size / u_size.y;
  float tone = noise(centre * 1.4 + vec2(t * 0.06, 0.0)) * 0.6 + noise(centre * 3.0 - vec2(0.0, t * 0.05)) * 0.4;
  tone = smoothstep(0.3, 0.74, tone);
  return step(bayer4(cell), tone) * 0.62;
}

void main() {
  vec2 uv = gl_FragCoord.xy / u_size;
  float ink = clamp(scene(uv, u_size.x / u_size.y, u_time), 0.0, 1.0);
  gl_FragColor = vec4(u_colour * ink, ink);
}`;

export function DitherField({ children }) {
  const ref = useRef(null);

  useEffect(() => {
    const canvas = ref.current;
    const gl = canvas.getContext("webgl", { antialias: false });
    if (!gl) return;
    const compile = (type, source) => {
      const part = gl.createShader(type);
      gl.shaderSource(part, source);
      gl.compileShader(part);
      return part;
    };
    const program = gl.createProgram();
    gl.attachShader(program, compile(gl.VERTEX_SHADER, "attribute vec2 corner; void main() { gl_Position = vec4(corner, 0.0, 1.0); }"));
    gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAGMENT));
    gl.linkProgram(program);
    gl.useProgram(program);
    gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
    const corner = gl.getAttribLocation(program, "corner");
    gl.enableVertexAttribArray(corner);
    gl.vertexAttribPointer(corner, 2, gl.FLOAT, false, 0, 0);
    const size = gl.getUniformLocation(program, "u_size");
    const time = gl.getUniformLocation(program, "u_time");
    const tone = gl.getUniformLocation(program, "u_colour");

    const still = matchMedia("(prefers-reduced-motion: reduce)").matches;
    let frame = 0;
    let seen = true;

    const draw = (now) => {
      frame = 0;
      const css = getComputedStyle(canvas).color;
      const parts = css.match(/[\d.]+/g).slice(0, 3).map(Number);
      gl.uniform3fv(tone, css.startsWith("color(") ? parts : parts.map((part) => part / 255));
      gl.uniform2f(size, canvas.width, canvas.height);
      gl.uniform1f(time, still ? 8 : now / 1000);
      gl.drawArrays(gl.TRIANGLES, 0, 3);
      if (seen && !still) frame = requestAnimationFrame(draw);
    };
    const start = () => {
      if (!frame) frame = requestAnimationFrame(draw);
    };

    const sized = new ResizeObserver(() => {
      canvas.width = canvas.clientWidth * Math.min(window.devicePixelRatio || 1, 2);
      canvas.height = canvas.clientHeight * Math.min(window.devicePixelRatio || 1, 2);
      gl.viewport(0, 0, canvas.width, canvas.height);
      start();
    });
    const watched = new IntersectionObserver(([entry]) => {
      seen = entry.isIntersecting;
      if (seen) start();
    });
    sized.observe(canvas);
    watched.observe(canvas);

    return () => {
      cancelAnimationFrame(frame);
      sized.disconnect();
      watched.disconnect();
    };
  }, []);

  return (
    <section className="dh-dither">
      <canvas ref={ref} className="dh-dither-art" aria-hidden="true" />
      <div className="dh-dither-content">{children}</div>
    </section>
  );
}

Made of squares.

Drifting clouds of tone, drawn as patterns of small squares.