I am trying to implement image mask as example below :
Example image here: https://s3-ap-northeast-1.amazonaws.com/utonly/demo/demo.jpg
I have tried so many ways and took a lot of time, but still can't find a good solution for it. The image must be able to resizing and rotating inside the mask, or edit from another view, and then apply the result back to the image.
I found similar example here http://jsfiddle.net/tbqrn/68/ , but I don't want images pass through each other. Is that possible? Or do I have any chance to try other ways to do this?
var img01URL = 'http://fabricjs.com/assets/printio.png';
var img02URL = 'http://fabricjs.com/lib/pug.jpg';
var img03URL = 'http://fabricjs.com/assets/ladybug.png';
var img03URL = 'http://fabricjs.com/assets/ladybug.png';
var canvas = new fabric.Canvas('c');
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.rect(10,10,150,150);
ctx.rect(170,10,200,200);
ctx.rect(10,170,150,150);
ctx.closePath();
ctx.stroke();
ctx.clip();
fabric.Image.fromURL(img01URL, function(oImg) {
oImg.scale(.25);
oImg.left = 10;
oImg.top = 10;
canvas.add(oImg);
canvas.renderAll();
});
fabric.Image.fromURL(img02URL, function(oImg) {
oImg.scale(.40);
oImg.left = 180;
oImg.top = 0;
canvas.add(oImg);
canvas.renderAll();
});
fabric.Image.fromURL(img03URL, function(oImg) {
oImg.left = 10;
oImg.top = 170;
canvas.add(oImg);
canvas.renderAll();
});
Thanks for the help.
TO EXPLAIN BETTER THIS ANSWER: This code is not in fabricJS, clipPath is not a fabricJS property ( even if i want it to be soon ). This solution is a little bit better than using clipTo function ( that is deprecated in fabric 2.0 ) because it allows you to use standard fabric shapes for clipping. The idea is attach a standard shape to another defining it as clipPath. the clipPath then can be fixed (like if it was attached to canvas) or move with the image. This idea has to be better explored yet.
There are many way to accomplish this. Those are no one liner solutions and require custom coding. This is a solution using a clipPath attached to image. It requires to modify the main Image
_render
method.