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

如何使用 FabricJS 将 Image 对象在画布的当前视口上垂直居中?

在本教程中,我们将展示如何将图像对象垂直居中使用 fabricjs 的画布的当前视口。我们可以通过创建一个 image 对象fabric.image 的实例。既然它是fabricjs的基本元素之一,我们也可以通过应用角度、不透明度等属性轻松自定义它。为了使图像居中对象垂直在画布的当前视口上,我们使用 viewportcenterv 方法。
语法viewportcenterv(): fabric.object
image 对象的默认外观示例让我们看一个代码示例,看看当使用 viewportcenterv 方法时我们的 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>default appearance of the image object</h2> <p>you can see the default appearance of image object</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); </script></body></html>
使用viewportcenterv方法示例让我们看一个代码示例,看看图像对象在使用viewportcenterv方法。在这种情况下,我们的图像对象将居中相对于画布的当前视口垂直。
<!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 viewportcenterv method</h2> <p> you can see that the image object is centered vertically with respect to the current viewport of canvas </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 viewportcenterv method image.viewportcenterv(); </script></body></html>
结论在本教程中,我们使用两个示例来演示如何使图像对象居中使用 fabricjs 在画布的当前视口上垂直显示。
以上就是如何使用 fabricjs 将 image 对象在画布的当前视口上垂直居中?的详细内容。
其它类似信息

推荐信息