在本教程中,我们将学习如何使用 fabricjs 查找 image 对象的真实中心坐标。我们可以通过创建fabric.image的实例来创建一个image对象。由于它是 fabricjs 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松自定义它。为了找到 image 对象的真实中心坐标,我们使用 getcenterpoint方法。
语法getcenterpoint(): fabric.point
使用getcenterpoint方法示例让我们看一个代码示例,以查看使用 getcenterpoint 方法时记录的输出。 getcenterpoint 方法返回对象的真实中心坐标。在本例中,记录的输出为 x= 256.5 和 y= 91,它们是中心点。
<!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 getcenterpoint method</h2> <p> you can open console from dev tools and see that the logged output contains the center points </p> <canvas id=canvas></canvas> <img src=https://www.tutorialspoint.com/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 getcenterpoint method console.log( the center point of image object is: , image.getcenterpoint() ); </script></body></html>
使用getcenterpoint方法和cropx方法示例在此示例中,我们使用了 getcenterpoint 方法和cropx 方法来证明记录的中心点值将保持不变。
<!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 getcenterpoint method along with cropx method</h2> <p> you can open console from dev tools and see that the logged output contains the center points </p> <canvas id=canvas></canvas> <img src=https://www.tutorialspoint.com/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, cropx: 70, }); // add it to the canvas canvas.add(image); // using the getcenterpoint method console.log( the center point of image object is: , image.getcenterpoint() ); </script></body></html>
以上就是fabricjs – 如何找到 image 对象的真实中心坐标?的详细内容。