如何使用 FabricJS 查詢 Line 物件的真實中心座標?
在本教程中,我們將學習如何使用 FabricJS 查詢 Line 的中心座標。Line 元素是 FabricJS 提供的基本元素之一,用於建立直線。由於線元素在幾何上是一維的,並且不包含內部,因此永遠不會填充。我們可以透過建立 fabric.Line 的例項,指定線的 x 和 y 座標並將其新增到畫布上來建立線物件。為了找到 Line 物件的真實中心座標,我們使用 getCenterPoint 方法。
語法
getCenterPoint(): fabric.Point
使用 getCenterPoint 方法
示例
讓我們來看一個程式碼示例,以檢視使用 getCenterPoint 方法時的輸出。getCenterPoint 方法返回物件的真實中心座標。在本例中,輸出為 x= 110.5 和 y= 150.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>Using getCenterPoint method</h2> <p> You can open console from dev tools and see that the logged output contains the center points </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 line = new fabric.Line([70, 100, 150, 200], { stroke: "blue", }); // Add it to the canvas canvas.add(line); // Using the getCenterPoint method console.log( "The center point of Line object is: ", line.getCenterPoint() ); </script> </body> </html>
使用 getCenterPoint 方法以及不同的線起點和終點座標
示例
在本例中,我們使用了 getCenterPoint 方法來獲取 Line 例項的中心座標,其中起點和終點座標分別為 (100, 250) 和 (250, 40)。中心點為 175.5 和 145.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> Using getCenterPoint method with different starting and ending coordinates of line </h2> <p> You can open console from dev tools and see that the logged output contains the center points </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 line = new fabric.Line([100, 250, 250, 40], { stroke: "blue", }); // Add it to the canvas canvas.add(line); // Using the getCenterPoint method console.log( "The center point of Line object is: ", line.getCenterPoint() ); </script> </body> </html>
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP