Skip to content

Window::create_image_bitmap_with_canvas_rendering_context_2d throws error #3227

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

Closed
JohnScience opened this issue Jan 8, 2023 · 2 comments · Fixed by #3231
Closed

Window::create_image_bitmap_with_canvas_rendering_context_2d throws error #3227

JohnScience opened this issue Jan 8, 2023 · 2 comments · Fixed by #3231
Labels

Comments

@JohnScience
Copy link

JohnScience commented Jan 8, 2023

Describe the Bug

I wanted to conveniently create an ImageBitmap from CanvasRenderingContext2d. When I found Window::create_image_bitmap_with_canvas_rendering_context_2d, I expected that it's a convenience method for doing so.

However, when I used it, I received the following error:

TypeError: Failed to execute 'createImageBitmap' on 'Window': The provided value is not of type '(Blob or HTMLCanvasElement or HTMLImageElement or HTMLVideoElement or ImageBitmap or ImageData or OffscreenCanvas or SVGImageElement or VideoFrame)'.

Steps to Reproduce

async fn update_buf_dims_gracefully(
    canvas: &HtmlCanvasElement,
    ctx: &CanvasRenderingContext2d,
    window: &Window,
) {
    let client_width= canvas.client_width();
    let client_height = canvas.client_height();

    let image_bitmap: JsFuture = window.
        create_image_bitmap_with_canvas_rendering_context_2d(ctx)
        .unwrap()
        .into();

    let image_bitmap = image_bitmap.await;
    match image_bitmap {
        Ok(image_bitmap) => { log(&image_bitmap) },
        Err(err) => { log(&err) }
    }

    canvas.set_width(client_width as u32);
    canvas.set_height(client_height as u32);
}

The code above is an incomplete implementation of a function relying on Window::create_image_bitmap_with_canvas_rendering_context_2d.

Expected Behavior

Window::create_image_bitmap_with_canvas_rendering_context_2d should either be removed or be functional

Actual Behavior

Results in an error.

Additional Context

No additional context.

@JohnScience JohnScience added the bug label Jan 8, 2023
@jneem
Copy link
Contributor

jneem commented Jan 9, 2023

My reading of this is that ImageBitmapSource should be defined as CanvasImageSource or Blob or ImageData, and then inlining the definition of CanvasImageSource gives the HTMLXXElements, ImageBitmap, VideoFrame, and OffscreenCanvas. The BufferSource variant seems to come from here, but I can't find any source for the CanvasRenderingContext2d variant.

@Liamolucko
Copy link
Collaborator

Yeah, that shouldn't be there. It's there because wasm-bindgen's WebIDL was initially taken from Firefox, which for whatever reason non-standardly supports passing CanvasRenderingContext2D to createImageBitmap.

The BufferSource variant is also broken, since the overload it's supposed to be a part of has a completely different signature to the regular createImageBitmap:

Promise<ImageBitmap> createImageBitmap (BufferSource buffer, long offset, long length, ImageFormat format, ImagePixelLayout layout);

It doesn't seem like that standard ever really went anywhere, either (the repo is archived).

So, I think they should both be removed. That is of course a breaking change, but I think it makes sense to do since any attempt to use them wouldn't work anyway (cc @alexcrichton).

Liamolucko added a commit to Liamolucko/wasm-bindgen that referenced this issue Jan 12, 2023
…`BufferSource` from `createImageBitmap`

Resolves rustwasm#3227

Both APIs are broken, to some extent, which is why I think it makes sense to remove them despite the potential breakage. Calling `createImageBitmap` with a `BufferSource` will instantly throw an error, and calling it with a `CanvasRenderingContext2D` will also fail except in Firefox, where it gives a deprecation warning.

`BufferSource` was there as part of the abandoned video capture worker API (https://w3c.github.io/mediacapture-worker). It looks like we inherited it from Firefox, which supported it behind a flag at the time we copied our WebIDL from it but later removed it. I also removed the rest of that API while I was at it.

`CanvasRenderingContext2D` is there because, for whatever reason, Firefox non-standardly accepts it in `createImageBitmap`, and we took our WebIDL from them. It's deprecated, and https://bugzilla.mozilla.org/show_bug.cgi?id=1500782 talks about removing it, but it hasn't been done yet.
Liamolucko added a commit to Liamolucko/wasm-bindgen that referenced this issue Jan 12, 2023
…`BufferSource` from `createImageBitmap`

Resolves rustwasm#3227

Both APIs are broken, to some extent, which is why I think it makes sense to remove them despite the potential breakage. Calling `createImageBitmap` with a `BufferSource` will instantly throw an error, and calling it with a `CanvasRenderingContext2D` will also fail except in Firefox, where it gives a deprecation warning.

`BufferSource` was there as part of the abandoned video capture worker API (https://w3c.github.io/mediacapture-worker). It looks like we inherited it from Firefox, which supported it behind a flag at the time we copied our WebIDL from it but later removed it. I also removed the rest of that API while I was at it.

`CanvasRenderingContext2D` is there because, for whatever reason, Firefox non-standardly accepts it in `createImageBitmap`, and we took our WebIDL from them. It's deprecated, and https://bugzilla.mozilla.org/show_bug.cgi?id=1500782 talks about removing it, but it hasn't been done yet.
alexcrichton pushed a commit that referenced this issue Jan 12, 2023
…`BufferSource` from `createImageBitmap` (#3231)

Resolves #3227

Both APIs are broken, to some extent, which is why I think it makes sense to remove them despite the potential breakage. Calling `createImageBitmap` with a `BufferSource` will instantly throw an error, and calling it with a `CanvasRenderingContext2D` will also fail except in Firefox, where it gives a deprecation warning.

`BufferSource` was there as part of the abandoned video capture worker API (https://w3c.github.io/mediacapture-worker). It looks like we inherited it from Firefox, which supported it behind a flag at the time we copied our WebIDL from it but later removed it. I also removed the rest of that API while I was at it.

`CanvasRenderingContext2D` is there because, for whatever reason, Firefox non-standardly accepts it in `createImageBitmap`, and we took our WebIDL from them. It's deprecated, and https://bugzilla.mozilla.org/show_bug.cgi?id=1500782 talks about removing it, but it hasn't been done yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants