Replace

replace() #

Either replaces non empty cells with value, or value1 filled cells with value2.

Examples #

(click on any cell; press ‘r’ to replace filled cells or other key to reset)

code
Quadrille.cellLength = 40;
let quadrille;
let red, green, blue, cyan;

function setup() {
  createCanvas(400, 400);
  red = color('red');
  green = color('green');
  blue = color('blue');
  cyan = color('cyan');
  reset();
}

function draw() {
  background('orange');
  drawQuadrille(quadrille);
}

function mouseClicked() {
  const value1 = quadrille.read(quadrille.mouseRow, quadrille.mouseCol);
  quadrille.replace(value1, cyan);
}

function keyPressed() {
  if (key === 'r') {
    quadrille.replace(cyan);
  }
  else {
    reset();
  }
}

function reset() {
  quadrille = createQuadrille(10, 10, 25, red).rand(25, green).rand(25, blue);
}
Observation
To replace value1 with value2, all cells containing value1 must share the same memory reference, see search().

Syntax #

replace(value)

replace(value1, value2)

Parameters #

parameterdescription
valuep5.Image | p5.Graphics | p5.Color | array | object | string | number
value1p5.Image | p5.Graphics | p5.Color | array | object | string | number
value2p5.Image | p5.Graphics | p5.Color | array | object | string | number