Rendering SVG/using Canvas with Cloudflare Workers

I want to a Workers to render a Canvas (which can change depending on input) and send it as a response. However, I’m having a lot of trouble finding a solution that can render to images when run as a Cloudflare Worker.

I wanted to use the Rust resvg library, however it depends on libcairo (most node.js libraries do as well). Is there any way I can integrate libcairo (or any native library for that matter) in a Cloudflare Worker? I know this is possible with AWS Lambda, but I’m very interested in trying out Cloudflare’s platform instead.

1 Like

I’m facing a similar issue… since no “document” is available and neither the HTML5Canvas nor OffscreeCanvas APIs are implemented, it’s impossible to work with a true canvas context. Most JS implementations of Canvas APIs rely on libcairo as noted by the OP. I tried to use a pure JS implementation (canvas-webworker) to get around needing any runtimes or browser APIs, but then CF flags the script as trying to perform dangerous operations.

Kind of at a loss for a good solution for dynamic image rendering in a Cloudflare Worker. This behavior is working just fine in an AWS Lambda environment.

I think there’s also no GPU in workers, so rendering have to be done in CPU. So far, I’ve found wasm modules to be efficient enough for image processing. Simpler things like QR codes also seem to work in vanilla js.

Check the source for a lot of goodies, even SVG to PNG conversion.

The QR image example is even mentioned in Cloudflare’s own documentation, however image rendering beyond that doesn’t seem possible currently. It’s a shame, I hope Cloudflare can expand upon Workers to allow this use case.

Yep, workers are definitely not for every type of use-case.

The QR thing is quite cool though, I’m using it to create e-mail verification signatures.