OffscreenCanvas Class
A canvas that is not in a document (https://html.spec.whatwg.org/multipage/canvas.html#the-offscreencanvas-interface): created in a window or in a worker, or by transferControlToOffscreen() for a canvas element (its placeholder), whose frames are then displayed by the element. It is transferable (to a worker), and needs a rendering backend for its 2D context.
public sealed class OffscreenCanvas : EventTarget- Inheritance
EventTargetOffscreenCanvas
Constructors
| Name | Description |
|---|---|
| OffscreenCanvas(ulong, ulong) | Creates an OffscreenCanvas of the given size, without context (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas). |
OffscreenCanvas(ulong, ulong)
OffscreenCanvas.csCreates an OffscreenCanvas of the given size, without context (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas).
Parameters
See also
Properties
| Name | Description |
|---|---|
| Height | Gets or sets the height of the bitmap (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-height): setting it resets the bitmap and the 2D context. |
| OnContextLost | Gets or sets the event handler of the contextlost event (never fired). |
| OnContextRestored | Gets or sets the event handler of the contextrestored event (never fired). |
| Width | Gets or sets the width of the bitmap (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-width): setting it resets the bitmap and the 2D context. |
Height
OffscreenCanvas.csGets or sets the height of the bitmap (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-height): setting it resets the bitmap and the 2D context.
public ulong Height { get; set; }OnContextLost
OffscreenCanvas.csGets or sets the event handler of the contextlost event (never fired).
OnContextRestored
OffscreenCanvas.csGets or sets the event handler of the contextrestored event (never fired).
Width
OffscreenCanvas.csGets or sets the width of the bitmap (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-width): setting it resets the bitmap and the 2D context.
public ulong Width { get; set; }Methods
| Name | Description |
|---|---|
| ClearElementGeometry(object?) | Clears the canvas transform of an element of the placeholder canvas (see OffscreenCanvas.UpdateElementGeometry). |
| ConvertToBlob(ImageEncodeOptions?) | Returns a Blob of the bitmap serialized as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-converttoblob): PNG, JPEG or WebP (another type is serialized as PNG). |
| GetContext(OffscreenRenderingContextId, CanvasRenderingContext2DSettings?) | Returns the rendering context of the canvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-getcontext): an OffscreenCanvasRenderingContext2D for "2d" (null without rendering backend), an ImageBitmapRenderingContext for "bitmaprenderer", or null for another type or when the canvas already has a context of another type. |
| GetElementImageDefaultSize(object?) | Returns the size of an element image in the pixels of the bitmap of the canvas (see HtmlCanvasElement.GetElementImageDefaultSize). |
| TransferToImageBitmap() | Returns an ImageBitmap of the bitmap and replaces the bitmap with a new transparent black bitmap (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-transfertoimagebitmap). |
| UpdateElementGeometry(object?, UpdateElementGeometryOptions?) | Sets the canvas transform of an element of the placeholder canvas (see HtmlCanvasElement.UpdateElementGeometry): the updates are batched, and an elementgeometryupdate event is fired at the placeholder canvas in a microtask. |
ClearElementGeometry(object?)
OffscreenCanvas.ElementImages.cs- Experimental (WEBPLATEXP0023)
Clears the canvas transform of an element of the placeholder canvas (see OffscreenCanvas.UpdateElementGeometry).
[Experimental("WEBPLATEXP0023", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public void ClearElementGeometry(object? element)Parameters
elementobject?- An element of the placeholder canvas, or an
ElementImageof the placeholder canvas.
Exceptions
DomException- An
InvalidStateErrorwhen the canvas has no placeholder, or the element is not a drawable element of its placeholder.
See also
ConvertToBlob(ImageEncodeOptions?)
OffscreenCanvas.csReturns a Blob of the bitmap serialized as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-converttoblob): PNG, JPEG or WebP (another type is serialized as PNG).
public Task<Blob> ConvertToBlob(ImageEncodeOptions? options = null)Parameters
optionsImageEncodeOptions?
Returns
Task<Blob> A task completed in a task of the event loop; it fails with an InvalidStateError when the canvas is detached, a SecurityError when it is not origin-clean, an IndexSizeError when it has no pixels.
See also
GetContext(OffscreenRenderingContextId, CanvasRenderingContext2DSettings?)
OffscreenCanvas.csReturns the rendering context of the canvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-getcontext): an OffscreenCanvasRenderingContext2D for "2d" (null without rendering backend), an ImageBitmapRenderingContext for "bitmaprenderer", or null for another type or when the canvas already has a context of another type.
public object? GetContext(OffscreenRenderingContextId contextId, CanvasRenderingContext2DSettings? options = null)Parameters
contextIdOffscreenRenderingContextIdoptionsCanvasRenderingContext2DSettings?
Returns
Exceptions
DomException- An
InvalidStateErrorwhen the canvas is detached.
See also
GetElementImageDefaultSize(object?)
OffscreenCanvas.ElementImages.cs- Experimental (WEBPLATEXP0023)
Returns the size of an element image in the pixels of the bitmap of the canvas (see HtmlCanvasElement.GetElementImageDefaultSize).
[Experimental("WEBPLATEXP0023", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public ElementImageDefaultSize GetElementImageDefaultSize(object? element)Parameters
elementobject?- An element of the placeholder canvas, or an
ElementImageof the placeholder canvas.
Returns
ElementImageDefaultSize The size.
Exceptions
DomException- An
InvalidStateErrorwhen the canvas has no placeholder, or the element is not a drawable element of its placeholder or has no snapshot.
See also
TransferToImageBitmap()
OffscreenCanvas.csReturns an ImageBitmap of the bitmap and replaces the bitmap with a new transparent black bitmap (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas-transfertoimagebitmap).
public ImageBitmap TransferToImageBitmap()Returns
Exceptions
DomException- An
InvalidStateErrorwhen the canvas is detached or has no context.
See also
UpdateElementGeometry(object?, UpdateElementGeometryOptions?)
OffscreenCanvas.ElementImages.cs- Experimental (WEBPLATEXP0023)
Sets the canvas transform of an element of the placeholder canvas (see HtmlCanvasElement.UpdateElementGeometry): the updates are batched, and an elementgeometryupdate event is fired at the placeholder canvas in a microtask.
[Experimental("WEBPLATEXP0023", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public void UpdateElementGeometry(object? element, UpdateElementGeometryOptions? options = null)Parameters
elementobject?- An element of the placeholder canvas, or an
ElementImageof the placeholder canvas. optionsUpdateElementGeometryOptions?- The transform and the clip of the element.
Exceptions
DomException- An
InvalidStateErrorwhen the canvas has no placeholder, or the element is not a drawable element of its placeholder.