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

Add exception info for HTMLCanvasElement.transferControlToOffscreen() and OffscreenCanvas.transferToImageBitmap() #36631

Merged
merged 7 commits into from
Nov 22, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ If the context identifier is not supported, or the canvas has already been set t
### Exceptions

- `InvalidStateError` {{domxref("DOMException")}}
- : Throws if the canvas has transferred its control to offscreen via calling `HTMLCanvasElement.transferControlToOffscreen()`.
- : Throws if the canvas has transferred its control to offscreen via calling {{domxref("HTMLCanvasElement.transferControlToOffscreen()")}}.
wbamberg marked this conversation as resolved.
Show resolved Hide resolved

## Examples

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ None.

An {{domxref("OffscreenCanvas")}} object.

### Exceptions

- `InvalidStateError` {{domxref("DOMException")}}
- : Throws if the canvas has been set to a context mode via calling {{domxref("HTMLCanvasElement.getContext()")}} or has transferred its control to offscreen via calling {{domxref("HTMLCanvasElement.transferControlToOffscreen()")}}.
wbamberg marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see in https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-transfercontroltooffscreen where we throw an exception if the canvas "has transferred its control to offscreen via calling {{domxref("HTMLCanvasElement.transferControlToOffscreen()")}}", can you point to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transferControlToOffscreen() method, when invoked, must run these steps:
If this canvas element's context mode is not set to none, throw an "InvalidStateError" DOMException.
Let offscreenCanvas be a new OffscreenCanvas object with its width and height equal to the values of the width and height content attributes of this canvas element.
Set the placeholder canvas element of offscreenCanvas to a weak reference to this canvas element.
Set this canvas element's context mode to placeholder.
Return offscreenCanvas.The transferControlToOffscreen() method, when invoked, must run these steps:
If this canvas element's context mode is not set to none, throw an "InvalidStateError" DOMException.
Let offscreenCanvas be a new OffscreenCanvas object with its width and height equal to the values of the width and height content attributes of this canvas element.
Set the placeholder canvas element of offscreenCanvas to a weak reference to this canvas element.
Set this canvas element's context mode to placeholder.
Return offscreenCanvas.

After calling transferControlToOffscreen(), the canvas's context mode will be set to "placeholder"; so if calling transferControlToOffscreen() again, it will fail at the first step.

Sample code is as follows:

image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! (I didn't doubt you were right here, I just couldn't see it!)


## Examples

The following example shows how to transfer control to an {{domxref("OffscreenCanvas")}} object on the main thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ If your goal is to pass the `ImageBitmap` to other web APIs which do not consume

If you call `transferToImageBitmap()` and don't intend to pass it to {{domxref("ImageBitmapRenderingContext.transferFromImageBitmap()")}}, consider whether you need to call `transferToImageBitmap()` at all. Many web APIs which accept `ImageBitmap` also accept `OffscreenCanvas` as an argument.

### Exceptions

- `InvalidStateError` {{domxref("DOMException")}}
- : Throws if the canvas has transferred to another context scope, for example, to worker; or hasn't set to a context mode via calling {{domxref("OffscreenCanvas.getContext()")}}.
skyclouds2001 marked this conversation as resolved.
Show resolved Hide resolved

## Examples

```js
Expand Down