Raggaer
Raggaer

Reputation: 3318

Fabricjs move object to layer

I am trying to move items to a different layer (so I can order them)

for (var x = 0; x < resp.Items.length; x++) {
    $('#inv' + resp.Items[x].Type).attr('src', '/public/items/universe/' + resp.Items[x].Type + '/' + resp.Items[x].Name + '.png');
    fabric.Image.fromURL('/public/items/universe/' + resp.Items[x].Type + '/' + resp.Items[x].Name + '.png', function(obj) {
        inventory.add(obj);
        obj.moveTo(obj.priority)
        obj.selectable = false;
        inventory.renderAll();
        console.log(inventory.getObjects().indexOf(obj), obj.priority)
    }, {
        id: resp.Items[x].Name,
        creator: resp.Items[x].Creator.Username,
        priority: resp.Items[x].Priority,
        category: resp.Items[x].Type
    });
}

I am trying to use moveTo method with no success at all. the console.log call shows always 0, 1, 2, 3... instead of the moveTo index

I get no errors and item.priority exists so... I am missing something I guess

Upvotes: 1

Views: 1884

Answers (1)

StefanHayden
StefanHayden

Reputation: 3669

moveTo() is only rearranging objects in an array. You shouldn't think of them as layers that can have any Id as much as objects in an array. So you can't move something to an index of 10 unless there are 11 objects.

A better option might be to load all objects first and sort them based on priority and then add them to the canvas based on that order.

Upvotes: 2

Related Questions