控制Fabric.js中的z-index

时间:2013-01-03 00:05:06

标签: z-index fabricjs

在fabricjs中,我想创建一个场景,其中鼠标下的对象在z-index中上升到场景的顶部,然后一旦鼠标离开该对象,它就会返回到它所来自的z-index从。一个人不能设置object.zindex(这会很好)。相反,我使用的占位符对象放在旧位置的对象列表中,然后使用canvas.insertAt将旧对象放回到列表中的位置。但是这不起作用。

请参阅http://jsfiddle.net/rFSEV/了解此状态。

var canvasS = new fabric.Canvas('canvasS', { renderOnAddition: false, hoverCursor: 'pointer', selection: false });
var bars = {}; //storage for bars (bar number indexed by group object)
var selectedBar = null; //selected bar (group object)
var placeholder = new fabric.Text("XXXXX", { fontSize: 12 });

//pass null or a bar
function selectBar(bar) {
    if (selectedBar) {
        //remove the old topmost bar and put it back in the right zindex
        //PROBLEM: It doesn't go back; it stays at the same zindex
        selectedBar.remove();
        canvasS.insertAt(selectedBar, selectedBar.XZIndex, true);
        selectedBar = null;
        }
    if (bar) {
        //put a placeholder object ("XXX" for now) in the position
        //where the bar was, and put the bar in the top position
        //so it shows topmost
        selectedBar = bar;
        canvasS.insertAt(placeholder, selectedBar.XZIndex, true);
        canvasS.add(bar);
        canvasS.renderAll();
        }
    }

canvasS.on({
     'mouse:move': function(e) {
        //hook up dynamic zorder
        if (!e.target) return;
        if (bars[e.target])
            selectBar(e.target);
        else
            selectBar(null);
        },
    });

var objcount = canvasS.getObjects().length;

//create bars
for (var i = 0; i < 20; ++i) {
    var rect = new fabric.Rect({
      left: 0,
      top: 0,
      rx: 3,
      ry: 3,
      stroke: 'red',
      width: 200,
      height: 25
    });
    rect.setGradientFill({
      x1: 0,
      y1: 0,
      x2: 0,
      y2: rect.height,
      colorStops: {
        0: '#080',
        1: '#fff'
      }
    });    
    var text = new fabric.Text("Bar number " + (i+1), {
        fontSize: 12
    });
    var group = new fabric.Group([ rect, text ], {
      left: i + 101,
      top: i * 4 + 26
    });
    group.hasControls = group.hasBorders = false;

    //our properties (not part of fabric)
    group.XBar = rect;
    group.XZIndex = objcount++;

    canvasS.add(group);
    bars[group] = i;
    }
canvasS.renderAll();

5 个答案:

答案 0 :(得分:29)

由于fabric.js版本1.1.4,zIndex操作的新方法可用:

canvas.moveTo(object, index);
object.moveTo(index);

我认为这对您的用例很有帮助。我已经更新了你的jsfiddle - 我希望这是你想要的:
jsfiddle

答案 1 :(得分:7)

还要确保在将对象添加到画布后更改z-index。

所以代码看起来像:

canvas.add(object);
canvas.moveTo(object, index);

否则fabricjs不关心你设置的z索引。

答案 2 :(得分:0)

您可以修改_chooseObjectsToRender方法,使其在方法末尾进行以下更改,并且可以实现css样式的zIndex ing。

objsToRender = objsToRender.sort(function(a, b) {
  var sortValue = 0, az = a.zIndex || 0, bz = b.zIndex || 0;

  if (az < bz) {
    sortValue = -1;
  }
  else if (az > bz) {
    sortValue = 1;
  }

  return sortValue;
});

https://github.com/fabricjs/fabric.js/pull/5088/files

答案 3 :(得分:0)

添加线对象后,使用以下命令使线出现在对象下方:

canvas.add(line);
canvas.sendToBack(line);

其他选项是

  • canvas.sendBackwards
  • canvas.sendToBack
  • canvas.bringForward
  • canvas.bringToFront

请参阅:https://github.com/fabricjs/fabric.js/issues/135

答案 4 :(得分:0)

您可以使用这两个函数来获取结构对象的z-index并修改对象的z-index,因为没有通过对象索引修改z-index的特定方法:

fabric.Object.prototype.getZIndex = function() {
    return this.canvas.getObjects().indexOf(this);
}

fabric.Canvas.prototype.moveToLayer = function(object,position) {
    while(object.getZIndex() > position) {
        this.sendBackwards(object);
    }
}