Pencil

Getting started

Pencil is built from source for now: there are no releases, and its package isn’t published. Everything below runs from a checkout of the repository.

To try it with nothing installed, open the playground, which runs in the browser.

The syntax and the compiled form change freely while the project is young, so a figure written today may need edits later.

What you need

  • Rust, for everything.
  • The wasm32-unknown-unknown target, only to draw figures in a browser.
  • ffmpeg, only for video.
  • Python 3, only for scripts/serve.py.
git clone https://github.com/krisselden/pencil-lang.git
cd pencil-lang

Render a frame

A figure is a .pen document, drawn with one or more .style sheets in order. --t picks a moment in seconds.

cargo run --release -p pencil-cli -- render corpus/012-limacon/limacon.pen \
  --style corpus/012-limacon/plain.style --t 1 --out out/limacon.png

The same document takes a different sheet without changing. The breathing circle, banded by distance:

cargo run --release -p pencil-cli -- render corpus/001-circle/circle.pen \
  --style corpus/001-circle/bands.style --t 1.25 --out out/frame.png

Compare the GPU with the CPU

Every figure has two renderers: shaders on the GPU, and a simple CPU interpreter that defines what each pixel should be. check draws both and fails if they differ by more than a few levels in any channel.

cargo run --release -p pencil-cli -- check corpus/012-limacon/limacon.pen \
  --style corpus/012-limacon/plain.style

Export a video

Video is the same figure with time running. --size is the width in pixels.

cargo run --release -p pencil-cli -- video corpus/003-unroll/unroll.pen \
  --style corpus/003-unroll/blog.style --size 1280 --out out/unroll.mp4

--param NAME=VALUE sets one of the figure’s params for the whole video. A param that follows time can’t be set this way, because time gives it a value in every frame, and the command says so. render and svg take one for a single frame, in place of--t: time and --param can’t both set the same param.

cargo run --release -p pencil-cli -- video corpus/009-squaring/squaring.pen \
  --style corpus/theme.style --style corpus/009-squaring/plain.style --param n=5 --out out/squaring.mp4

Figures in a browser

The core compiles to WebAssembly, and <pencil-figure>, in the package atpackages/figure, draws a figure on a page with WebGPU, or WebGL2 where the browser has no WebGPU. The first build installs a matching wasm-bindgen and takes a few minutes.

rustup target add wasm32-unknown-unknown
scripts/build-web.sh
scripts/serve.py

Then open http://localhost:8741/web/playground.html. web/ has a page for most figures, and adding ?gl to a page’s address uses WebGL2.

On a page of your own, the element needs only the figure and its sheets:

<script type="module" src="packages/figure/src/element.js"></script>
<pencil-figure src="corpus/012-limacon/limacon.pen"
  sheets="corpus/012-limacon/plain.style"></pencil-figure>

It starts the GPU only when the figure comes near the viewport, and lets it go when the figure leaves the page. Its attributes, methods and events are described at the top ofpackages/figure/src/element.js.

A site with a bundler imports the package, @pencil-lang/figure. It isn’t published yet, so it is installed from a checkout. In Astro its <Figure> compiles each figure at build and ships it with a poster, which shows until the figure's first frame and is what a reader without JavaScript or a GPU sees. ItsREADME has both.

Bring formulas from LaTeX

Formulas copied from Desmos or MathLive, one per line, come back as Pencil text:

cargo run --release -p pencil-cli -- latex formulas.tex

The playground does the same when LaTeX is pasted into it.

Run the tests

cargo test --workspace

Each figure in corpus/ has its expected compiled form beside it. After a change that is meant to alter it, write the new ones:

UPDATE_GOLDENS=1 cargo test -p pencil-core --test corpus

The pencil command

pencil compile DOC.pen [--style FILE]... --out FIGURE.pfig
pencil build  DOC.pen [--style FILE]... [--t SECONDS] [--param NAME=VALUE]... [--size WIDTH] [--card] --out DIR
pencil ir     DOC.pen
pencil wgsl   DOC.pen [--style FILE]...
pencil render DOC.pen [--style FILE]... [--t SECONDS] [--param NAME=VALUE]... [--size WIDTH] [--cpu] --out FRAME.png
pencil svg    DOC.pen [--style FILE]... [--t SECONDS] [--param NAME=VALUE]... [--size WIDTH] --out FIGURE.svg
pencil check  DOC.pen [--style FILE]... [--t SECONDS]... [--param NAME=VALUE]... [--size WIDTH] [--out DIR]
pencil video  DOC.pen [--style FILE]... [--param NAME=VALUE]... [--size WIDTH] [--fps N] [--seconds S] [--font FILE] --out MOVIE.mp4
pencil latex  FILE.tex...
pencil help

cargo run --release -p pencil-cli -- stands in for pencil in a checkout.ir prints a figure’s compiled form, and wgsl the shader it becomes.compile writes a figure and its sheets as one file a page loads without parsing, andbuild writes that with its poster, which is what the package runs for a site.

svg writes one frame as vector graphics, for a paper or a drawing program: strokes, flat fills, dots and arrowheads as shapes, and labels as text. A style that is worked out per pixel, such as a fill that reads self.dist, has no vector form, so that part is left out and a line names the mark. The language referencelists what exports.

Where next

  • The language reference, for the statements, built-ins and styles.
  • The demos, for every figure in the corpus with its source.
  • The playground, to edit one and watch it recompile.