CodeInSpace
CodeInSpace

Reputation: 23

fabricjs Polygon Redundant Area

2https://i.sstatic.net/2aZm2.png

1https://codepen.io/1gelistirici/pen/KKRjgoj[enter image description here]

 // Initiate a Canvas instance
        var canvas = new fabric.Canvas("canvas");
  
        // Initiate a polygon instance
        var polygon = new fabric.Polygon([
        { x: 200, y: 10 },
        { x: 250, y: 50 },
        { x: 250, y: 180},
        { x: 150, y: 180},
        { x: 150, y: 50 }], {
            fill: 'green'
        });
  
        // Render the polygon in canvas
        canvas.add(polygon);

Hi, I have created a polygon in fabricjs. Then I check your intersect. When the other object comes to a non-polygon but defined as a polygon by fabricjs, it is detected that it hits. How can I get ahead of this? Can you help me?

Upvotes: 0

Views: 110

Answers (1)

Curtis Rock
Curtis Rock

Reputation: 1

Check for intersection using object.containsPoint(). You can define the point as anywhere associated with the moving object coordinates.

var targetPoint = new fabric.Point(moveEventTarget.left, moveEventTarget.top);
// detect if move event target intersects with another object
if (intersectionObject.containsPoint(targetPoint)) {
    if (!canvas.isTargetTransparent(intersectionObject, targetPoint.x, targetPoint.y)) {
        // do something
    };
};

Upvotes: 0

Related Questions