首頁 > web前端 > js教程 > 主體

FabricJS – 如何將 Line 物件在繪製物件堆疊中向上移動一步?

王林
發布: 2023-08-24 14:37:08
轉載
875 人瀏覽過

FabricJS – 如何将 Line 对象在绘制对象堆栈中向上移动一步?

在本教程中,我們將學習如何使用 FabricJS 將 Line 物件在繪製物件堆疊中向上移動一步。 Line 元素是 FabricJS 中提供的基本元素之一。它用於創建直線。由於線元素在幾何上是一維的並且不包含內部,因此它們永遠不會被填充。我們可以透過建立 fabric.Line 的實例來建立線條對象,指定線條的 x 和 y 座標並將其新增至畫布。為了將 Line 物件在繪製物件堆疊中向上移動一步,我們使用 bringForward 方法。

文法

bringForward(intersecting: Boolean): fabric.Object
登入後複製

參數

  • Intersecting - 此參數接受一個Boolean 值,當分配一個「true」值時,將物件傳送到前面下一個上部相交對象。如果值為“false”,它通常會將物件向上傳送到堆疊中的下一個物件。此參數是可選的。

使用bringForward方法

範例

讓我們來看一個程式碼範例,看看使用 bringForward 方法時的輸出。 bringForward 方法將物件在繪製物件堆疊中向上移動一步。在本例中,使用 bringForward 方法將 line1 傳送到 line2 之上。

<!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>Using bringForward method</h2>
   <p>
      You can see that line1 (blue) has been moved up in the stack of drawn objects
   </p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a Line object
      var line1 = new fabric.Line([200, 100, 100, 40], {
         stroke: "blue",
         strokeWidth: 20,
      });

      // Initiate another Line object
      var line2 = new fabric.Line([200, 70, 70, 40], {
         stroke: "red",
         strokeWidth: 20,
      });

      // Add both to the canvas
      canvas.add(line1);
      canvas.add(line2);

      // Using bringForward method
      line1.bringForward();
   </script>
</body>
</html>
登入後複製

在三個物件和啟用交集鍵的情況下使用 bringForward 方法

範例

在此範例中,我們使用了三個線條對象,分別是line1、line2 和line3。儘管它們已按照數字順序添加到畫布中,但 line1 明顯位於 line3 之上。這是因為我們使用了啟用了交集鍵的 bringForward 方法,該方法將 line1 發送到其下一個上部相交物件(即 line3)的頂部。

<!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>Using bringForward method with three objects and intersection key enabled</h2>
   <p>
      You can see that the blue line now lies above the green line which is line number 3
   </p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a Line object
      var line1 = new fabric.Line([200, 100, 100, 40], {
         stroke: "blue",
         strokeWidth: 20,
      });

      // Initiate another Line object
      var line2 = new fabric.Line([500, 70, 400, 40], {
         stroke: "red",
         strokeWidth: 20,
      });

      // Initiate another Line object
      var line3 = new fabric.Line([200, 30, 30, 90], {
         stroke: "green",
         strokeWidth: 20,
      });

      // Add them all to the canvas
      canvas.add(line1);
      canvas.add(line2);
      canvas.add(line3);

      // Using bringForward method
      line1.bringForward(true);
   </script>
</body>
</html>
登入後複製

以上是FabricJS – 如何將 Line 物件在繪製物件堆疊中向上移動一步?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!