如何使用 FabricJS 設定橢圓的描邊寬度?


在本教程中,我們將學習如何使用 FabricJS 設定橢圓的描邊寬度。橢圓是 FabricJS 提供的各種形狀之一。為了建立橢圓,我們將建立一個 fabric.Ellipse 類的例項並將其新增到畫布上。strokeWidth 屬性允許我們指定物件描邊的寬度。

語法

new fabric.Ellipse( { strokeWidth: Number }: Object)

引數

  • options (可選) - 此引數是一個 Object,它為我們的橢圓提供了額外的自定義功能。使用此引數,可以更改與物件相關的顏色、游標、描邊寬度以及許多其他屬性,其中 strokeWidth 是一個屬性。

選項鍵

  • strokeWidth - 此屬性接受一個 Number 值,允許我們指定物件的描邊寬度。其預設值為 1。

示例 1

物件的描邊的預設外觀

以下示例描述了橢圓物件的描邊的預設外觀。由於我們沒有使用 strokeWidth 屬性,因此正在渲染預設寬度。

<!DOCTYPE html>
<html>
   <head>
      <!-- Adding the Fabric JS Library-->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
   </head>

   <body>
      <h2>Setting the width of stroke of an Ellipse using FabricJS</h2>
      <p>Here we haven't set the <b>strokeWidth</b> explicitly. By default, it is set to 1. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");

         // Initiate an ellipse instance
         var ellipse = new fabric.Ellipse({
            left: 115,
            top: 50,
            rx: 80,
            ry: 50,
            fill: "#ffdead",
            stroke: "#cd853f",
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

示例 2

strokeWidth 屬性作為鍵傳遞

在此示例中,我們將 strokeWidth 屬性的值設定為 5。這將確保我們的橢圓物件以寬度為 5 畫素的描邊進行渲染。

<!DOCTYPE html>
<html>
   <head>
      <!-- Adding the Fabric JS Library-->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
   </head>

   <body>
      <h2>Setting the width of stroke of an Ellipse using FabricJS</h2>
      <p>Observe the stroke width of the ellipse. Here we have set the <b>strokeWidth</b> at 5 pixels.</p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");

         // Initiate an ellipse instance
         var ellipse = new fabric.Ellipse({
            left: 115,
            top: 50,
            rx: 80,
            ry: 50,
            fill: "#ffdead",
            stroke: "#cd853f",
            strokeWidth: 5,
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

更新於: 2022-05-25

116 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.