您好,欢迎访问一九零五行业门户网

如何使用 FabricJS 查找 Line 实例的复杂度?

在本教程中,我们将学习如何使用 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 实例的复杂度?的详细内容。
其它类似信息

推荐信息