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

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)

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