语法new fabric.image( element: htmlimageelement | htmlcanvaselement | htmlvideoelement | string, { opacity: number }: object, callback: function)
参数element - 此参数接受 htmlimageelement、htmlcanvaselement、htmlvideoelement 或表示图像元素的字符串。该字符串应该是一个 url,并将作为图像加载。
选项(可选) - 此参数是一个对象,它为我们的对象提供额外的自定义。使用此参数,可以更改与不透明度为属性的图像对象相关的原点、描边宽度和许多其他属性。
回调(可选) - 此参数是一个函数,在应用最终过滤器后调用..选项键opacity - 该属性接受一个number,它允许我们控制一个东西。 opacity 属性的默认值为 1。
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 image object</h2> <p>you can see that the image object is fully opaque</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: 50, }); // add it to the canvas canvas.add(image); </script></body></html>
将opacity属性作为键传递示例在此示例中,我们将看到为 opacity 属性赋值如何改变画布中图像对象的不透明度。这里我们使用 0.3 作为不透明度,因此使我们的图像对象看起来半透明而不是完全不透明。
<!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>passing the opacity property as key</h2> <p>you can see our image object is not fully opaque</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: 50, opacity: 0.3, }); // add it to the canvas canvas.add(image); </script></body></html>
以上就是如何使用fabricjs设置图像的不透明度?的详细内容。
