在本教學中,我們將學習如何使用 FabricJS 來尋找線條的複雜度。 Line 元素是 FabricJS 中提供的基本元素之一。它用於創建直線。由於線元素在幾何上是一維的並且不包含內部,因此它們永遠不會被填充。我們可以透過建立 fabric.Line 的實例來建立線條對象,指定線條的 x 和 y 座標並將其新增至畫布。為了獲得 Line 物件的複雜度,我們使用複雜度方法。如果當前物件直接繼承自基底類別而不是子類,則此方法將傳回 1。
complexity(): Number
讓我們看一個程式碼範例,以查看使用複雜性方法取得Line 實例的複雜性時記錄的輸出。除非進行子分類,否則複雜度為 1。
<!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 the complexity method</h2> <p>You can open console from dev tools and see the logged output</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 complexity method console.log("The complexity of Line instance is: ", line.complexity()); </script> </body> </html>
在此範例中,我們使用複雜度方法來比較線實例和多邊形實例的複雜度。您可以從開發工具開啟控制台來查看複雜度的不同。
<!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 the complexity method to compare different objects</h2> <p>You can open console from dev tools and see that the complexities are different </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", }); // Initiate a Polygon object var polygon = new fabric.Polyline( [ { x: 50, y: 30 }, { x: 105, y: 10 }, { x: 160, y: 30 }, { x: 100, y: 150 }, ], { fill: "red", left: 300, top: 70, } ); // Add both to the canvas canvas.add(line); canvas.add(polygon); // Using the complexity method console.log("The complexity of Line instance is: ", line.complexity()); console.log( "The complexity of Polygon instance is: ", polygon.complexity() ); </script> </body> </html>
以上是如何使用 FabricJS 來尋找 Line 實例的複雜度?的詳細內容。更多資訊請關注PHP中文網其他相關文章!