HTML5 畫布中的 getContext 是什麼?


Canvas 元素有一個叫 getContext 的 DOM 方法,用於獲取渲染上下文及其繪圖功能。此函式帶一個引數,即 2d 的上下文型別。

以下是獲取所需上下文並檢查你的瀏覽器是否支援 <canvas> 元素的程式碼

var canvas = document.getElementById("mycanvas");
if (canvas.getContext){
   var ctx = canvas.getContext('2d');

   // drawing code here
   } else {

   // canvas-unsupported code here
}

於 28-01-2020 更新

1000+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.