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
EventTarget OffscreenCanvas

Constructors

NameDescription
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.cs

Creates an OffscreenCanvas of the given size, without context (https://html.spec.whatwg.org/multipage/canvas.html#dom-offscreencanvas).

public OffscreenCanvas(ulong width, ulong height)

Properties

NameDescription
HeightGets 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.
OnContextLostGets or sets the event handler of the contextlost event (never fired).
OnContextRestoredGets or sets the event handler of the contextrestored event (never fired).
WidthGets 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.

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.

public ulong Height { get; set; }

Property value

ulong

Exceptions

DomException
An InvalidStateError when the canvas is detached.

See also

OnContextLost

OffscreenCanvas.cs

Gets or sets the event handler of the contextlost event (never fired).

public Action<Event>? OnContextLost { get; set; }

OnContextRestored

OffscreenCanvas.cs

Gets or sets the event handler of the contextrestored event (never fired).

public Action<Event>? OnContextRestored { get; set; }

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.

public ulong Width { get; set; }

Property value

ulong

Exceptions

DomException
An InvalidStateError when the canvas is detached.

See also

Methods

NameDescription
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

element object?
An element of the placeholder canvas, or an ElementImage of the placeholder canvas.

Exceptions

DomException
An InvalidStateError when the canvas has no placeholder, or the element is not a drawable element of its placeholder.

See also

ConvertToBlob(ImageEncodeOptions?)

OffscreenCanvas.cs

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).

public Task<Blob> ConvertToBlob(ImageEncodeOptions? options = null)

Parameters

options ImageEncodeOptions?

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.cs

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.

public object? GetContext(OffscreenRenderingContextId contextId, CanvasRenderingContext2DSettings? options = null)

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

element object?
An element of the placeholder canvas, or an ElementImage of the placeholder canvas.

Returns

ElementImageDefaultSize The size.

Exceptions

DomException
An InvalidStateError when the canvas has no placeholder, or the element is not a drawable element of its placeholder or has no snapshot.

See also

TransferToImageBitmap()

OffscreenCanvas.cs

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).

public ImageBitmap TransferToImageBitmap()

Returns

ImageBitmap

Exceptions

DomException
An InvalidStateError when 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

element object?
An element of the placeholder canvas, or an ElementImage of the placeholder canvas.
options UpdateElementGeometryOptions?
The transform and the clip of the element.

Exceptions

DomException
An InvalidStateError when the canvas has no placeholder, or the element is not a drawable element of its placeholder.

See also