如何使用 FabricJS 識別 Line 例項的型別?
在本教程中,我們將學習如何使用 FabricJS 識別 Line 例項的型別。Line 元素是 FabricJS 提供的基本元素之一。它用於建立直線。由於線元素在幾何上是一維的,並且不包含內部,因此它們永遠不會被填充。我們可以透過建立 fabric.Line 的例項、指定線的 x 和 y 座標並將其新增到畫布上來建立線物件。為了識別 Line 例項的型別,我們使用 isType 方法。
語法
isType(type: String): Boolean
引數
type − 此引數接受一個字串,指定我們要檢查的型別。
使用 isType 方法
示例
讓我們看一個程式碼示例,以檢視使用 isType 方法時記錄的輸出。isType 方法根據例項的型別是否與我們想要檢查的型別匹配返回 true 或 false 值。在這種情況下,由於型別匹配,因此返回 true 值。
<!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 isType method</h2> <p> You can open console from dev tools and see that the logged output contains a true value </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([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Add it to the canvas canvas.add(line); // Using isType method console.log( "Is the specified type identical to a line instance? : ", line.isType("line") ); </script> </body> </html>
使用 isType 方法與不同的值
示例
在本例中,我們使用 isType 檢查指定的圓形型別是否與線例項相同。這裡,返回 false 值,因為它們不相同。
<!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 isType method with a different value</h2> <p> You can open console from dev tools and see that the logged output contains a false value </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([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Add it to the canvas canvas.add(line); // Using isType method console.log( "Is the specified type identical to a line instance? : ", line.isType("circle") ); </script> </body> </html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP