画布的缩放值不为1时选择边框不正确

时间:2018-08-31 05:51:29

标签: javascript canvas fabricjs

版本2.3.6

复制步骤:

  1. 单击“全选”,选择就可以了。
  2. 再次单击“将缩放设置为1.5”,
  3. 然后单击“全选”,

结果:选择边框现在不正确。

$(document).ready(function() {
  var canvas = new fabric.Canvas('canvas');
  canvas.add(new fabric.Rect({
    left: 50,
    top: 50,
    width: 75,
    height: 50,
    fill: 'green',
    stroke: 'black',
    strokeWidth: 3,
    padding: 10
  }));

  canvas.add(new fabric.Circle({
    left: 120,
    top: 120,
    radius: 30,
    fill: 'gray',
    stroke: 'black',
    strokeWidth: 3
  }));


  $("#select-all").click(function() {
    canvas.discardActiveObject();
    var elements = canvas.getObjects();
    var selection = new fabric.ActiveSelection(elements);
    canvas.setActiveObject(selection);
    canvas.requestRenderAll();
  });

  $("#set-zoom-15").click(function() {
    canvas.setZoom(1.2);
  });
  $("#set-zoom-10").click(function() {
    canvas.setZoom(1.0);
  });
  canvas.renderAll();
});
canvas {
  border: 1px solid #ccc;
  margin-top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.6/fabric.min.js"></script>


<input id="select-all" type="button" value="select all" />
<input id="set-zoom-15" type="button" value="set zoom as 1.5" />
<input id="set-zoom-10" type="button" value="set zoom as 1" />
<canvas id="canvas" width="400" height="400" style="border:1px solid red" />

预期的行为:
缩放为1.5时,选择效果很好(例如缩放为1)。

实际行为
缩放为1.5时选择不正确。

1 个答案:

答案 0 :(得分:2)

创建ActiveSelection时,您需要传递canvas对象。

var selection = new fabric.ActiveSelection(elements, {
   canvas: canvas,
});

有关文档,请参见http://fabricjs.com/manage-selection

$(document).ready(function() {
  var canvas = new fabric.Canvas('canvas');
  canvas.add(new fabric.Rect({
    left: 50,
    top: 50,
    width: 75,
    height: 50,
    fill: 'green',
    stroke: 'black',
    strokeWidth: 3,
    padding: 10
  }));

  canvas.add(new fabric.Circle({
    left: 120,
    top: 120,
    radius: 30,
    fill: 'gray',
    stroke: 'black',
    strokeWidth: 3
  }));


  $("#select-all").click(function() {
    canvas.discardActiveObject();
    var elements = canvas.getObjects();
    var selection = new fabric.ActiveSelection(elements, {
      canvas: canvas,
    });
    canvas.setActiveObject(selection);
    canvas.requestRenderAll();
  });

  $("#set-zoom-15").click(function() {
    canvas.setZoom(1.2);
  });
  $("#set-zoom-10").click(function() {
    canvas.setZoom(1.0);
  });
  canvas.renderAll();
});
canvas {
  border: 1px solid #ccc;
  margin-top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.6/fabric.min.js"></script>


<input id="select-all" type="button" value="select all" />
<input id="set-zoom-15" type="button" value="set zoom as 1.5" />
<input id="set-zoom-10" type="button" value="set zoom as 1" />
<canvas id="canvas" width="400" height="400" style="border:1px solid red" />