简体   繁体   English

fabric.js:将剪切的图像移到固定的剪切蒙版后面

[英]fabric.js: move clipped Image behind fixed clipping-mask

I'm working on a small script that lets a user load a custom image into the canvas on the webpage. 我正在研究一个小的脚本,该脚本可让用户将自定义图像加载到网页上的画布中。 So far that works pretty neat. 到目前为止,效果还不错。 The canvas is initialized using the fabric.js script in order to let the user do some easy editing tasks. 使用fabric.js脚本初始化画布是为了让用户执行一些简单的编辑任务。

The "uploaded" image is clipped by a simple rectangle. “上传的”图像被一个简单的矩形裁剪。 Now comes the tricky part: the user should then be able to move around, scale and rotate the image, whilst the rectangle stays in place; 现在到了棘手的部分:用户应该能够在矩形保持原位的同时移动,缩放和旋转图像。 selecting the image section preferred. 选择首选的图像部分。 However, even 但是,即使

lockMovement = true;

or 要么

lockMovementX = true;    
lockMovementY = true;    

do not keep that clipping mask in place. 不要将剪裁面罩固定在适当的位置。 Any other way to achieve this? 还有其他方法可以做到这一点吗?

Any help is greatly appreciated! 任何帮助是极大的赞赏! Please find a demo here: http://jsfiddle.net/efmbrm4v/ 请在此处找到演示: http : //jsfiddle.net/efmbrm4v/

I had the same problem and I solved it with following code: 我遇到了同样的问题,并使用以下代码解决了该问题:

image.clipTo = function (ctx) {
  ctx.save();

  ctx.setTransform(1, 0, 0, 1, 0, 0); // Reset transformation to default for canvas
  ctx.rect(
    100, 100, // Just x, y position starting from top left corner of canvas
    200, 200 // Width and height of clipping rect
  );

  ctx.restore();
};

You can try it out here: http://jsfiddle.net/Jagi/efmbrm4v/1/ 您可以在这里尝试: http : //jsfiddle.net/Jagi/efmbrm4v/1/

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

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