---
title: "Soft Grain · Playground · Drew Minns"
description: "A treatment for photos: one colour, fine grain, and wind."
url: https://reallygood.work/playground/soft-grain/
---

Playground · 05

# Soft Grain

A treatment for photos: one colour, fine grain, and wind.

Live piece: A colonnade in periwinkle duotone with grain blowing off it.

Colour

PeriwinkleEmberMossInk

Grain

0.55

Wind

0.60

Use your own image

How it works

The image is mapped into the tones of one colour, from deep shadow to a warm highlight, then covered in fine grain that's heavier in the shadows.

Grain blows off one bright edge in streaks, widening as it travels, like sand off stone. Try it on one of your own photos; it stays in your browser.

```js
// only brightness matters, so any photo gets the same treatment
let L = luminance + (random() - .5) * grain * (.35 + .65 * (1 - luminance));  // heavier in shadows
if (inPlume && random() < plume * wind * streak * streak) L += .45;           // sand blowing off
```

Make it yourself

The whole thing in one file, with no libraries. Save it as an .html file and open it. The settings at the top follow the controls above, so tune it there, then copy.

soft-grain.html

```html
<!doctype html>
<meta charset="utf-8">
<title>Soft Grain</title>
<style>
  body { margin: 0; min-height: 100vh; display: grid; place-items: center; gap: 16px; background: #15130f; color: #f4f1ea; font: 14px system-ui, sans-serif; }
  canvas { width: min(92vw, 960px); aspect-ratio: 2 / 1; display: block; }
</style>
<canvas id="plate"></canvas>
<label>Choose a photo <input type="file" accept="image/*" id="file"></label>
<script>
const settings = {
  grain: 0.55,   // how much grain, heavier in the shadows
  wind: 0.6,     // how much grain blows off the bright edge
  tone: 'periwinkle',     // which colour the photo is mapped into
};
const TONES = {   // shadow to highlight
  periwinkle: ['#12164a', '#323da8', '#6b78e0', '#a9b2f4', '#f6dce8'],
  ember: ['#1c0905', '#6b1d0e', '#d4532a', '#f4a35e', '#fbe7cc'],
  moss: ['#0a1710', '#1d4630', '#4f8a5b', '#a9cf8f', '#eff3d4'],
  ink: ['#0e0e10', '#3a3a40', '#7d7d86', '#c4c4c9', '#f4f1ea'],
};
const hex = h => [1, 3, 5].map(i => parseInt(h.slice(i, i + 2), 16));
const stops = TONES[settings.tone].map(hex);
// a lookup table: brightness 0–255 → a colour along the ramp
const LUT = Array.from({ length: 256 }, (_, i) => { const f = i / 255 * (stops.length - 1), j = Math.min(stops.length - 2, Math.floor(f)), t = f - j; return stops[j].map((v, k) => v + (stops[j + 1][k] - v) * t); });

// smooth value noise, for the streaks in the wind
const hash = (x, y) => { let h = Math.imul(x, 374761393) ^ Math.imul(y, 668265263); h = Math.imul(h ^ (h >>> 13), 1274126177); return ((h ^ (h >>> 16)) >>> 0) / 4294967296; };
function noise(x, y) {
  const xi = Math.floor(x), yi = Math.floor(y), xf = x - xi, yf = y - yi, u = xf * xf * (3 - 2 * xf), v = yf * yf * (3 - 2 * yf);
  const a = hash(xi, yi), b = hash(xi + 1, yi), c = hash(xi, yi + 1), d = hash(xi + 1, yi + 1);
  return a + (b - a) * u + (c - a) * v + (a - b - c + d) * u * v;
}

const canvas = document.getElementById('plate'), ctx = canvas.getContext('2d');
const ANGLE = 0.62, WC = Math.cos(ANGLE), WS = Math.sin(ANGLE);   // the wind's direction
let lum = null, plume = null, w = 0, h = 0;

// only brightness matters, so any photo gets the same treatment
function prepare(img) {
  const r = canvas.getBoundingClientRect(); w = canvas.width = Math.round(r.width); h = canvas.height = Math.round(r.height);
  const s = Math.max(w / img.width, h / img.height);
  ctx.drawImage(img, (w - img.width * s) / 2, (h - img.height * s) / 2, img.width * s, img.height * s);
  const px = ctx.getImageData(0, 0, w, h).data;
  lum = new Float32Array(w * h); plume = new Float32Array(w * h);
  for (let i = 0; i < lum.length; i++) lum[i] = (0.299 * px[i * 4] + 0.587 * px[i * 4 + 1] + 0.114 * px[i * 4 + 2]) / 255;
  // the plume: a cone that starts at one bright edge and widens downwind
  const ex = w * 0.55, ey = h * 0.45;
  for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
    const dx = x - ex, dy = y - ey, along = dx * WC + dy * WS, across = -dx * WS + dy * WC;
    if (along < 0) continue;
    const spread = 12 + along * 0.42;
    plume[y * w + x] = Math.exp(-(across * across) / (spread * spread)) * Math.exp(-along / (w * 0.45)) * Math.min(1, along / 20);
  }
}

function draw(time) {
  if (lum) {
    const img = ctx.createImageData(w, h), d = img.data, drift = time * 55;
    for (let y = 0, i = 0; y < h; y++) for (let x = 0; x < w; x++, i += 4) {
      const k = y * w + x, m = plume[k];
      let L = lum[k] + (Math.random() - 0.5) * settings.grain * (0.35 + 0.65 * (1 - lum[k])) * 0.9;   // heavier in shadows
      if (m > 0.02 && settings.wind > 0) {   // sand blowing off
        const streak = noise((x * WC + y * WS - drift) * 0.035, (-x * WS + y * WC) * 0.55);
        L += m * settings.wind * 0.18;
        if (Math.random() < m * settings.wind * streak * streak * 1.4) L += 0.45;
      }
      const c = LUT[Math.max(0, Math.min(255, (L * 255) | 0))];
      d[i] = c[0]; d[i + 1] = c[1]; d[i + 2] = c[2]; d[i + 3] = 255;
    }
    ctx.putImageData(img, 0, 0);
  }
  setTimeout(() => requestAnimationFrame(t => draw(t / 1000)), 40);
}

// until a photo is chosen, a soft gradient stands in
const demo = document.createElement('canvas'); demo.width = 400; demo.height = 200;
const g = demo.getContext('2d'), grad = g.createLinearGradient(0, 0, 400, 200);
grad.addColorStop(0, '#222'); grad.addColorStop(0.55, '#eee'); grad.addColorStop(1, '#555');
g.fillStyle = grad; g.fillRect(0, 0, 400, 200);
prepare(demo);
document.getElementById('file').addEventListener('change', e => {
  const f = e.target.files[0]; if (!f) return;
  const img = new Image(); img.onload = () => prepare(img); img.src = URL.createObjectURL(f);   // it stays in your browser
});
draw(0);
</script>
```

-   [Previous piece — Dot Chart](https://reallygood.work/playground/dot-chart/)
-   [Next piece — Isometric Diagrams](https://reallygood.work/playground/isometric-diagrams/)
