FabricJS – 如何將線條物件在繪製物件的堆疊中向上移動一步?
在本教程中,我們將學習如何使用 FabricJS 將線條物件在繪製物件的堆疊中向上移動一步。線條元素是 FabricJS 提供的基本元素之一。它用於建立直線。由於線條元素在幾何上是一維的並且不包含內部,因此它們永遠不會被填充。我們可以透過建立fabric.Line的例項,指定線條的 x 和 y 座標並將其新增到畫布上來建立線條物件。為了將線條物件在繪製物件的堆疊中向上移動一步,我們使用bringForward方法。
語法
bringForward(intersecting: Boolean): fabric.Object
引數
Intersecting − 此引數接受一個布林值,當分配“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>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP