options

The options parameter is the channel between the draw call and the code stored inside cells. A display function passed as a draw param receives the whole params bag; a function stored in a cell — or an object cell's display method — receives options and nothing else. Whatever keys you put in it ride along untouched, and drawQuadrille adds three of its own: origin, plus the row and col of the cell being drawn.

That is the whole mechanism. A cell-stored function has no other way to learn where it is.

Example

(move the mouse: horizontally sets the ring count, vertically the shade — both are custom options keys)

edit, then run

Note

One function object fills all 64 cells, and every cell draws something different — because row and col arrive through options, not through a closure. rings and shade are keys the sketch invented; drawQuadrille neither reads nor validates them, it just forwards the object. Note the cell-local coordinate system: the origin is the cell's own upper-left corner, so side / 2 is the cell's center, not the canvas's.

Warning

It is one object, reused for every cell. drawQuadrille writes options.row and options.col immediately before each display call, so the object you hand in is mutated 64 times per frame in the example above. Reading it during the call is correct; keeping a reference is not — after the draw, it reports the last cell visited:

let stash;
function bullseye(options) {
  stash = options;        // ⚠ same object every time
}
// after drawQuadrille on an 8x8 board: stash.row === 7, stash.col === 7

Copy what you need instead — const { row, col } = options; — or spread it: const here = { ...options };

Warning

origin is defaulted once, not per draw. drawQuadrille fills it in with ??=, so it is only written when absent. Omit options and each call gets a fresh object with the renderer's default (CORNER in P2D, CENTER in WEBGL). Hand in your own object and reuse it across a P2D canvas and a WEBGL buffer, and it keeps whichever origin it was first drawn under. Pass a fresh object per draw, or set options.origin explicitly, when you draw the same options to both.

Object cells

When a cell holds an object with a display field and no objectDisplay is supplied, the object is rendered through its own display — and if that is a function, it too is called with options as its only argument, with this bound to the object:

const piece = {
  emoji: '🐉',
  display(options) {
    // `this` is the piece; `options` says where it is
    textAlign(CENTER, CENTER);
    textSize(Quadrille.cellLength * 0.7);
    text(this.emoji, Quadrille.cellLength / 2, Quadrille.cellLength / 2);
  }
};

The split is worth naming: this carries what the cell is, options carries where it is. Neither knows about the other, which is what lets one object be stored in many cells at once.

Beyond drawQuadrille

The same options object threads through every render entry point — toImage, sort and sample all accept and forward it — so a cell function written for the canvas works unchanged when the board is saved to a file or sampled for sorting.

Syntax

drawQuadrille(quadrille, { options })

Parameters

Param Description
options Object: forwarded, unmodified except for the three keys below, as the sole argument to functions stored in cells and to object cells' display methods. Default is a fresh {} per call

Keys drawQuadrille writes into it:

Key Description
row Number: row index of the cell being drawn. Overwritten before every display call
col Number: column index of the cell being drawn. Overwritten before every display call
origin Constant: CORNER or CENTER. Written only if absent (??=), so a supplied value is never overridden
What this page's examples use
p5.quadrille
drawQuadrille createQuadrille cellLength
p5.js
createCanvas background text height map CENTER circle noFill stroke strokeWeight mouseY textSize mouseX textAlign width floor

Names the analysis can prove are called, and the line each one is documented on. Local variables and members of an unknown receiver are left out.