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

Proposal: CanvasRenderingContext2D#getImageData should accept optional target ImageData parameter #5707

Closed
oguzeroglu opened this issue Jul 2, 2020 · 8 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: canvas

Comments

@oguzeroglu
Copy link

oguzeroglu commented Jul 2, 2020

At this point CanvasRenderingContext2D#getImageData returns a copy of the existing buffer. This may be a performance problem for applications where one needs to process the ImageData ideally 60 times per second.

One nice feature to have for getImageData would be an additional targetImageData parameter, in that case getImageData would write the bytes over the target buffer rather than creating a new ImageData.

Example usage:

var targetImageData = ctx.createImageData(width, height);

ctx.getImageData(x, y, width, height, targetImageData);

// The buffer of targetImageData may now be transferred to workers via transferables for processing.

Any feedback on that?

@Kaiido
Copy link
Member

Kaiido commented Jul 3, 2020

Was already proposed in #4748
But it's been closed in favor of #4785, which I think is more aimed at short-circuiting video -> ImageData.
I personally agree that having this option on the canvas's getImageData would still be useful too, so I'm not sure it should be closed because of #4785, but maybe the discussion should be done there anyway.

@domenic domenic added addition/proposal New features or enhancements topic: canvas labels Jul 24, 2020
@annevk annevk added the needs implementer interest Moving the issue forward requires implementers to express interest label Apr 25, 2021
@annevk
Copy link
Member

annevk commented Apr 25, 2021

cc @whatwg/canvas

@kdashg
Copy link

kdashg commented Apr 26, 2021

Yes, this is low-hanging fruit and we should do it.

@annevk
Copy link
Member

annevk commented Apr 27, 2021

Small quibble on the signature, presumably you don't need width and height parameters as the target already has those. And since you don't need a return value perhaps we should have a new method, e.g.,

getImageDataInto(x, y, destination)

It seems that should fail though if destination points to a detached buffer so you cannot transfer to workers, but rather have to copy, right?

@Kaiido
Copy link
Member

Kaiido commented Apr 27, 2021

It seems that should fail though if destination points to a detached buffer so you cannot transfer to workers, but rather have to copy, right?

The ImageData can be constructed from a TypedArray, so you can still transfer the buffer back and forth and only create a new ImageData wrapper.

@trusktr
Copy link

trusktr commented May 10, 2021

But it's been closed in favor of #4785,

Wouldn't that mean an unnecessary extra allocation of memory to hold the pixels, thus mem and CPU waste?

I personally agree that having this option on the canvas's getImageData would still be useful too

Not just useful, but necessary for avoiding memory allocation.

@trusktr

This comment has been minimized.

@annevk
Copy link
Member

annevk commented Dec 18, 2024

Let's forward duplicate this into #10855 as that has some momentum.

@annevk annevk closed this as not planned Won't fix, can't repro, duplicate, stale Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: canvas
Development

No branches or pull requests

6 participants