ホームページ  >  記事  >  ウェブフロントエンド  >  FabricJSを使用してキャンバス上の選択領域の境界線の幅を設定するにはどうすればよいですか?

FabricJSを使用してキャンバス上の選択領域の境界線の幅を設定するにはどうすればよいですか?

WBOY
WBOY転載
2023-09-03 14:53:111425ブラウズ

如何使用 FabricJS 设置画布上选择区域边框的宽度?

この記事では、FabricJS を使用してキャンバス上の選択領域の境界線の幅を設定する方法を学びます。選択領域はマウスで選択された領域を表し、この領域の下にあるすべてのオブジェクトが選択されます。 FabricJS では、selectionLineWidth プロパティを使用して選択領域の境界線の幅を調整できます。

構文

new fabric.Canvas(element: HTMLElement|String, { selectionLineWidth: Number }: Object)

パラメータ

  • 要素 - このパラメータは です em> 要素自体は、 document.getElementById() または 要素自体の ID を使用して取得できます。 FabricJS キャンバスはこの要素で初期化されます。

  • オプション (オプション) - このパラメータは、キャンバスの追加のカスタマイズを提供するオブジェクトです。このパラメーターを使用すると、色、カーソル、境界線の幅など、キャンバスに関連する多くのプロパティを変更できます。selectionLineWidth はそのプロパティの 1 つです。選択範囲の境界線で使用される線の幅を決定する数値を受け入れます。デフォルト値は 1 です。

例 1

selectionLineWidth キーをクラスに渡す

コード例を見て、FabricJS 設定の使用方法を理解しましょう。キャンバス内の選択領域の境界線の幅。 SelectionLineWidth パラメーターは値として数値を受け入れます。設定した数値が大きいほど、キャンバス領域の境界線が広くなります。

<!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 the selection area border in canvas using FabricJs</h2>
   <p>Select an area around the object to see the width of the selection area border.</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas", {
         selectionLineWidth: 23,
      });
      // Creating an instance of the fabric.Rect class
      var rect = new fabric.Rect({
         left: 170,
         top: 90,
         width: 60,
         height: 80,
         fill: "#006400",
         angle: 90,
      });
      // Adding it to the canvas
      canvas.add(rect);
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
   </script>
</body>
</html>

例 2

selectionLineWidth をselectionColor およびselectionBorderColor とともに使用する

selectionLineWidth パラメーターを他のパラメーターと組み合わせることができます。 プロパティを使用するselectionColorselectionBorderColor などを使用すると、それぞれ選択範囲の色を設定したり、その選択範囲の境界線の色を調整したりできます。コードがどのようなものかを見てみましょう:

<!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 selection area border in canvas using Fabric</h2>
   <p>Select an area around the object to see the selection color and selection border color.</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas", {
         selectionLineWidth: 3,
         selectionColor: "rgba(42,82,190,0.3)",
         selectionBorderColor: "black",
      });
      // Creating an instance of the fabric.Rect class
      var rect = new fabric.Rect({
         left: 170,
         top: 90,
         width: 60,
         height: 80,
         fill: "#006400",
         angle: 90,
      });
      // Adding it to the canvas
      canvas.add(rect);
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
   </script>
</body>
</html>

以上がFabricJSを使用してキャンバス上の選択領域の境界線の幅を設定するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。