Online Portfolio2021-27Network:
Source ยท src/sketches/letters-in-dots.js
// Letters in dots โ€” a nod to 36 Days of Type: each glyph is sampled into a grid of
// circles and squares that morph from one letter to the next. Click to skip ahead.
const GLYPHS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

export default function lettersInDots(p, el) {
  let W, H, pg, i = 0, cells = [], target = [];
  const step = () => Math.max(8, Math.round(Math.min(W, H) / 34));

  function sample(ch) {
    pg.background(0); pg.fill(255); pg.noStroke();
    pg.textAlign(p.CENTER, p.CENTER); pg.textStyle(p.BOLD);
    const size = Math.min(W, H) * 0.92;
    pg.textSize(size); pg.text(ch, W / 2, H / 2 + size * 0.04);
    pg.loadPixels();
    const s = step(), pts = [];
    for (let y = s / 2; y < H; y += s)
      for (let x = s / 2; x < W; x += s) {
        const k = 4 * (Math.floor(y) * pg.width + Math.floor(x));
        pts.push({ x, y, on: pg.pixels[k] > 128 });
      }
    return pts;
  }

  function build() {
    target = sample(GLYPHS[i % GLYPHS.length]);
    if (cells.length !== target.length) cells = target.map(t => ({ x: t.x, y: t.y, v: 0 }));
  }
  const next = () => { i++; build(); };

  const makeBuffer = () => { pg = p.createGraphics(W, H); pg.pixelDensity(1); };

  p.setup = () => {
    W = el.clientWidth; H = el.clientHeight;
    p.createCanvas(W, H); makeBuffer(); build();
  };

  p.draw = () => {
    p.background(17);
    const s = step(), tt = p.millis() / 1000;
    p.noStroke();
    for (let k = 0; k < cells.length; k++) {
      const c = cells[k], on = target[k].on ? 1 : 0;
      c.v += (on - c.v) * 0.12;
      const n = p.noise(c.x * 0.012, c.y * 0.012, tt * 0.35);
      const r = s * (0.12 + 0.86 * c.v) * (0.72 + 0.28 * n);
      p.fill(255, 255, 0, 30 + 225 * c.v);
      if (n > 0.56) p.rect(c.x - r / 2, c.y - r / 2, r, r); else p.circle(c.x, c.y, r);
    }
    if (p.frameCount % 150 === 0) next();
  };

  p.mousePressed = () => {
    if (p.mouseX >= 0 && p.mouseX <= W && p.mouseY >= 0 && p.mouseY <= H) next();
  };

  p.windowResized = () => {
    W = el.clientWidth; H = el.clientHeight;
    p.resizeCanvas(W, H); makeBuffer(); cells = []; build();
  };
}