I’m currently working on web app for photo editing using FabricJS and one of features I need to implement is something like Clipping masks from Photoshop.
For example I have this assets: frame, mask and image. I need to insert image inside frame and clip it with mask. Most tricky part is in requirements:
- User should be able to modify image inside frame, e.g. move, rotate, skew… Frame itself also can be moved inside canvas.
- Number of layers is not limited so user can add objects under or above masked image.
- Masks, frames and images is not predefined, user should be able to upload and use new assets.
My current solution is this:
- Load assets
- Set
globalCompositeOperation
of image tosource-out
- Set
clipTo
function for image. - Add assets on canvas as a group
In this solution clipTo
function preserve image inside rectangular area of frame and with help of globalCompositeOperation
I’m clipping image to actual mask. At first sight it works fine but if I add new layer above this newly added group it will be cutted off because of globalCompositeOperation="source-out"
rule. I’ve created JSFiddle to show this.
So, that else could I try? I’ve seen some posts on StackOverflow with advices to use SVGs for clipping mask, but if I understand it correctly SVG must contain only one path. This could be a problem because of third requirement of my app.
Any advice in right direction will help, because right now I’m totally stuck with this problem.
3
Answers
Not sure what you want.
If you want the last image loaded (named
img2
), the one you send to the back to not effect the layers above do the following.You have
mask
,frame
,img
, andimg2
;Put them in the following order and with the following comp settings.
If you want something else you will have to explain it in more detail.
Personally when I provide masking to the client I give them full access to all the composite methods and allow them to work out what they need to do to achieve a desired effect. Providing a UI that allows you to change the comp setting, and layer order makes it a lot easier to sort out the sometimes confusing canvas composite rules.
I’d suggest looking at this solution.
Multiple clipping areas on Fabric.js canvas
You end up with a shape layer that is used to define the mask shape. That shape then gets applied as a clipTo to your image.
The one limitation I can think off though that you might run into is when you start to rotate various shapes. I know I have it working great with a rectangle and a circle, however ran into some issues with polygons from what I recall… This was all setup under and older version of FabricJS however, so there may have been some improvements there that I’m not experienced with.
The other issue I ran into was drop shadows didn’t render correctly when passed to a NodeJS server running FabricJS.
You can do this by using ClipPath property of Img Object which you want to mask. With this, you can Mask Any Type of Object. and also you need to add some Ctx Configuration in ClipTo function of Img Object.
check this link https://jsfiddle.net/naimsajjad/8w7hye2v/8/