p5loom

API

Everything is exported from index.js, plus the modules below for finer use. There are no classes to construct.

mount(element, spec, { p5 })

Validates the spec, builds the plan, and draws it into a new figure appended to element. Returns a handle.

ArgumentMeaning
elementA DOM element to append the picture to.
specA spec object. Refused with a list of problems if invalid.
{ p5 }The p5 constructor. Required; there is no global lookup.

The handle has three properties:

It throws a TypeError with a message that lists every problem when the spec is invalid. Call it from the browser: it needs a document.

validateSpec(spec)

Returns an array of problem strings. Empty means valid. It never throws, even for junk input, so it is safe to call on untrusted data.

validateSpec({ version: 1, seed: -1, canvas: { width: 8, height: 8 }, elements: [] });
// [ "seed: must be an integer from 0 to 4294967295",
//   "canvas.width: integer from 16 to 4096",
//   "canvas.height: integer from 16 to 4096" ]

assertValidSpec(spec)

Returns the spec if valid, otherwise throws a TypeError. The error has a problems array.

buildPlan(spec)

Pure. Validates the spec and returns { width, height, themeName, theme, ops }, where ops is the ordered list of drawing operations in pixels. The same spec always gives the same plan, so you can test a picture without a browser.

buildPlan(spec).ops[0];
// { op: "background", colour: "#fbfaf7" }

describePlan(plan, spec)

Returns the sentence used as the text alternative. It counts elements by type and adds the number of generated dots.

createRng(seed)

The seeded generator the library uses. Returns next() (a number from 0 up to but not including 1), range(min, max) and int(min, max). Exposed so your own code can draw the same random numbers as a spec.

Themes

Policy

Import paths

import { mount, validateSpec, buildPlan, describePlan } from "p5loom";
import { createRng } from "p5loom/rng";
import { THEMES, contrast } from "p5loom/theme";

Subpath imports are spec, plan, theme, rng and policy. Until the package is on npm, import from the src/ files directly.