WebGLRenderingContext: canvas property

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.

Note: This feature is available in Web Workers.

Thuộc tính WebGLRenderingContext.canvas là một tham chiếu chỉ đọc tới đối tượng HTMLCanvasElement hoặc OffscreenCanvas được liên kết với ngữ cảnh. Nó có thể là null nếu nó không được liên kết với phần tử <canvas> hoặc đối tượng OffscreenCanvas.

Giá trị

Hoặc là một đối tượng HTMLCanvasElement hoặc OffscreenCanvas hoặc null.

Ví dụ

Phần tử canvas

Cho phần tử <canvas> này:

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

Bạn có thể lấy lại một tham chiếu tới nó từ WebGLRenderingContext bằng cách sử dụng thuộc tính canvas:

js
const canvas = document.getElementById("canvas");
const gl = canvas.getContext("webgl");
gl.canvas; // HTMLCanvasElement

Canvas ngoài màn hình

Ví dụ sử dụng đối tượng thử nghiệm OffscreenCanvas.

js
const offscreen = new OffscreenCanvas(256, 256);
const gl = offscreen.getContext("webgl");
gl.canvas; // OffscreenCanvas

Thông số kỹ thuật

Thông số kỹ thuật
WebGL Specification
# DOM-WebGLRenderingContext-canvas

Khả năng tương thích của trình duyệt

Xem thêm