Wraps a native surface. This is the one injection point: a test, or a host
without node:fs, substitutes a NativeCanvas.
toBuffer('apng'). Every page as a frame, with PNG's colour and alpha.
toBuffer('avif'). Smaller again, and slower to encode.
toBuffer('bmp'). Uncompressed, and rarely what is wanted.
What this render's input said that could not be used: a markup tag, or a
filter or backdropFilter chain the renderer refuses, which is drawn as
though it were not set, as Chrome drops an invalid declaration.
Separate from Canvas.warnings, and the distinction is the point. A warning says the world did not answer — a fetch that failed. A diagnostic says the input did not say what it meant, which is the caller's to fix and not the network's.
The case these exist for is one where the render is correct:
<color=#ff00> is conformant, four-digit #RGBA, yellow at alpha zero,
and a caller who truncated #ff0000 sees blank text either way. Nothing
in the picture can tell them apart.
Always an array, as warnings is.
Which rasteriser drew the pages: 'gpu' or 'cpu'.
The outcome rather than the request, and they disagree: a build with no GPU
backend compiled, a driver that declines, and a float colorType all
rasterise on the CPU whatever gpu says. Without this, a caller who asks
for the GPU and gets the CPU has no way to find out.
toBuffer('gif'). Every page as a frame, at 256 colours.
Whether the GPU was asked for.
Asking is not getting — compare Canvas.engine. This is what
Root was told, and it is true when nothing was said, because that is
the renderer's own default.
toBuffer('ico'). The Windows icon container.
toBuffer('jpg'). Lossy and opaque — no alpha channel.
How many pages were painted.
What a page means is the format's answer: a frame for GIF and APNG, a sheet for PDF and TIFF, one size of the same icon for ICO.
toBuffer('pdf'). Vector, and every page a page.
toBuffer('png'). Lossless, and the format to reach for without a reason not to.
toBuffer('raw'). The pixels, unencoded.
Whether the surface is still there.
The device-pixel multiplier the pages were drawn at.
Layout always solves at one, so this is resolution rather than anything about where things sit.
toBuffer('svg'). Vector, so text stays text.
toBuffer('tiff'). Lossless, and what a print pipeline usually asks for.
Every image source that could not be resolved, in node order.
Always an array, so canvas.warnings.length === 0 is a check you
write once and never guard — empty is the ordinary answer, not a missing
one. One entry per distinct source rather than per node, because sixty
nodes drawing one dead URL is one thing that went wrong.
Populated whatever onImageError on Root chose, including
'ignore'. That option decides what is drawn, never what is known: a
caller who finds the placeholder distracting keeps the diagnostic.
A render that finishes with warnings is not one that succeeded quietly. Each entry is a picture that was asked for and did not arrive.
toBuffer('webp'). Smaller than PNG at the same quality, and takes every page as an animation.
Frees the Skia surface now rather than at the next collection.
Optional. The surface is freed when this canvas is collected either way; this only makes it sooner, which a server rendering thousands of images wants and a script does not need. Calling it twice is not an error, and every other method throws afterwards rather than reading freed memory.
Encodes the canvas on a worker thread and resolves with the bytes.
This is where the pixels are allocated, because painting recorded a drawing rather than a bitmap. So this is what costs time in proportion to the canvas area — about 11 ms at 800×800, 65 ms at 2000×2000 and 256 ms at 4000×4000 on one machine — and this is what throws when the area is more than the host can allocate, however long ago the size was chosen.
That time is not spent on the event loop. What crosses to the worker is the recorded pages, not the scene — the drawing is already shaped, so the worker consults no font and cannot substitute one. The loop keeps only the half of an export that needs the canvas, which does not grow with area.
See Canvas.toBufferSync for why the type is Buffer.
Optionalformat: FormatOptionaloptions: EncodeOptionsEncodes the canvas and returns the bytes.
The same bytes Canvas.toBuffer resolves with, produced on the calling thread instead of a worker: this one blocks the event loop for the whole encode, which is what a script wants and what a server does not.
A Buffer, because that is the value the addon returns; Buffer extends
Uint8Array, so a caller wanting either is satisfied.
Optionalformat: FormatOptionaloptions: EncodeOptionsThe HTMLCanvasElement spelling of Canvas.toURLSync.
Synchronous and taking a quality rather than an options object, because the DOM method it is named after is both.
Optionalformat: FormatOptionalquality: numberEncodes the canvas on a worker and writes it to path.
The bytes never come back through JavaScript. The file is written where it is encoded, and a spanning format streams into it page by page, so a long animation is bounded by disk rather than by memory.
Optionaloptions: EncodeOptionsEncodes the canvas and writes it to path, blocking.
The same call on the calling thread. It asks the format question once, on this side, and everything after that is the one decision made in one place — see NativeCanvas.write.
Optionaloptions: EncodeOptionsEncodes the canvas on a worker and resolves with a data: URL.
The base64 runs on the calling thread, which is where it has to: it is a string, and a string cannot cross to a worker without being copied twice. It is also the cheap end — a 4000×4000 PNG is tens of kilobytes by the time it is bytes, against the hundred milliseconds that made it.
Optionalformat: FormatOptionaloptions: EncodeOptionsEncodes the canvas and returns a data: URL.
Optionalformat: FormatOptionaloptions: EncodeOptions
A painted canvas, and the ways to read it back.
Constructed by
Root; a caller never builds one. It holds the native surface and nothing else, so every method here is an encode of work already done.