filter

Filters which cells to draw from a given quadrille q. It accepts the following types:

  • A value collection (Array or Set) Draws only the cells whose values are included in the collection. Example: drawQuadrille(q, { filter: [tomato, skyblue] })

  • A predicate function over the cell ((cell) => boolean) The function receives { row, col, value } and must return true to draw that cell. Example: drawQuadrille(q, { filter: ({ value }) => brightness(value) < 50 })

  • A single value (anything that is neither a function nor a collection) Draws only the cells whose value matches it by identity (===): drawQuadrille(q, { filter: tomato })

Note

Arrow functions offer a compact syntax. For instance, the predicate

function (cell) {
  return brightness(cell.value) < 50;
}

becomes:

cell => brightness(cell.value) < 50;

You can also destructure to focus only on what you need:

({ value }) => brightness(value) < 50;

Example: Filtering by Value Collection

(click on canvas and press any key to randomize q)

edit, then run

Note

The values array must contain references to the exact instances used to fill the quadrille. For example, if q was filled with the variable yellow = color('lemonchiffon'), then values must include that same variable, not a new color('lemonchiffon').

Example: Filtering by Predicate Value

(click on canvas and press any key to randomize q)

edit, then run

Note

The method q.visit(callback) iterates over all cells in row-major order, invoking callback({ row, col, value }) for each one. Use destructuring (e.g., { value }) when you only need part of the cell.

Note

In JavaScript, for...in is used to iterate over object keys, typically for plain objects like filters:

for (const label in filters) {
  select.option(label);
}

Example: Filtering by Predicate Value, Row, and Column

(click on canvas and press any key to randomize q)

edit, then run

Syntax

drawQuadrille(quadrille, { filter })

Parameters

Param Description
filter Specifies which cells to draw. All cells are drawn if omitted or undefined. It can be:
  • a value collection (Array or Set) checked by identity (===) against cell.value
  • a cell-predicate function ({ row, col, value }) => boolean
  • a single value (neither function nor collection), matched by identity (===) against cell.value
What this page's examples use
p5.quadrille
drawQuadrille createQuadrille cellLength fill visit randomize outlineWeight outline
p5.js
createCanvas background color value random createCheckbox createSelect checked option blue red brightness saturation
JavaScript
Array.push

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.