HTML Canvas - getContextAttributes() 方法



HTML Canvas 的getContextAttributes() 方法是 Canvas API 的一部分,它返回一個包含相同上下文引數的 Canvas 物件。

這些屬性通常在上下文建立時由getContext() 方法請求。它通常在使用其物件請求時返回在畫布元素內繪製的任何圖形的上下文。

語法

以下是 HTML Canvas getContextAttributes() 方法的語法

CanvasRenderingContext2D.getContextAttributes();

引數

因為它只是一個返回方法,所以它不接受任何引數。

返回值

CanvasRenderingContext2D 介面方法使用上下文物件,並且當 HTML Canvas getContextAttributes() 方法被物件使用時,物件引數透過控制檯或視窗警報返回。

示例

以下示例使用 HTML Canvas getContextAttributes() 方法在控制檯中返回畫布元素的上下文引數。實現程式碼如下所示。

<!DOCTYPE html>
<html lang="en">
   <head>
      <title>Reference API</title>
      <style>
         body {
            margin: 10px;
            padding: 10px;
         }
      </style>
   </head>
   <body onload="Context();">
      <canvas id="canvas" width="200" height="200" style="border: 1px solid black;"></canvas>
      <script>
         function Context() {
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");
            console.log(context.getContextAttributes());
         }
      </script>
   </body>
</html>

輸出

該方法在網頁上返回的輸出為:

HTML Canvas getContextAttributes() Method

在控制檯螢幕上可以看到的輸出為:

HTML Canvas getContextAttributes() Method

示例

以下示例在控制檯中返回畫布元素內填充矩形的上下文引數。實現程式碼如下所示。

<!DOCTYPE html>
<html lang="en">
   <head>
      <title>Reference API</title>
      <style>
         body {
            margin: 10px;
            padding: 10px;
         }
      </style>
   </head>
   <body onload="Context();">
      <canvas id="canvas" width="200" height="200" style="border: 1px solid black;"></canvas>
      <script>
         function Context() {
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");
            context.fillRect(10, 10, 150, 100);
            console.log(context.getContextAttributes());
         }
      </script>
   </body>
</html>

輸出

該方法在網頁上返回的輸出為:

HTML Canvas getContextAttributes() Method

在控制檯螢幕上可以看到的輸出為:

HTML Canvas getContextAttributes() Method
html_canvas_rectangles.htm
廣告
© . All rights reserved.