Canvas JavaScript

HTMLCanvasElement Class

HTMLCanvasElement Class
InheritsDescription
HTMLElementThis represents any HTML element.
ElementThis represents any kind of element in a document, including types like MathML or SVG elements.
NodeThis represent every kind of DOM node, including a document.
EventTargetThis represents a broader group of objects that can receive event notices.
PropertiesDescription
heightThis defines the height of the canvas element in pixels.
widthThis defines the width of the canvas element in pixels.
MethodsDescription
MediaStream captureStream(fps = auto)This function takes a parameter that specifies the number of frames that are captured per second. If nothing is passed to the function, a frame is captured each time the canvas changes. If 0 is passed in a frame will captured only when requestFrame() is called on the returned object.
various getContext(conType)
various getContext(conType, conAttr)
This function gets a context for drawing onto the canvas element. The first parameter, conType, is string that defines the type of context and it has one of the following values: "2d", "webgl" (originally "experimental-webgl"), "webgl12", "webgpu", or "bitmaprenderer". The second paramter, conAttr, is an object that contains a set of values to that define the nature of the context with the following members:
  • alpha - a boolean that specifies whether the canvas has an alpha channel. Opaque drawing without alpha values can be faster.
  • colorSpace - a string with the value "srgb" or "display-p3"
  • desynchronized - a boolean which may make rendering faster when it is false
  • willReadFrequently - a boolean that when true requires buffers to be explicitly clearer or written over
The return type is one of the following:
  • CanvasRenderingContext2D for "2d"
  • WebGLRenderingContext for "webgl"
  • WebGL2RenderingContext for "webgl2"
  • GPUCanvasContext for "webgpu"
  • ImageBitmapRenderingContext for "bitmaprenderer"
toBlob(callback)
toBlob(callback, type)
toBlob(callback, type, quality)
This create a blob from the canvas image data and passes it into the callback. The parameter, callback, is a function that takes the Blob object as its only argument. The type paramter is a string that specifies the type of the blob and its default value is "image/png". The quality parameter is a number from 0 to 1 that indicates the quality for lossy formats, like "image/jpeg" or "image/webp".
string toDataURL()
string toDataURL(type)
string toDataURL(type, quality)
This function returns a URL that contains a base64 specification of the image of the canvas. The type paramter is a string that specifies the type of the blob and its default value is "image/png". The quality parameter is a number from 0 to 1 that indicates the quality for lossy formats, like "image/jpeg" or "image/webp".
transferControlToOffscreen()This tranfers control to an offscreen canvas. Operations may be specified on a separate worker thread.
EventsDescription
webglcontextcreationerrorThe context can not be created.
webglcontextlostThe current context loses its drawing buffer.
webglcontextrestoredThe context is restored, but previously created resources are no longer valid.
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.