如何使用 FabricJS 查詢 Image 例項的複雜度?
在本教程中,我們將學習如何使用 FabricJS 查詢 Image 例項的複雜度。我們可以透過建立 fabric.Image 的例項來建立一個 Image 物件。由於它是 FabricJS 的基本元素之一,因此我們也可以透過應用角度、不透明度等屬性輕鬆自定義它。為了找到 Image 例項的複雜度,我們使用complexity 方法。如果當前物件直接繼承自基類而不是子類,則此方法將返回 1。
語法
complexity(): Number
使用 complexity 方法
示例
讓我們看一個程式碼示例,瞭解當我們使用 complexity 方法獲取 Image 例項的複雜度時記錄的輸出。除非是子類,否則複雜度為 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> <img src="https://tutorialspoint.tw/images/logo.png" id="img1" style="display: none" /> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the image element var imageElement = document.getElementById("img1"); // Initiate an Image object var image = new fabric.Image(imageElement, { top: 50, left: 110, }); // Add it to the canvas canvas.add(image); // Using the complexity method console.log("The complexity of Image instance is: ", image.complexity()); </script> </body> </html>
使用 complexity 方法比較不同的物件
示例
在此示例中,我們使用了 complexity 方法來比較 Image 例項和多邊形例項的複雜度。您可以從開發者工具中開啟控制檯,檢視它們的複雜度不同。
<!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 the logged output</p> <canvas id="canvas"></canvas> <img src="https://tutorialspoint.tw/images/logo.png" id="img1" style="display: none" /> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the image element var imageElement = document.getElementById("img1"); // Initiate an Image object var image = new fabric.Image(imageElement, { top: 50, left: 50, }); // 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: 450, top: 70, } ); // Add them both to the canvas canvas.add(image); canvas.add(polygon); // Using the complexity method console.log("The complexity of Image instance is: ", image.complexity()); console.log( "The complexity of Polygon instance is: ", polygon.complexity() ); </script> </body> </html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP