FabricJS - disable layer index changing when object is selected
Asked Answered
L

1

13

I've a problem with my FabricJS app. By default, the object layer in fabricjs jumps to the top when I select it.

I want to disable this option so that the index of the active element not changing. It's possible ?

Lennox answered 25/8, 2016 at 13:42 Comment(0)
P
21

You just need to set the preserveObjectStacking options as shown in the below code when setting up the canvas.

var fabricCanvas = new fabric.Canvas("t", { preserveObjectStacking: true });

fabricCanvas
  .add(new fabric.Rect({
    top: 0,
    left: 0,
    width: 100,
    height: 100,
    fill: "green"
  }))
  .add(new fabric.Rect({
    top: 50,
    left: 50,
    width: 100,
    height: 100,
    fill: "red"
  }))
  .add(new fabric.Rect({
    top: 100,
    left: 100,
    width: 100,
    height: 100,
    fill: "blue"
  }))
  .renderAll();
canvas {
  border: 1px solid black;
}
<canvas id="t" width="400" height="300"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.6.3/fabric.min.js"></script>
Presidentship answered 25/8, 2016 at 15:49 Comment(1)
This option I was looking for. Thank You very much !Lennox

© 2022 - 2024 — McMap. All rights reserved.