HTMLCanvasElement

HTMLCanvasElement接口提供用于操纵<canvas>元素的布局和表示的属性和方法。 HTMLCanvasElement接口还继承了HTMLElement接口的属性和方法。

属性

从其父项HTMLElement继承属性。

HTMLCanvasElement.height
是一个正整数,反映了<canvas> 元素的height HTML属性,以CSS像素表示。 如果未指定属性,或者将其设置为无效值(例如负数),则使用默认值150。
HTMLCanvasElement.width
是一个正整数,反映了<canvas> 元素的width HTML属性,以CSS像素表示。 如果未指定属性,或者将其设置为无效值(例如负数),则使用默认值300。
HTMLCanvasElement.mozOpaque
是反映 <canvas>元素的moz-opaque 属性的Boolean。 它让画布知道半透明性是否会成为一个因素。 如果画布知道没有透明感,则可以优化绘画性能。 仅基于Mozilla的浏览器支持此功能, 可以使用标准化的canvas.getContext('2d', { alpha: false }) 代替。
HTMLCanvasElement.mozPrintCallback
是最初为空的函数。 Web内容可以将其设置为JavaScript函数,该函数将在打印页面时重新绘制画布时调用。 调用时,回调将传递一个实现了MozCanvasPrintState接口的“ printState”对象。 回调可以从printState对象获取要绘制的上下文,然后必须在完成时在其上调用done() 。 mozPrintCallback 的目的是在所使用打印机的分辨率下获得较高分辨率的画布渲染。 请参阅此博客文章

方法

从其父项HTMLElement继承方法。

HTMLCanvasElement.captureStream()
返回CanvasCaptureMediaStream ,它是对画布表面的实时视频捕获。
HTMLCanvasElement.getContext()
返回画布上的绘图上下文;如果不支持上下文ID,则返回null。 绘图上下文可让您在画布上绘图。 调用getContext传入"2d" 可以返回一个CanvasRenderingContext2D对象,也可以传入"webgl"(或"experimental-webgl")返回一个WebGLRenderingContext 对象。 此上下文仅在实现WebGL的浏览器上可用。
HTMLCanvasElement.toDataURL()
返回一个数据URL,该URL包含由类型参数指定的格式的图像(默认为png)。 返回的图像分辨率为96dpi。
HTMLCanvasElement.toBlob()
创建一个Blob 对象,表示canvas中包含的图像; 该文件可以由用户代理决定是否缓存在磁盘上或存储在内存中。
HTMLCanvasElement.transferControlToOffscreen()
将控制权转移到主线程或辅助线程上的 OffscreenCanvas对象。

过时的方法

HTMLCanvasElement.mozGetAsFile()
返回代表画布中包含的图像的File 对象; 该文件是基于内存的文件,具有指定的名称。 如果未指定类型,则图像类型为image / png。

事件

使用addEventListener()监听这些事件。

webglcontextcreationerror
如果用户代理无法创建WebGLRenderingContext 或WebGL2RenderingContext 上下文,则触发该事件。
webglcontextlost
如果用户代理检测到与WebGLRenderingContext 或WebGL2RenderingContext 对象关联的绘图缓冲区已丢失,则触发此事件。
webglcontextrestored
如果用户代理为WebGLRenderingContext 或WebGL2RenderingContext 对象恢复绘图缓冲区,则触发该事件。

规范

Specification Status Comment
HTML Living Standard
HTMLCanvasElement
Living Standard Primary definition of HTMLCanvasElement.
Media Capture from DOM Elements
HTMLCanvasElement
Working Draft Adds the method captureStream().

浏览器支持

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
HTMLCanvasElementChrome Full support 1Edge Full support 12Firefox Full support 1.5IE Full support 9Opera Full support 9
Notes
Full support 9
Notes
Notes Opera Mini 5.0 and later has partial support.
Safari Full support 2WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1
Notes
Full support 10.1
Notes
Notes Opera Mini 5.0 and later has partial support.
Safari iOS Full support 1Samsung Internet Android Full support 1.0
captureStream
Experimental
Chrome Full support 51Edge Full support ≤79Firefox Full support 43IE No support NoOpera Full support 36Safari Full support 11WebView Android Full support 51Chrome Android Full support 51Firefox Android Full support 43Opera Android Full support 36Safari iOS Full support 11Samsung Internet Android Full support 5.0
getContextChrome Full support 1Edge Full support 12Firefox Full support 1.5IE Full support 9Opera Full support 9Safari Full support 2WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
heightChrome Full support 1Edge Full support 12Firefox Full support 1.5IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
mozFetchAsStream
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 13 — 43IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
mozGetAsFile
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 4 — 74
Notes
No support 4 — 74
Notes
Notes mozGetAsFile() was deprecated in Firefox 26 (in 2013) and was removed entirely in Firefox 74.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 4Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
mozOpaque
Non-standard
Chrome No support NoEdge No support NoFirefox Full support 3IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 4Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
toBlobChrome Full support 50Edge Full support 79
Full support 79
No support 12 — 79
Prefixed
Prefixed Implemented with the vendor prefix: ms
Firefox Full support 19IE Full support 10
Prefixed
Full support 10
Prefixed
Prefixed Implemented with the vendor prefix: ms
Opera Full support 37Safari Full support 11WebView Android Full support 50Chrome Android Full support 50Firefox Android Full support 4Opera Android Full support 37Safari iOS Full support 11Samsung Internet Android Full support 5.0
toDataURLChrome Full support 1Edge Full support 12Firefox Full support 2IE Full support 9Opera Full support 9Safari Full support 4WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3.2Samsung Internet Android Full support 1.0
transferControlToOffscreen
Experimental
Chrome Full support 69Edge Full support 79Firefox Full support 44
Disabled
Full support 44
Disabled
Disabled From version 44: this feature is behind the gfx.offscreencanvas.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android Full support 69Firefox Android Full support 44
Disabled
Full support 44
Disabled
Disabled From version 44: this feature is behind the gfx.offscreencanvas.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support 10.0
webglcontextcreationerror eventChrome Full support 9Edge Full support 12Firefox Full support 49IE Full support 11Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support 25Firefox Android Full support 49Opera Android Full support 12Safari iOS Full support 8Samsung Internet Android Full support 1.5
webglcontextlost eventChrome Full support 9Edge Full support 12Firefox Full support 4IE Full support 11Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support 25Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 8Samsung Internet Android Full support 1.5
webglcontextrestored eventChrome Full support 9Edge Full support 12Firefox Full support 4IE Full support 11Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support 25Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 8Samsung Internet Android Full support 1.5
widthChrome Full support 1Edge Full support 12Firefox Full support 1.5IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.