gitmyhub

node-canvas

JavaScript ★ 11k updated 7h ago

Node canvas is a Cairo backed Canvas implementation for NodeJS.

A Node.js library that brings the browser's canvas drawing API to the server, letting you generate images, PDFs, and SVGs from code using the same commands you'd write in a browser.

JavaScriptNode.jsCairoC++setup: moderatecomplexity 3/5

Node-canvas brings the browser's drawing API to Node.js, letting you create and manipulate images on a server without a browser involved. In a browser, you can draw shapes, text, and images onto a canvas element and then export the result as a PNG or JPEG. Node-canvas replicates that same programming interface so that scripts running on a server can do all the same things: draw rectangles, render text with custom fonts, load and composite photos, and save the finished image to a file or send it as a response.

Under the hood, it relies on Cairo, a well-established graphics library used in Linux desktop environments and many other places. This is why the installation involves a few system-level dependencies beyond the usual npm install. On a Mac or Linux machine you install libraries like libpango and libcairo through your system package manager first, then install the npm package. Pre-built binaries are available for common platforms, so most users do not need to compile anything themselves.

The API closely follows the standard Web Canvas API that browsers expose. If you have written canvas drawing code for a website, most of it will work in node-canvas with little or no change. The project documents the gaps where behavior differs from the browser standard. There are also a handful of extras not found in browsers, such as the ability to export directly to a PDF or SVG file, stream output progressively, or load fonts from local files using registerFont.

Common use cases include generating dynamic images on a server, creating thumbnail previews, rendering charts or diagrams as PNG files, and producing PDFs programmatically. Because Cairo handles text through the Pango library, node-canvas supports full Unicode text, right-to-left scripts, and complex font features, which plain image-manipulation libraries sometimes lack.

The package is maintained by Automattic, the company behind WordPress.com. It requires Node.js 18.12.0 or later.

Where it fits