HtmlCanvasElement Class

https://html.spec.whatwg.org/multipage/canvas.html#htmlcanvaselement

public sealed class HtmlCanvasElement : HtmlElement
Inheritance
HtmlElement HtmlCanvasElement

Properties

NameDescription
ContentGets or sets the content attribute: "drawable" when the children of the canvas are laid out and can be drawn into it, else "fallback" (they are its fallback content).
Heighthttps://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-height
OnElementGeometryUpdateGets or sets the event handler of the elementgeometryupdate event (the onelementgeometryupdate event handler attribute).
OnPaintGets or sets the event handler of the paint event (the onpaint event handler attribute).
Widthhttps://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-width
  • Experimental (WEBPLATEXP0023)

Gets or sets the content attribute: "drawable" when the children of the canvas are laid out and can be drawn into it, else "fallback" (they are its fallback content).

[Experimental("WEBPLATEXP0023", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public string Content { get; set; }

https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-height

public uint Height { get; set; }

Property value

uint

Exceptions

DomException
An InvalidStateError when the canvas is the placeholder of an OffscreenCanvas.

See also

OnElementGeometryUpdate

HtmlElements.g.cs
  • Experimental (WEBPLATEXP0023)

Gets or sets the event handler of the elementgeometryupdate event (the onelementgeometryupdate event handler attribute).

[Experimental("WEBPLATEXP0023", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public Action<Event>? OnElementGeometryUpdate { get; set; }
  • Experimental (WEBPLATEXP0023)

Gets or sets the event handler of the paint event (the onpaint event handler attribute).

[Experimental("WEBPLATEXP0023", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public Action<Event>? OnPaint { get; set; }

https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-width

public uint Width { get; set; }

Property value

uint

Exceptions

DomException
An InvalidStateError when the canvas is the placeholder of an OffscreenCanvas.

See also

Methods

NameDescription
CaptureElementImage(Element)Returns the last snapshot of a drawable element, which stays the same when the element changes.
CaptureStream(double?)Captures the content of the canvas as a stream with one video track, a CanvasCaptureMediaStreamTrack (https://w3c.github.io/mediacapture-fromelement/#dom-htmlcanvaselement-capturestream): a frame is captured each time the canvas changes.
ClearElementGeometry(object?)Clears the canvas transform of an element: it is the identity again.
GetContext(string, CanvasRenderingContext2DSettings?)Returns the rendering context of the canvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext): a CanvasRenderingContext2D for "2d" and an ImageBitmapRenderingContext for "bitmaprenderer" (created the first time), or null for a context type that is not supported ("webgl", "webgl2", "webgpu") or that does not match the context of the canvas. The 2D context needs a rendering backend (BrowsingContextOptions.RenderingBackend): without one, getContext("2d") returns null.
GetElementImageDefaultSize(object?)Returns the size of the last snapshot of an element in the coordinates of the bitmap of the canvas: the size of its border box scaled by the ratio of the size of the bitmap to the size of the content box of the canvas.
GetElementTransform(Element)Returns the canvas transform of an element (a new matrix), the identity when none is set.
RequestPaint()Fires a paint event at the next rendering update, even when no drawable element changed.
ToBlob(BlobCallback, string, double?)Serializes the bitmap as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-toblob) and invokes the callback with a Blob of the file in a task (null when the canvas has no pixels).
ToDataUrl(string, double?)Returns a data: URL of the bitmap serialized as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-todataurl): PNG, JPEG or WebP (another type is serialized as PNG), or data:, when the canvas has no pixels.
TransferControlToOffscreen()Transfers the control of the canvas to an OffscreenCanvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-transfercontroltooffscreen): the canvas becomes its placeholder and displays the frames drawn by its rendering context (e.g. in a worker).
UpdateElementGeometry(object?, UpdateElementGeometryOptions?)Sets the canvas transform of an element (its geometry), which maps its border box to the content box of the canvas.

CaptureElementImage(Element)

HtmlCanvasElement.HtmlInCanvas.cs
  • Experimental (WEBPLATEXP0023)

Returns the last snapshot of a drawable element, which stays the same when the element changes.

[Experimental("WEBPLATEXP0023", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public ElementImage CaptureElementImage(Element element)

Parameters

element Element
A drawable element of the canvas.

Returns

ElementImage The image of the element.

Exceptions

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

See also

CaptureStream(double?)

CaptureFromElement.cs

Captures the content of the canvas as a stream with one video track, a CanvasCaptureMediaStreamTrack (https://w3c.github.io/mediacapture-fromelement/#dom-htmlcanvaselement-capturestream): a frame is captured each time the canvas changes.

public MediaStream CaptureStream(double? frameRequestRate = null)

Parameters

frameRequestRate double?
The maximum number of frames per second, or 0 to capture the frames only when CanvasCaptureMediaStreamTrack.RequestFrame is called.

Returns

MediaStream

Exceptions

DomException
A SecurityError: the canvas is not origin-clean; a NotSupportedError: the frame rate is negative.

See also

ClearElementGeometry(object?)

HtmlCanvasElement.HtmlInCanvas.cs
  • Experimental (WEBPLATEXP0023)

Clears the canvas transform of an element: it is the identity again.

[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 canvas, or an ElementImage of the canvas.

Exceptions

DomException
An InvalidStateError when the element is not a drawable element of the canvas.

See also

GetContext(string, CanvasRenderingContext2DSettings?)

EmbeddedContent.cs

Returns the rendering context of the canvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext): a CanvasRenderingContext2D for "2d" and an ImageBitmapRenderingContext for "bitmaprenderer" (created the first time), or null for a context type that is not supported ("webgl", "webgl2", "webgpu") or that does not match the context of the canvas. The 2D context needs a rendering backend (BrowsingContextOptions.RenderingBackend): without one, getContext("2d") returns null.

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

Parameters

contextId string
The type of the context.
options CanvasRenderingContext2DSettings?
The options of the context.

Returns

object? The context, or null.

Exceptions

DomException
An InvalidStateError when the canvas is the placeholder of an OffscreenCanvas.

See also

GetElementImageDefaultSize(object?)

HtmlCanvasElement.HtmlInCanvas.cs
  • Experimental (WEBPLATEXP0023)

Returns the size of the last snapshot of an element in the coordinates of the bitmap of the canvas: the size of its border box scaled by the ratio of the size of the bitmap to the size of the content box of the canvas.

[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?
A drawable element of the canvas, or an ElementImage of the canvas.

Returns

ElementImageDefaultSize The size.

Exceptions

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

See also

GetElementTransform(Element)

HtmlCanvasElement.HtmlInCanvas.cs
  • Experimental (WEBPLATEXP0023)

Returns the canvas transform of an element (a new matrix), the identity when none is set.

[Experimental("WEBPLATEXP0023", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public DomMatrix GetElementTransform(Element element)

Parameters

element Element
A drawable element of the canvas.

Returns

DomMatrix The canvas transform.

Exceptions

DomException
An InvalidStateError when the element is not a drawable element of the canvas.

See also

  • Experimental (WEBPLATEXP0023)

Fires a paint event at the next rendering update, even when no drawable element changed.

[Experimental("WEBPLATEXP0023", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public void RequestPaint()

ToBlob(BlobCallback, string, double?)

EmbeddedContent.cs

Serializes the bitmap as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-toblob) and invokes the callback with a Blob of the file in a task (null when the canvas has no pixels).

public void ToBlob(BlobCallback callback, string type = "image/png", double? quality = null)

Parameters

callback BlobCallback
The callback.
type string
The MIME type of the file (PNG, JPEG or WebP; another type is serialized as PNG).
quality double?
The quality of the lossy formats: a number from 0 to 1, else the default quality.

Exceptions

DomException
An InvalidStateError for the placeholder of an OffscreenCanvas, a SecurityError when the canvas is not origin-clean.

See also

ToDataUrl(string, double?)

EmbeddedContent.cs

Returns a data: URL of the bitmap serialized as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-todataurl): PNG, JPEG or WebP (another type is serialized as PNG), or data:, when the canvas has no pixels.

public string ToDataUrl(string type = "image/png", double? quality = null)

Parameters

type string
The MIME type of the file.
quality double?
The quality of the lossy formats from 0 to 1 (another value is the default quality), or null for the default quality.

Returns

string

Exceptions

DomException
A SecurityError when the canvas is not origin-clean.

See also

TransferControlToOffscreen()

EmbeddedContent.cs

Transfers the control of the canvas to an OffscreenCanvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-transfercontroltooffscreen): the canvas becomes its placeholder and displays the frames drawn by its rendering context (e.g. in a worker).

public OffscreenCanvas TransferControlToOffscreen()

Returns

OffscreenCanvas

Exceptions

DomException
An InvalidStateError when the canvas has a context or was already transferred.

See also

UpdateElementGeometry(object?, UpdateElementGeometryOptions?)

HtmlCanvasElement.HtmlInCanvas.cs
  • Experimental (WEBPLATEXP0023)

Sets the canvas transform of an element (its geometry), which maps its border box to the content box of the canvas.

[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 canvas, or an ElementImage of the canvas.
options UpdateElementGeometryOptions?
The transform and the clip of the element.

Exceptions

DomException
An InvalidStateError when the element is not a drawable element of the canvas.
TypeErrorException
The matrix is not valid.

See also