Show HN: canvas_ity - A tiny, single-header canvas-like 2D rasterizer for C++ (github.com)

8 points by a_e_k ↗ HN
Hi HN! After a few years of off-and-on work on it, I'm pleased to release my canvas_ity library as open source.

TLDR: This is a single-header C++ library with no dependencies beyond the standard C++ library. In about 2300 lines of 78-column code (not counting blanks or comments), or 1300 semicolons, it implements an API based on the basic W3C <canvas> specification to draw 2D vector graphics into an image buffer:

- Strokes and fills (with antialiasing and gamma-correct blending)

- Linear and radial gradients

- Patterns (with repeat modes and bi-cubic resampling)

- Line caps and line joins (handling high curvature)

- Dash patterns and dash offsets

- Transforms

- Lines, quadratic and cubic Beziers, arcs, and rectangles

- Text (very basic, but does its own TTF font file parsing)

- Raster images (i.e., sprites)

- Clipping (via masking)

- Compositing modes (Porter-Duff)

- Drop shadows with Gaussian blurs

Why? Some years ago I needed to programmatically draw on an image file. I looked at Skia and was put off by its size and build complexity. Cairo was much better, but by then I was spoiled by the STB single-header libraries. I like tiny code and writing a single-header but powerful vector graphics library seemed like a fun challenge and a good way to pay things forward. I ended up with something that can compile down to less than 36 KiB of object code on x86-64 and uncovered a lot of interesting browser <canvas> quirks along the way with my testing suite. Enjoy!

2 comments

[ 1.3 ms ] story [ 10.9 ms ] thread
Wonderful idea, great achievement. The illustration A/Bing it against a browser is inspiring. And Truetype font support, damn son! Can't wait to delve into it more. One of those things that should have been done years ago. Congratulations and thank you!
Thanks for the kind words. To be fair, I did start it years ago, I just spent way too long off-and-on polishing it. :-)