简体   繁体   English

在fabric.js中将组用作遮罩

[英]Use a group as a mask in fabric.js

I want to create a group that is 300px wide and 200px high, and then load a few things inside that group. 我想创建一个宽度为300px且高度为200px的组,然后在该组中加载一些内容。 When I load images in that are larger than the group dimensions, it bleeds outside the group. 当我加载的图像大于组尺寸时,它在组外出血。 I'd love to "crop" the image (similar to a CSS overflow:hidden property). 我想“裁剪”图像(类似于CSS溢出:隐藏属性)。

Is this possible? 这可能吗?

To accomplish your task you should use the clipTo function on your image, the clipTo function on a group already has an open bug , btw you can work around there, by transpose the dimension and the position of your group to clipTo function: 要完成你应该用你的任务clipTo您的图像功能, clipTo一组功能已经有一个开放的错误 ,顺便说一句,你可以解决那里,调换尺寸和您的团队clipTo功能的位置:

clipTo :Function § Function that determines clipping of an object (context is passed as a first argument) Note that context origin is at the object's center point (not left/top corner) clipTo:Function§确定对象裁剪的函数(上下文作为第一个参数传递)请注意,上下文原点位于对象的中心点(而不是左/上角)

Take a look to official demo , then after the clip operation on your image you can add it to a group(run below script to see an example). 看一下官方演示 ,然后在对图像进行剪辑操作之后,可以将其添加到组中(在脚本下面运行以查看示例)。

 var canvas = window.__canvas = new fabric.Canvas('c'); var path = 'http://fabricjs.com/lib/pug.jpg'; var _img = new Image(); _img.onload = function(img) { var dog = new fabric.Image(_img, { left: 100, top: 100, width: 300, height: 300, selectable: false, clipName: 'dog', clipTo: function(ctx) { ctx.rect(0, 0, 50, 50); } }); var group = new fabric.Group([dog], { left: 100, top: 100, width: 100, height: 100, borderColor: 'black', }); canvas.add(group); }; _img.src = path; canvas.renderAll(); 
 <script src="//cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.13/fabric.min.js"></script> <canvas id="c" height="300" width="300" style="border:1px dashed #333;"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM