shift(n, wrap)

Shifts the whole grid left or right. n > 0 shifts left by n; n < 0 shifts right by |n|. With wrap on (default) it wraps around; with wrap off it slides and leaves empty cells. Returns this quadrille (chainable) or undefined (when n = 0).

Example #

(click to shift; press any key to reset)

code
let q;
let vertical, wrap, flip;

Quadrille.outlineWeight = 1;
Quadrille.cellLength = 30;

function setup() {
  createCanvas(450, 300);
  reset();
  // Shift controls:
  // Vertical: shift along columns (via transpose → shift → transpose)
  vertical = createCheckbox('Vertical', true);
  vertical.position(10, 10);
  vertical.style('color', '#ddd');
  // Wrap: circular rotation when ON; logical slide (empty-fill) when OFF
  wrap = createCheckbox('Wrap', true);
  wrap.position(10, 32);
  wrap.style('color', '#ddd');
  // Flip: reverses direction (left/up vs right/down)
  flip = createCheckbox('Flip', true);
  flip.position(10, 54);
  flip.style('color', '#ddd');
}

function draw() {
  background(20);
  drawQuadrille(q);
}

function mousePressed() {
  // Resolve UI to shift parameters:
  const isVertical = vertical.checked(); // shift along cols (true), rows (false)
  const shouldWrap = wrap.checked();     // wrap-around (true) vs slide (false)
  const step = flip.checked() ? -1 : 1;  // invert direction
  // Apply one-cell shift:
  // Horizontal: shift directly
  // Vertical: transpose to treat columns as rows, shift, then transpose back
  isVertical
    ? q.transpose().shift(step, shouldWrap).transpose()
    : q.shift(step, shouldWrap);
}

function keyPressed() {
  reset();
}

function reset() {
  // Random board so shift behavior is easy to see on click
  q = createQuadrille(16, 10)
    .rand(5, '🐲')
    .rand(5, '🦑')
    .rand(5, '🦜')
    .rand(5, '🐦')
    .rand(5, '🐞')
    .rand(5, '🍄');
}

Syntax #

shift([n = 1], [wrap = true])

Parameters #

ParamDescription
nNumber (default 1). How many cells to shift. n > 0 shifts left; n < 0 shifts right
wrapBoolean (default true). When on, the grid wraps around; when off, it slides and leaves empty spaces