CanvasRenderingContext2D:canvas 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Canvas APICanvasRenderingContext2D.canvas 属性是对与给定上下文关联的 HTMLCanvasElement 对象的只读引用。如果没有与之关联的 <canvas> 元素,对象值为 null

一个 HTMLCanvasElement 对象。

示例

给出 <canvas> 元素:

html
<canvas id="canvas"></canvas>

你可以通过 CanvasRenderingContext2D 调用内部的 canvas 属性,获取 canvas 元素的引用:

js
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.canvas; // HTMLCanvasElement

规范

Specification
HTML Standard
# dom-context-2d-canvas-dev

浏览器兼容性

BCD tables only load in the browser

参见