简体   繁体   中英

Fabric.js Ungroup items change their positions

Hi I'm trying to ungroup items using fabric.js, but everytime I try to ungroup them their position changes on the canvas.

I tried many technique and even tried to manually recalculate the positions but I was always off.

What seams to be the best technique is listed on this post

https://gist.github.com/msievers/6069778

Since the _restoreObjectsState is called by the group.destroy(), I used this instead (both gave me the same results)

I have a jsfiddle that shows that shows the weird behavior.

http://jsfiddle.net/vq3Lj0th/

var canvas =  new fabric.Canvas('viewport');

var text1 = new fabric.IText('Text1', {
            left: 0,
            top: 0
});
var text2 = new fabric.IText('Text2', {
            left: 0,
            top: 125
});
var group = new fabric.Group([text1, text2], {
    left: 150,
    top: 100
});
canvas.add(group);

document.getElementById('ungroup').addEventListener('click' ,function ungroup() {

    var destroyedGroup = group.destroy();
    var items = destroyedGroup.getObjects();

    items.forEach(function (item) {
        canvas.add(item);
    });
    canvas.remove(group);
});

as a reference, what i'm trying to do is to ungroup the items and send them to another canvas to simulate group edition mode. i'm almost there but I can't figure how to ungroup things without losing their position.

Thanks for your help.

I found a way to make it work:

The bleeding edge version of fabric resolve this issue:

https://github.com/kangax/fabric.js/issues/1798

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM