Open jimmywarting opened 3 years ago
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 OffscreenCanvas
instead of Canvas
, which is more accurately what it is current anyways. Maybe even change import { createCanvas } from 'canvas'
to import { OffscreenCanvas } from 'canvas'
(but keep the old function as well for compatibility)?
Anything that makes us more compatible I'm 👍
(but keep the old function as well for compatibility)?
yea, sounds good!
but OffscreenCanvas is a constructor doe... need to use new
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 import()
instead cuz esm are async b/c of top level await
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. convertToBlob is async either way also. so can just lazy import it when needed instead
(but keep the old function as well for compatibility)?
yea, sounds good! but OffscreenCanvas is a constructor doe...
Yeah, we could export an OffscreenCanvas
class
and have createCanvas
return new OffscreenCanvas(...)
fyi, fetch-blob is a ESM only package
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 .toBlob
gonna be implemented..?
b/c node-canvas is more like a OffscreenCanvas
then i have already made a PR that adds the async convertToBlob
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)
? deprecatenew Image()
andloadImage()
? use FinalizationRegistry and ImageBitmap.close to release the memory in native binding