[英]Fabric js clip object to background image
I am working on fabric js and I have encountered a problem where i have to clip user added object (text, image, svg etc) to background image set in canvas. 我正在研究结构js,我遇到了一个问题,我必须将用户添加的对象(文本,图像,svg等)剪辑到画布中设置的背景图像。 The background image could be png or svg(with multiple path).
背景图像可以是png或svg(具有多个路径)。 The background image is of non-uniform shape, such as leaf, shirt etc.
背景图像的形状不均匀,如叶子,衬衫等。
How could it be done? 怎么可能呢?
var canvas = new fabric.Canvas('canvas'); fabric.Image.fromURL('https://upload.wikimedia.org/wikipedia/commons/a/a2/Litchi_chinensis_leaf.png', function(img){ img.scale(0.3); canvas.setBackgroundImage(img); canvas.renderAll(); } ); var rect = new fabric.Rect({ width:80, height:100, fill:'red', globalCompositeOperation: 'source-atop', top: 100 }); canvas.add(rect); var circle = new fabric.Circle({ radius:80, height:100, fill:'blue', globalCompositeOperation: 'source-atop' }); canvas.add(circle); circle.center().setCoords();
<script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id='canvas' width="500" height="400" style="border:#000 1px solid;"></canvas>
This snippet shows you what is doable. 这个片段向您展示了什么是可行的。 Is not easy to clip with an "image".
用“图像”剪辑不容易。 What you can do is use globalCompositeOperation as i show you.
你可以做的就是使用globalCompositeOperation。
As you see rect and circle are visible only over the background and not outside. 如您所见,矩形和圆形仅在背景上可见,而不在外面。
If you want using another value other than "source-atop" you can obtain different effects. 如果你想使用“source-atop”以外的其他值,你可以获得不同的效果。 With "source-in" the background will be transparent and will serve the only porpouse of "clipping" the area
通过“source-in”,背景将是透明的,并将成为“剪裁”该区域的唯一场所
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.