在本教程中,我们将学习如何使用 FabricJS 获取图像的不透明度。我们可以通过创建fabric.Image的实例来创建一个Image对象。由于它是 FabricJS 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松自定义它。为了获取 Image 的不透明度,我们使用 getObjectOpacity 方法。 p>
语法
getObjectOpacity(): Number
使用getObjectOpacity方法
示例
让我们看一个代码示例,以查看使用 getObjectOpacity 方法时记录的输出。在这种情况下,控制台中将显示默认的不透明度,即 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 getObjectOpacity method</h2> <p> You can open console from dev tools and see that the logged value is being displayed in the console </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 getObjectOpacity method console.log("The opacity is: ", image.getObjectOpacity()); </script> </body> </html>
使用 getObjectOpacity 方法并传递 opacity 属性
示例
让我们看一个代码示例,以查看当 getObjectOpacity 方法与 opacity 属性结合使用时记录的输出。在本例中,图像对象的不透明度被指定为 0.7,因此记录的输出将为 0.7。
<!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 getObjectOpacity method and passing the opacity property</h2> <p> You can open console from dev tools and see that the opacity value is being displayed in the console </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, opacity: 0.7, }); // Add it to the canvas canvas.add(image); // Using getObjectOpacity method console.log("The opacity is: ", image.getObjectOpacity()); </script> </body> </html>
以上就是如何使用 FabricJS 获取 Image 对象的不透明度?的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。