Skip to content

Latest commit

 

History

History
80 lines (62 loc) · 1.64 KB

File metadata and controls

80 lines (62 loc) · 1.64 KB
weight 1
title visit(callback)

Iterates all cells in row-major order and calls callback({ row, col, value }) for each. Reading and mutating the grid inside the callback is allowed.

Example

(click to randomly refill q with emojis)
{{< p5-global-iframe quadrille="true" width="425" height="425" >}} 'use strict'; Quadrille.cellLength = 50; let q; const emojis = ['🐸', '🐯', '🐱', '🐶', '🐮'];

function setup() { createCanvas(8 * Quadrille.cellLength, 8 * Quadrille.cellLength); q = createQuadrille(8, 8); update(); }

function draw() { background('#DAF7A6'); drawQuadrille(q); }

function mousePressed() { update(); }

function update() { q.visit(({ row, col }) => q.fill(row, col, random(emojis))); } {{< /p5-global-iframe >}}

{{% details title="code" open=true %}}

Quadrille.cellLength = 50;
let q;
const emojis = ['🐸', '🐯', '🐱', '🐶', '🐮'];

function setup() {
  createCanvas(8 * Quadrille.cellLength, 8 * Quadrille.cellLength);
  q = createQuadrille(8, 8);
  update();
}

function draw() {
  background('#DAF7A6');
  drawQuadrille(q);
}

function mousePressed() {
  update();
}

function update() {
  q.visit(({ row, col }) => q.fill(row, col, random(emojis)));
}

{{% /details %}}

{{< callout type="info" >}}

  • All cells are visited in row-major order.
  • The callback decides what to do with each cell (e.g., refill with a random emoji). {{< /callout >}}

Syntax

visit(callback)

Parameters

Param Description
callback Function called for each visited cell, receiving an object { row, col, value }. Its return value is ignored