HtmlCanvasElement Class
https://html.spec.whatwg.org/multipage/canvas.html#htmlcanvaselement
public sealed class HtmlCanvasElement : HtmlElement- Inheritance
HtmlElementHtmlCanvasElement
Properties
| Name | Description |
|---|---|
| Content | 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). |
| Height | https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-height |
| OnElementGeometryUpdate | Gets or sets the event handler of the elementgeometryupdate event (the onelementgeometryupdate event handler attribute). |
| OnPaint | Gets or sets the event handler of the paint event (the onpaint event handler attribute). |
| Width | https://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; }Height
HandWrittenReflection.cshttps://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-height
public uint Height { get; set; }Property value
Exceptions
DomException- An
InvalidStateErrorwhen the canvas is the placeholder of anOffscreenCanvas.
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; }OnPaint
HtmlElements.g.cs- 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
Exceptions
DomException- An
InvalidStateErrorwhen the canvas is the placeholder of anOffscreenCanvas.
See also
Methods
| Name | Description |
|---|---|
| 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
elementElement- A drawable element of the canvas.
Returns
ElementImage The image of the element.
Exceptions
DomException- An
InvalidStateErrorwhen the element is not a drawable element of the canvas, or it has no snapshot.
See also
CaptureStream(double?)
CaptureFromElement.csCaptures 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
frameRequestRatedouble?- The maximum number of frames per second, or 0 to capture the frames only when
CanvasCaptureMediaStreamTrack.RequestFrameis called.
Returns
Exceptions
DomException- A
SecurityError: the canvas is not origin-clean; aNotSupportedError: 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
elementobject?- An element of the canvas, or an
ElementImageof the canvas.
Exceptions
DomException- An
InvalidStateErrorwhen the element is not a drawable element of the canvas.
See also
GetContext(string, CanvasRenderingContext2DSettings?)
EmbeddedContent.csReturns 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
contextIdstring- The type of the context.
optionsCanvasRenderingContext2DSettings?- The options of the context.
Returns
object? The context, or null.
Exceptions
DomException- An
InvalidStateErrorwhen the canvas is the placeholder of anOffscreenCanvas.
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
elementobject?- A drawable element of the canvas, or an
ElementImageof the canvas.
Returns
ElementImageDefaultSize The size.
Exceptions
DomException- An
InvalidStateErrorwhen 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
elementElement- A drawable element of the canvas.
Returns
DomMatrix The canvas transform.
Exceptions
DomException- An
InvalidStateErrorwhen the element is not a drawable element of the canvas.
See also
RequestPaint()
HtmlCanvasElement.HtmlInCanvas.cs- 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.csSerializes 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
callbackBlobCallback- The callback.
typestring- The MIME type of the file (PNG, JPEG or WebP; another type is serialized as PNG).
qualitydouble?- The quality of the lossy formats: a number from 0 to 1, else the default quality.
Exceptions
DomException- An
InvalidStateErrorfor the placeholder of anOffscreenCanvas, aSecurityErrorwhen the canvas is not origin-clean.
See also
ToDataUrl(string, double?)
EmbeddedContent.csReturns 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.
Parameters
typestring- The MIME type of the file.
qualitydouble?- The quality of the lossy formats from 0 to 1 (another value is the default quality), or null for the default quality.
Returns
Exceptions
DomException- A
SecurityErrorwhen the canvas is not origin-clean.
See also
TransferControlToOffscreen()
EmbeddedContent.csTransfers 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
Exceptions
DomException- An
InvalidStateErrorwhen 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
elementobject?- An element of the canvas, or an
ElementImageof the canvas. optionsUpdateElementGeometryOptions?- The transform and the clip of the element.
Exceptions
DomException- An
InvalidStateErrorwhen the element is not a drawable element of the canvas. TypeErrorException- The matrix is not valid.