Multiple clipping areas on Fabric.js canvas

前端 未结 7 1509
一生所求
一生所求 2020-11-28 04:26

For making Photo Collage Maker, I use fabric js which has an object-based clipping feature. This feature is great but the image inside that clipping region cannot be scaled,

相关标签:
7条回答
  • 2020-11-28 05:20

    Update to @Promlnc answer.

    You need to replace the order of context transformations in order to perform proper clipping.

    1. translation
    2. scaling
    3. rotation

    Otherwise, you will get wrongly clipped object - when you scale without keeping aspect ratio (changing only one dimension).

    Code (69-72):

    ctx.translate( ctxLeft, ctxTop );
    
    ctx.rotate(degToRad(this.angle * -1));
    ctx.scale(scaleXTo1, scaleYTo1);
    

    Replace to:

    ctx.translate( ctxLeft, ctxTop );
    ctx.scale(scaleXTo1, scaleYTo1);
    ctx.rotate(degToRad(this.angle * -1));
    

    See this: https://jsfiddle.net/ZxYCP/185/

    Proper result:

    UPDATE 1:

    I have developed a feature to clip by polygon: https://jsfiddle.net/ZxYCP/198/

    0 讨论(0)
提交回复
热议问题