Skip to content
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

Support ImageBitmap and createImageBitmap #876

Open
brettz9 opened this issue Feb 24, 2017 · 7 comments
Open

Support ImageBitmap and createImageBitmap #876

brettz9 opened this issue Feb 24, 2017 · 7 comments

Comments

@brettz9
Copy link
Contributor

brettz9 commented Feb 24, 2017

Feature

Add and expose ImageBitmap and createImageBitmap and allow their use within relevant canvas methods (ideally also working with jsdom to hook into this as well).

@piranna
Copy link
Contributor

piranna commented Jan 16, 2019

Does this include to define it as a context?

@brettz9
Copy link
Contributor Author

brettz9 commented Mar 13, 2019

That would need to be done on the end of jsdom (or whatever DOM library), but yes, the request is for allowing node-canvas to be used by such libraries for such purposes.

@jimmywarting
Copy link
Contributor

NodeJS just introduced a fs.openAsBlob()

import { openAsBlob } from 'node:fs'
const blob = await openAsBlob('the.file.png')
const bitmap = await createImageBitmap(blob)

you may also fetch blobs natively

const blob = await response.blob()

@liudonghua123
Copy link

liudonghua123 commented Jul 9, 2023

I want to make https://github.com/imgly/background-removal-js support both web and node platform, and I found this canvas for node project but it seems missing some api like createImageBitmap and so on, how can I implements in node?

See also:

@jimmywarting
Copy link
Contributor

jimmywarting commented Jul 10, 2023

If you would like to support some basic version of createImageBitmap yourself right now you could implement something like:

import { openAsBlob } from 'node:fs'
import { Image } from 'canvas'

async function createImageBitmap (blob) {
  const ab = await blob.arrayBuffer()
  const img = new Image()
  await new Promise(rs => {
    img.onload = rs
    img.src = new Uint8Array(ab) // might have to do: Buffer.from(ab)
  })
  return img
}

const blob = await fetch(request).then(res => res.blob())
const blob = await openAsBlob('the.file.png')
const bitmap = await createImageBitmap(blob)

imo i think that the Image class should be removed / deprecated and be replaced with a own built in createImageBitmap powered by node-canvas

@cayblood
Copy link

@jimmywarting the code sample above refers to undefined methods, like squid, and the ab arrayBuffer never gets used. Can you please fix that code snippet for those of us who aren't as familiar?

@jimmywarting

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants