在本教程中,我们将学习如何使用 fabricjs 删除克隆图像中的当前对象阴影。我们可以通过创建fabric.image的实例来创建一个image对象。由于它是 fabricjs 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松自定义它。为了删除克隆图像中当前对象的阴影,我们使用 withoutshadow属性。
语法cloneasimage( callback: function, { withoutshadow: boolean }: object): fabric.object
参数 回调(可选) - 此参数是一个函数,将使用克隆图像实例作为第一个调用论证。
选项(可选) - 此参数是一个可选的对象,它为我们的克隆图像提供额外的自定义。使用此参数,我们可以设置乘数、裁剪克隆图像、删除当前对象变换或可以更改许多其他属性,其中 withoutshadow 是一个属性。
选项键 withoutshadow - 此属性接受一个布尔值,该值确定是否删除当前对象阴影。此属性是可选的。
使用 withoutshadow 属性并向其传递一个“true”值示例让我们看一个代码示例,了解使用 withoutshadow 属性并向其传递“true”值时克隆的 image 对象如何显示。这里,图像对象已经分配有阴影属性。然而,由于我们将“true”值传递给 withoutshadow 属性,该对象阴影将被删除,并且我们的克隆图像将不再拥有阴影。
<!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 withoutshadow property and passing it a ‘true’ value</h2> <p>you can see that clone image does not have a shadow</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 a shadow object var shadow = new fabric.shadow({ color: black, blur: 12, }); // initiate an image object var image = new fabric.image(imageelement, { top: 50, left: 110, skewx: 20, shadow: shadow, }); // using cloneasimage method image.cloneasimage( function (img) { img.set(top, 150); img.set(left, 150); canvas.add(img); }, { withoutshadow: true, } ); </script></body></html>
使用withoutshadow属性并向其传递一个“false”值示例在此示例中,我们使用了 withoutshadow 属性并为其传递了一个“false”值。因此,我们的克隆对象的阴影将不会被删除。
<!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 withoutshadow property and passing it a ‘false’ value</h2> <p>you can see that clone image contains a shadow</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 a shadow object var shadow = new fabric.shadow({ color: black, blur: 12, }); // initiate an image object var image = new fabric.image(imageelement, { top: 50, left: 110, skewx: 20, shadow: shadow, }); // using cloneasimage method image.cloneasimage( function (img) { img.set(top, 150); img.set(left, 150); canvas.add(img); }, { withoutshadow: false, } ); </script></body></html>
以上就是fabricjs – 如何删除克隆图像中当前对象的阴影?的详细内容。