How to get the canvas-relative position of an object that is in a group?

前端 未结 4 1951
野的像风
野的像风 2020-11-30 11:36

Normally an object\'s position relative to the canvas can be gotten from it\'s .left and .top attributes, but these become relative to the group if

4条回答
  •  一生所求
    2020-11-30 12:02

    Had to solve this issue with another approach... I had to find images in JSON canvas data, so my code is:

    let findItemByType = function(source, type, rX, rY)
    {
        rX = rX ? rX : 0;
        rY = rY ? rY : 0;
        let objs = [];
        source.forEach(function(item){
            if (item.type === type){
                item.rX = rX + item.left;
                item.rY = rY + item.top;
                objs.push(item);
            }
            else {
                if (item.objects)
                {
                    item.rX = rX;
                    item.rY = rY;
                    let subresult = findItemByType(item.objects, 
                    type, 
                    item.rX + item.left + item.width / 2, 
                    item.rY + item.top + item.height / 2);
                    if (subresult){
                        objs = objs.concat(subresult);
                    }
                }   
            }
        });
        return objs;
    }
    let images = findItemByType(canvas.toJSON().objects, "image", 0, 0);
    

    This code should bring an array of all image objects and they should have rX and rY attributes which is relative positions of images.

提交回复
热议问题