HTML Canvas - actualBoundingBoxLeft 屬性



TextMetrics 介面的 actualBoundingBoxLeft 屬性是一個只讀方法,它返回一個雙精度值,給出 CanvasRenderingContext2D 介面上下文物件的文字基線到在其中渲染文字的邊界矩形方框左側的平行距離。該雙精度值以 CSS 畫素為單位。

TextMetrics 介面的 actualBoundingBoxRight 屬性是一個只讀方法,它返回一個雙精度值,給出 CanvasRenderingContext2D 介面上下文物件的文字基線到在其中渲染文字的邊界矩形方框右側的平行距離。該雙精度值以 CSS 畫素為單位。

示例 − (返回值)

以下示例演示 HTML Canvas actualBoundingBoxLeftactualBoundingBoxRight 屬性。實現程式碼如下所示。

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Reference API</title>
   <style>
      body {
         margin: 10px;
         padding: 10px;
      }
   </style>
</head>
<body onload="text();">
   <canvas id="canvas" width="500" height="100" style="border: 1px solid black;"></canvas>
   <script>
      function text() {
         var canvas = document.getElementById("canvas");
         var context = canvas.getContext("2d");
         context.font = '50px Verdana';
         context.fillText('TutorialsPoint', 50, 50);
         var word = context.measureText('TutorialsPoint');
         alert("Bounding box left : " + word.actualBoundingBoxLeft + "\nBounding box right : " + word.actualBoundingBoxRight);
      }
   </script>
</body>
</html>

輸出

上述程式碼在網頁上返回的輸出如下 −

HTML Canvas ActualBoundingBoxDescent Property HTML Canvas ActualBoundingBoxDescent Property
html_canvas_text.htm
廣告
© . All rights reserved.