Skip to contentCREATIVE·CODE·ART
10
6
3
1
p5.js

Kaleido

A kaleidoscope you draw with. Drag a finger across it and your line blooms into a turning mandala. Leave it alone and it draws by itself, slowly.

Licence: CC BY-NC-SA 4.0: remix with credit, non-commercial, share alike

View the code
// Kaleido — a p5.js kaleidoscope you draw with your finger
// Drag on the canvas and your line is mirrored into a turning mandala.
// Idle, a slow auto-pen keeps it alive. Palette: charcoal, bone, signal orange.
// Site sliders: Mirrors · Fade · Width · Drift — read through params.*

const BONE = [233, 229, 218];
const ORANGE = [255, 180, 84];
const BG = [13, 13, 18];

let autoA = 0;

function setup() {
  createCanvas(windowWidth, windowHeight);
  background(...BG);
}

function drawWedge(x0, y0, x1, y1, col, wgt) {
  const mirrors = round(params.mirrors ?? 10);
  const seg = TWO_PI / mirrors;
  const cx = width / 2, cy = height / 2;
  const dx0 = x0 - cx, dy0 = y0 - cy;
  const dx1 = x1 - cx, dy1 = y1 - cy;
  stroke(col[0], col[1], col[2], 170);
  strokeWeight(wgt);
  for (let i = 0; i < mirrors; i++) {
    const a = i * seg;
    const ca = cos(a), sa = sin(a);
    line(cx + dx0 * ca - dy0 * sa, cy + dx0 * sa + dy0 * ca,
         cx + dx1 * ca - dy1 * sa, cy + dx1 * sa + dy1 * ca);
    line(cx + dx0 * ca + dy0 * sa, cy + dx0 * sa - dy0 * ca,
         cx + dx1 * ca + dy1 * sa, cy + dx1 * sa - dy1 * ca);
  }
}

function draw() {
  noStroke();
  fill(BG[0], BG[1], BG[2], params.fade ?? 6);
  rect(0, 0, width, height);

  const wgt = params.width ?? 3;
  const m = 0.5 + 0.5 * sin(frameCount * 0.01);
  const col = [
    lerp(BONE[0], ORANGE[0], m),
    lerp(BONE[1], ORANGE[1], m),
    lerp(BONE[2], ORANGE[2], m)
  ];

  if (mouseIsPressed && mouseX > 0) {
    drawWedge(pmouseX, pmouseY, mouseX, mouseY, col, wgt);
  } else {
    autoA += 0.011 * (params.drift ?? 1);
    const R = min(width, height) * 0.30;
    const x = width / 2 + cos(autoA * 2.1) * R * (0.55 + 0.45 * sin(autoA * 0.7));
    const y = height / 2 + sin(autoA * 3.3) * R * (0.55 + 0.45 * cos(autoA * 0.9));
    const px = width / 2 + cos((autoA - 0.011 * (params.drift ?? 1)) * 2.1) * R * (0.55 + 0.45 * sin((autoA - 0.01) * 0.7));
    const py = height / 2 + sin((autoA - 0.011 * (params.drift ?? 1)) * 3.3) * R * (0.55 + 0.45 * cos((autoA - 0.01) * 0.9));
    drawWedge(px, py, x, y, col, wgt * 0.8);
  }
}

function keyPressed() {
  if (key === 's' || key === 'S') saveCanvas('kaleido', 'png');
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
  background(...BG);
}