Fabric.js Clip Canvas (or object group) To Polygon

后端 未结 2 796
难免孤独
难免孤独 2020-12-03 06:03

I have a canvas drawn in Fabric.js that i am adding a group of rectangles to, i want to limit the edges of those rectangles as a group to not go outside a certain area.

相关标签:
2条回答
  • 2020-12-03 06:42

    You can just render a shape inside canvas.clipTo :)

    I just loaded a random SVG shape in kitchensink and did this:

    var shape = canvas.item(0);
    canvas.remove(shape);
    canvas.clipTo = function(ctx) {
      shape.render(ctx);
    };
    

    canvas clipped to a shape

    As you can see, entire canvas is now clipped by that SVG shape.

    0 讨论(0)
  • 2020-12-03 06:52

    You may also try this one: http://jsfiddle.net/ZxYCP/198/

    var clipPoly = new fabric.Polygon([
        { x: 180, y: 10 },
        { x: 300, y: 50 },
        { x: 300, y: 180 },
        { x: 180, y: 220 }
    ], {
        originX: 'left',
        originY: 'top',
        left: 180,
        top: 10,
        width: 200,
        height: 200,
        fill: '#DDD', /* use transparent for no fill */
        strokeWidth: 0,
        selectable: false
    });
    

    You can simply use Polygon to clip. Answer is based on @natchiketa idea in this question Multiple clipping areas on Fabric.js canvas

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