Pure Image and the Appropriate Use of AI

I’m happy to present a new version of PureImage, my 100% JS Canvas implementation, now with dramatically better spec compatibility. I’ve been working on this library for over a decade now, but this is the first release that involved AI usage. I have very mixed feelings about AI, so today I’m going to tell you how I used it and why.

HTML Canvas Changes

First, the implementation changes. The HTML Canvas spec was stable for a long time, but in recent years the W3C updated the spec to flesh out some of the inconsistencies, tighten up the spec (so many color syntaxes), as well as add some small quality of life features like expanded text metrics.

I knew PureImage needed an update, but I was hestiant for two reasons. First, I’m a busy person and it’s gotten harder to justify spending time on an open source project that is heavily used (~100k downloads in the past week!), but provides me no benefits or accolades.

Second, I knew the path stroking code was hacked together flaming garbage, but doing a full rewrite felt too daunting.  Both of those changed recently.

Agentic Era

First, I was laid off last week, so I have a lot more time than I used to. I need to find a new job ASAP of course (gotta pay the mortgage), but once I catch up on paperwork I’ll have time for pleasure coding.  Second, AI coding agents have gotten really good in the last six months. They are far more reliable and require far less hand holding and supervision.  So last week I made my mind up to do a big overhaul.

In my experience at work, AI agents do a really good job when they given a task in the category of “easily verifiable correctness”.  It’s hard to say when a user interface is “correct” or “finished”, but it is very easy to say when a compiler is correct: when the code compiles.  Being a Canvas implementation puts PureImage squarely in that second group.  I’ve been able to manage the project as a solo developer for a decade because I have comprehensive unit tests. AIs are good at making unit tests and making sure they pass, so this felt like a good match.

New Tests

Still, for all of these new features I felt like we needed better unit tests than what I’d written by hand. Surely, I thought, other people must have made test suites for canvas.  Enter the second thing AI agents are good at: searching for information. I put Claude on the task and in less five minutes it had found the Web Platform Testsproject, including over a thousand canvas tests, and hooked it up to my test runner. Now we could see exactly what is broken or missing in PureImage.  WPT tests gave us a target with “verifiable correctess”.

From there I went down the list with Claude, churning through the low hanging fruit. It was only adding hooks so that existing test would pass or it added very small missing parts of the API. few lines here, few lines there, very few changes to the core library. Almost all of it is testing infrastructure.  We did the work in chunks with me reviewing each PR, but I didn’t have to look to closely. The unit tests ensured the right thing was done.

I did review closely the changes to the core library to make sure they fit my preferred style and weren’t overly complex. Fortunately Claude was good about adhering to my repo conventions and using the linter and prettier settings.

This cautious small fixes approach got me pretty far. Most of the code I had written was pretty good. It just needed a little more testing and extend some of the error conditions and boundary conditions like handle if someone types in an infinite number to draw rect call. (It takes a lot of work to make sure an API is robust). I finished this work with Claude in just a few days working just an hour or two a day of human time.  Now it was time to tackle what I’d been dreading. Stroking.

Path Stroking

Now I need to tell you a little bit more about how canvas actually works. Most of the Canvas API is about managing how new pixels are blended against the current pixels that are already there. Fundamentally, all that is pretty simple arithmetic.  The hard part is drawing paths.

A path is a series of connected curves — either cubic curves or lower-level curves like arcs and lines that can be made with cubics — which form the actual complex shape to be filled. Filling shapes is actually a pretty well-understood problem. There's a couple different approaches, and the one that I've been using this whole time was called scanline rendering with anti-aliasing.

If filling shapes is easy you would think that drawing the outlines would be easy too. After all, we already have code to turn the path into a series of straight line segments, so we can just draw those with Bresenham’s, right? Right?! And in fact, that's what I've been doing until enough people asking me to support stroking.

Path stroking means drawing the outlines of a shape with a virtual pen thicker than a single pixel. I’d been doing that with a modified Bresenham, but it starts to break down once we want to stroke paths curves. Unfortunately, we cannot easily calculate new cubics that are offset perpendicularly to the original curve. So I hacked it instead.

I turned the paths into straight line segments and then extruded those out perpendicularly to create a new shape. This mostly worked for the common cases but started to break down in places where a line crossed over itself, or when we need to control the way joints and end caps are rendered. I knew the code extending my buggy code to cover the edge cases wouldn’t work. I needed something new, and honestly, I didn't have the time or patience to implement all of that again from scratch.

Vibing a new Stroking Algorithm

I could have used a third-party library that just does stroking but since I want this library to have as few dependencies as possible, I decided to see what Claude could come up with. After all, there are well- known algorithms for stroking that have been published for decades. I knew Claude could do it, but this meant, for the first time, I was adding a feature to PureImage where I had’nt personally written the core algorithm. This really worried me. I needed to make sure I understood it.

I’m cautious with my use of AI. I still want to understand the code. This is my project so I need to feel like I still own it. I’m not just a rubber stamper working with a robot. I thought a lot about this, and here’s what I came up with. To keep it clean I had Claude do the stroking in a single file in a single PR. That way I could study it in detail. A single PR that I could read through and really understand how it works. The bulk of the work is in the new stroke.ts file.

The new algorithm is slightly slower, about 9%, but since the old one was fundamentally broken in many ways, this is a worthwhile trade off.

Le Fin?

I think my approach has been a very nuanced way to use AI, and it certainly had a dramatic improvement to the quality of the library. We went from ~22% to now passing 79% of the Canvas API in a week. Most of the improvement was missing context state and rasterizing pixels at the centers, or missing features in the test enviornment rather than the core lib, with strokes being the only truly reimplemented feature.

I think this was a successful experiment and shows a way to use AI usefully and productively. Most importantly, there is no AI pretending to be a human.  The PRs always show Claude as the author and I still wrote all of the user facing docs by hand. (spelling errors and all). I feel it is important to have the human in the loop.