![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
canvas-to-buffer
Advanced tools
Convert a Canvas graphic to a Buffer, as fast as possible, without a copy
A tiny converter to turn any graphic canvas into a buffer. With focus on speed: it does not make an expensive copy and makes clever use of atob()
and Uint8Array
.
Following performance tests prove that this module is using the fatest known method: http://jsperf.com/data-uri-to-buffer-performance/3
Useful scenarios for this module:
toBuffer
part and/or you care about speedFurthermore this module is an important part of https://github.com/binarykitchen/videomail-client whose implementation can be seen on https://www.videomail.io
var Frame = require('canvas-to-buffer') // I call it a Frame but you can go with i.E. CanvasConverter, whatever
var frame = new Frame(canvas) // Drop in any canvas, i.E. from a webcam
var buffer = frame.toBuffer()
var Frame = require('canvas-to-buffer')
var frame = new Frame(canvas, {
quality: 0.4,
image: {
types: ['webp', 'png']
}
})
...
The example means, it tries to encode the canvas first as webp
at the given quality before converting that data into a buffer. If that fails, i.E. the browser does not support it, then it will try again with the png
format.
Default: 0.5
Determines the quality when encoding the canvas into an image with the given type.
Default: ['webp', 'jpeg']
You know, turning a canvas into binary form requires an image type. No worries this module is able to automatically detect the supported image type in your browser.
But if you want to explicitely specify the image type for whatever reason, this is the option to use.
It can be a string or an array with max two image types.
FYI webp
images have better compression, but are supported on Google Chrome only. Hence this module automatically falls back to 'jpeg' for any other browsers. Beware that binary data for JPEGs is about 20% larger.
MIT. Copyright (C) Michael Heuberger
FAQs
Converts a Canvas graphic into a Buffer, as fast as possible and without a copy.
We found that canvas-to-buffer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.