Skip to content

XOR grid

Move your mouse over the grid.

Can you clean up the resulting mess?

A grid of squares, toggled when you move over them

function setup() {
  createCanvas(400, 400);
}

let squares = []
let lastI = 0

function draw() {
  background(220);
  
  for (let x = 0; x < 20; x ++) {
    for (let y = 0; y < 20; y ++) {
      noFill()
      rect(x * 20, y * 20, 20, 20)
      let i = y * 20 + x
      if (squares[i]) {
        fill(0)
        rect(x * 20 + 2, y * 20 + 2, 16, 16)
      }
      if (mouseX >= x * 20 && mouseY >= y * 20 && mouseX < x * 20 + 20 && mouseY < y * 20 + 20) {
        if (i != lastI) {
          squares[i] = !squares[i]
          lastI = i
        }
      }
    }
  }
}

(Originally seen at https://editor.p5js.org/bojidar-bg/sketches/EEE8v1aZw)

Experiments tagged p5 (19/85)

Experiments tagged interactive (6/26)

Experiments on this site (19/85)