Fabricjs intersectsWithObject returning false when Object is fabric.Rect

扶醉桌前 提交于 2019-12-04 05:29:26

I found that by explicitly calling myObj.setCoords() prior to calling myObj.intersectsWithObject(otherObj) fixes the issue.

I updated the fiddle here http://jsfiddle.net/cyberpantz/9MkYJ/29/

It appears that fabric.Rect coordinates are not updated automatically while it is being moved while fabric.Group coordinates are, although I could be off-base here...

Updated (and simplified) code

cvs.observe('object:moving', function(e) {

    var targ = e.target;

    // this fixes it
    targ.setCoords();

    var items = cvs.getObjects().filter(function(o){
        return targ !== o;
    });

    var hit = false;

    for (var i = 0, n = items.length; i < n; i++) {
        var m = items[i];

        if (targ.intersectsWithObject(m)) {
            targ.setFill("red");
            hit = true;
        } 
        else {
            if (!hit) {
                targ.setFill("#CCCCCC");
            }
        }
    }
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!