A Paint Program
This project is a paint program that you can use on this page. The engine under it never sees a mouse. The engine takes points and gives pixels, so tests that draw with no window check every brush, every layer and every undo. The browser build is about 150 KB of WebAssembly. Before the site ships the build, the build must draw a test picture with exactly the pixels that the native tests pinned.
The engine is plain Zig, built for wasm32-freestanding. Thus CI builds the file that this page runs, and nothing is committed. The engine is a program for instruction: one brush engine, two layers, and 8-bit colour that blends in the same way as a browser canvas.
- If you make one mark for each pointer event, the stroke depends on hand speed. Walk the path, and carry the distance that is left into the next segment. Then the spacing stays even.
- A stroke at 50% opacity must stay at 50% where it crosses itself. Build the coverage of the stroke in a separate buffer. Apply the opacity once, at the end.
- Undo does not need a copy of the picture. Keep the rectangle that an action changed. Then undo and redo become the same swap.
- When no memory is available, the program should lose an undo step, not the stroke. In Zig, that failure is an ordinary error, and you decide what to do with it.
14 chapters.
- A Paint Program You Can TestA paint program that you can use on this page, and the one rule that lets tests check each brush.
- StrokesPointer samples come at irregular distances. Move along the path, put a dab at a fixed distance, and keep the distance that remains for the next segment.
- Smoothing, Pressure and ScatterMake a shaky hand steady with a moving average. Get the size of each dab from pen pressure. Scatter dabs with a random generator that repeats.
- The TipA dab is a disc with a soft edge. Make it an ellipse, turn it, and the same twenty lines draw a soft brush, a calligraphy nib and a ribbon.
- Bristles and PaperA row of thin hairs across the stroke gives separate tracks. A fixed grid of paper heights decides where a dry brush touches.
- Oil: Pickup and Paint LoadAn oil brush carries paint. It collects some of the colour below it, and it becomes dry as it moves.
- Flow and OpacityDabs overlap. Put the coverage of a stroke in its own buffer and apply its opacity at the end. Then a stroke at 50% stays at 50% where it crosses itself.
- Layers and the EraserLayers are transparent pixel arrays on white paper. Store them premultiplied, and the eraser becomes one multiplication.
- UndoKeep only the changed rectangle. Undo and redo are one swap, two stacks are an array and an index, and a memory failure costs an undo step, not the stroke.
- FillA flood fill finds its area a small number of pixels at a time. A queue and a visited map do this in fixed memory. Recursion uses all of the stack.
- Save a PNGThe program flattens the picture onto white paper and writes it as a PNG. Each row gets the filter, of five, that makes its bytes smallest.
- Projects and PresetsA project file keeps the layers that a PNG merges. A preset keeps a brush. Other people can write both, thus the program examines both first.
- WatercolourWater and pigment on a grid of paper. They spread, the paper dries, and pigment settles into it. Fixed steps make each run the same on all computers.
- A Native WindowThe same engine in a desktop window with raylib. Linux builds for Windows with one flag. A licence, not the compiler, stops a build for macOS.