Crosshatch shader preview: Pen-and-ink crosshatching of any image, or the layers beneath.
ittybitty

Crosshatch

Pen-and-ink crosshatching of any image, or the layers beneath.

Code

The usage always matches the preview, layers and performance settings included. No build step or package.

Use as
Code view

Copy embed gives one block to paste into any page or site builder, no setup. For your own code: add crosshatch.js, ittybitty-shader.js, runtime.js, texture.js next to this page. Highlighted values follow the controls.

<!-- Quickest: one element. Size it with CSS; it fills its box. -->
<script type="module" src="./ittybitty-shader.js"></script>
<ittybitty-shader effect="crosshatch" style="width: 100%; aspect-ratio: 16 / 10;"></ittybitty-shader>

<!-- Or with JavaScript, for full control: -->
<canvas id="crosshatch" aria-hidden="true" style="width: 100%; aspect-ratio: 16 / 10; display: block"></canvas>

<script type="module">
  import { crosshatch } from './crosshatch.js';

  const canvas = document.getElementById('crosshatch');
  const effect = crosshatch(canvas, {
    showPaper: true,
    showOutlines: true,
    image: '/examples/still-life.webp',
    spacing: 5,
    sets: 4,
    angle: 35,
    weight: 0.42,
    wobble: 0.5,
    shake: 0.3,
    follow: 0.35,
    variance: 0.5,
    contrast: 1.2,
    colour: 0,
    ink: '#1a1612',
    paper: '#f7f1e3',
    // Performance
    dpr: 2,
    adaptive: true,
  });

  // Later: effect.set(…), effect.pause(), effect.resume(), effect.destroy()
</script>

Details

Tone built the way an illustrator builds it: sets of parallel strokes at new angles, switched on one after another as the tone darkens, so darker tones keep every lighter stroke. Strokes are broken into tapered dashes with a slight wobble, and ink outlines follow the edges. Static: one draw per change.

Payload
7.2 KB brotli for crosshatch.js and ittybitty-shader.js and runtime.js and texture.js, measured at build time. See the method.
Resolution
Device pixel ratio capped at 2.
Frame rate
None. One frame per change.
When unseen
Stops rendering when the canvas leaves the viewport or the tab is hidden, and resumes where it left off.
Reduced motion
Nothing to change: it never moves.
Layers
Up to five effects fuse into one shader and one draw call. Filters such as dithering and halftone process the layers beneath them.
Browsers
Any browser with WebGL2: Chrome and Edge 56+, Firefox 51+, Safari 15+.
Licence
MIT. Copy it, change it, ship it.