--- title: CanvasRenderingContext2D.canvas slug: Web/API/CanvasRenderingContext2D/canvas tags: - API - Canvas translation_of: Web/API/CanvasRenderingContext2D/canvas ---
CanvasRenderingContext2D
.canvas
属性是 Canvas API 的一部分,是对与给定上下文关联的{{domxref("HTMLCanvasElement")}}对象的只读引用。如果没有 {{HTMLElement("canvas")}} 元素与之对应,对象值为{{jsxref("null")}} 。
ctx.canvas;
给出 {{HTMLElement("canvas")}} 元素:
<canvas id="canvas"></canvas>
你可以通过CanvasRenderingContext2D调用内部的canvas属性,获取canvas的一个反向引用:
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.canvas // HTMLCanvasElement
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-canvas", "CanvasRenderingContext2D.canvas")}} | {{Spec2('HTML WHATWG')}} |
{{Compat("api.CanvasRenderingContext2D.canvas")}}