-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce blob's #1845
Comments
This would also help to quickly upload/download things using (node-)fetch and do other things with it |
I think that it would be awesome to "transition" node-canvas to be an implementation of Anything that makes us more compatible I'm 👍 |
yea, sounds good! |
ctx.drawImage would have to accept a ImageBitmap class instead/also, But it maybe already do? saw some stuff in the readme that i missed and that u have util like createBitmap |
fyi, fetch-blob is a ESM only package, so to load it from commonjs you would have to use the async but that would be fine either way. createImageBitmap don't need to import something to know that it's a blob and can be read. |
Yeah, we could export an
With the next major canvas version I think we should go ESM only as well |
Maybe this is a good boilerplate to start of from: import { Bitmap } from './bindings.js'
const unlock = Symbol('Illegal constructor')
class ImageBitmap {
#width = 0
#height = 0
#close
constructor(key, width, height, #close) {
if (key !== unlock) {
throw new Error('Illegal constructor')
}
}
get width () {
return this.#width
}
get height () {
return this.#height
}
close() {
this.#close && this.#close()
this.#close = null
}
}
const registry = new FinalizationRegistry(bitmap => {
bitmap.close()
});
/**
* @param {Blob} image
*/
async function createImageBitmap(image, sx, sy, sw, sh, options = {}) {
const ab = await blob.arrayBuffer()
const data = new Bitmap(new Uint8Array(ab))
const imageBitmap = new ImageBitmap(unlock, data.width, data.height, data.close.bind(data))
registry.register(imageBitmap, data)
return imageBitmap
} |
is |
b/c node-canvas is more like a |
This issue may be fixed by #2127, which @jimmywarting has done (it is even approved). Can we merge it into the main branch this year? |
NodeJS recently introduced Blobs into core
think this is grate cuz now we can have
canvas.toBlob(cb)
we could also introduce a new fn that can create a ImageBitmap out of a more web/standarlized way with createImageBitmap(blob) instead of using the abnormal way with
loadImage(...)
ornew Image(...)
(that i think should be deprecated/removed)I also think
createCanvas
should go away for a web-worker related thing which is the OffscreenCanvas cunstructorso that would also mean: we would use OffscreenCanvas.convertToBlob instead of
canvas.toBlob
This was what i did to feel more at home:
(still a tiny bit node specific - would like it to work 100% like a Web Worker would handle everything for cross browser coding)
fetch-blob is a good candidate for implementing support for it already
An example with using fetch-blob with createImageBitmap() would look like
This would be the correct way to get a buffer:
Close #1845, #1705, #1735, #1758, #1802 in favor of the new way to load images with
createImageBitmap(blob)
?deprecate
new Image()
andloadImage()
?use FinalizationRegistry and ImageBitmap.close to release the memory in native binding
The text was updated successfully, but these errors were encountered: