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)
Note
One function object fills all 64 cells, and every cell draws something different — because
rowandcolarrive throughoptions, not through a closure.ringsandshadeare keys the sketch invented;drawQuadrilleneither 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, soside / 2is the cell's center, not the canvas's.
Warning
It is one object, reused for every cell.
drawQuadrillewritesoptions.rowandoptions.colimmediately 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 === 7Copy what you need instead —
const { row, col } = options;— or spread it:const here = { ...options };
Warning
originis defaulted once, not per draw.drawQuadrillefills it in with??=, so it is only written when absent. Omitoptionsand each call gets a fresh object with the renderer's default (CORNERinP2D,CENTERinWEBGL). Hand in your own object and reuse it across aP2Dcanvas and aWEBGLbuffer, and it keeps whichever origin it was first drawn under. Pass a fresh object per draw, or setoptions.originexplicitly, 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
drawQuadrillecreateQuadrillecellLength- p5.js
createCanvasbackgroundtextheightmapCENTERcirclenoFillstrokestrokeWeightmouseYtextSizemouseXtextAlignwidthfloor
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.