Skip to content

The grid pilot

Vex came from a layout engine. This pilot writes one layout strategy, the grid, as one Vex program.

The grid layout puts the items in rows, from left to right:

  • The cell is the size of the largest item. An item without a size counts as 0, and the cell is at least 1 by 1.
  • The configuration can set the number of columns. Without it, the number of columns is the number of cells that fit in the width of the bounds. The limit is the square root of the number of items, rounded up.
  • Each item starts at the top-left corner of its cell. An item without a size gets the size of the cell.

The Vex program evaluates at each item. Two values come from all the items: the cell and the number of items. The axis each reads them. The slot of the item comes from an extension, like ROW() in a spreadsheet.

The math of the width and the math of the height are the same. Thus the program calculates them once, with a vector of Vec2Domain:

  • The op vec2 makes a vector from two numbers, and the ops x and y give its components.
  • reduce("max") gives the component-wise maximum of the vectors. The op max of a list takes only numbers.
  • An op of Vec2 lifts a number to a vector with that number twice. Thus max(1) makes the cell at least 1 by 1.
const cell = root.start(0).each((t) => t.from("width").ifError(0)._.vec2(t.from("height").ifError(0)))
.reduce("max")._.max(1);
const pitch = cell._.add(gap);
const count = root.start(0).each((t) => t).count();
const cols = root.start(bounds.width + gap)._.divide(pitch._.x())._.floor()._.max(1)
._.min(count._.sqrt()._.ceil())._.max(1);
const i = root.start(root.ext<number>("slot"));
const corner = i._.mod(cols)._.vec2(i._.divide(cols)._.floor())._.multiply(pitch);
root.start(root.rec({
left: corner._.x(),
top: corner._.y(),
width: root.from("width").ifError(cell._.x()),
height: root.from("height").ifError(cell._.y()),
}));

The corner of an item is its column and its row, as one vector, times the pitch. Only the number of columns uses one dimension: it comes from the width of the bounds. Vectors tells more about this style.

One test has four items of 100 by 100, in bounds of 400 by 400, with a gap of 10:

import { gridVex } from "@vex/pilots";
const items = [{ width: 100, height: 100 }, { width: 100, height: 100 }, { width: 100, height: 100 }, { width: 100, height: 100 }];
gridVex(items, { width: 400, height: 400 }, { gap: 10 }).map((r) => [r.left, r.top]); // => [[0, 0], [110, 0], [0, 110], [110, 110]]

Change the sliders. The chip compares the rectangles of the Vex program with the rectangles of a plain TypeScript implementation of the same rules.

A uniform grid layout, as one formulaequal to the reference
0123456

7 rectangles from a program of 211 nodes. A dashed item has no size, so it gets the size of the cell.

  • It fits. The grid layout is two reductions and one formula for each item. The axes and the domains of numbers and vectors express it without new parts. The only addition is the extension slot.
  • It is equal. A property test gives random items, bounds and configurations to both implementations, and compares the rectangles.
  • It is slower. The program evaluates the two reductions again at each item, so its cost grows with the square of the number of items. A sheet with one column for each reduction, or the compile step of the plan, removes this cost.
  • Not each layout fits. A packed layout places each item after the items before it. That is an ordered fold with a state, not a formula for each item. Vex does not try to express it.