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

如何使用 FabricJS 获取 Line 对象的坐标?

在本教程中,我们将展示如何使用 fabricjs 获取线的坐标。 line 元素是 fabricjs 中提供的基本元素之一。它用于创建直线。由于线元素在几何上是一维的并且不包含内部,因此它们永远不会被填充。我们可以通过创建 fabric.line 的实例来创建线条对象,指定线条的 x 和 y 坐标并将其添加到画布中。为了获取 line 对象的坐标,我们使用 getcoords 方法。
语法 getcoords(): array
使用 getcoords 方法 示例 让我们看一个代码示例,以查看 getcoords 方法执行时记录的输出用过的。 getcoords 方法以数组格式返回 line 的左上角、右上角、右下角和左下角坐标。
<!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 getcoords 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([50, 100, 310, 100], { stroke: blue, strokewidth: 10, }); // add it to the canvas canvas.add(line); // using getcoords method console.log(the coordinates are: , line.getcoords()); </script></body></html>
使用 getcoords 方法绘制斜线 示例 在此示例中,我们使用了 getcoords 方法获取具有不同起始和结束坐标的 line 实例的坐标。我们可以看到记录的输出是:(100, 40)、(220, 40)、(220,120)、(100,120),分别是该行的左上角、右上角、右下角和左下角坐标.
<!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 getcoords method for a slant line</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([200, 100, 100, 40], { stroke: blue, strokewidth: 20 }); // add it to the canvas canvas.add(line); // using getcoords method console.log(the coordinates are: , line.getcoords()); </script></body></html>
以上就是如何使用 fabricjs 获取 line 对象的坐标?的详细内容。
其它类似信息

推荐信息