Pencil

Pencil

A declarative language for mathematical figures. A figure is a text file that says what’s in it and what each thing means, and style sheets say how it looks. The same file renders as an interactive figure on the web and as video.

It’s early, and the syntax will change. Pencil is being built to make math explainers, one real figure at a time, and nothing about the language is stable yet.

Two hearts in bipolar coordinates around the foci at plus and minus 1, over the grid of coordinate circles, with the two circles through a moving point on each.
Two hearts in bipolar coordinates, over the two families of circles the coordinates are made of. The sheet makes each line a light that glows by its distance, and they add where they cross, which is why the crossings and the foci burn white. The document has no colors in it.

Styling a figure

A document names things and says what they are. This one says there’s a curve c, the points at distance r from the origin, and that r breathes with time. It holds no colors and no sizes.

A sheet targets the name c. Because the curve is implicit, it has a signed distance, so a sheet can style its inside and outside. The second sheet bands both sides by that distance and moves the hue with time, and not a line of the document changes.

circle.penon GitHub
// A breathing circle: the smallest figure that exercises the whole path.doc circle// One breath, so it loops without a jump.duration 2·πviewport { x: -2..2, y: -2..2 }let r = 1 + 0.25·sin(time)curve c = implicit (x, y): x² + y² = r²
A circle that breathes, its radius swinging a quarter either side of 1.
circle.styleon GitHub
style c::inside  { fill: oklch(0.72, 0.13, 50) }style c::outside { fill: oklch(0.42, 0.09, 250) }style c          { stroke: white; width: 3px }
A circle that breathes, its radius swinging a quarter either side of 1. Its sides are banded by their distance from it.
bands.styleon GitHub
// A restyle of the same document: distance bands on both sides, and a hue that// moves with time. Style values are expressions, so this needs no new mechanism.style c::inside  { fill: mix(hue(0.08 + 0.05·sin(time)), white, 0.4 + 0.3·cos(24·self.dist)) }style c::outside { fill: mix(oklch(0.45, 0.10, 250), white, 0.4 + 0.3·cos(24·self.dist)) }style c          { stroke: white; width: 2.5px }

What’s in it

  • Mathematical expressions.Points are complex numbers, derivatives are exact, and a solve can find unknowns each frame. A figure compiles to an IR with its expressions in MathJSON, so it isn’t locked into this tool.
  • Animation and interaction.Both bind the params of one figure, so what a reader drags and what a video plays are the same description.
  • Collections and maps.One declaration can draw thousands of dots, circles or curves, and can go through a function, so one thing can appear in several panels.
  • Strokes and sides.A circle has a handedness and can straighten into a line and turn over, and both of its sides can be drawn at once. A taper can follow arc length.
  • Style sheets.A figure holds no colors, sizes or renderer-specific calls. Styles target its names, with CSS-like rules and math-specific properties.
  • Rendering and checking.A Rust core compiles figures to WGSL for WebGPU and WebGL2, and a separate CPU interpreter defines what each pixel should be. pencil check compares them.

Where to go

  • Getting started

    Build it, render a frame, compare the GPU with the CPU, and export video.

  • Language reference

    Statements, curve forms, expressions, built-ins and styles.

  • Demos

    Every figure in the corpus, drawn live, with its source.

  • Playground

    Edit any figure and its sheets in the browser, and watch it recompile.

  • Source

    The Rust core, the renderer, the command and the browser host, under MIT.

Where it stands

Pencil is built from a corpus of real figures, and a feature goes in only when a figure needs it. The syntax and the IR change freely while that goes on, so expect files written today to need edits later.

Figures here are drawn on your GPU, through WebGPU where the browser has it and WebGL2 otherwise. They’ve been checked mostly in Chrome on a Mac; other browsers and phones much less. If a figure doesn’t draw, the page says why under it.