Top-Left coordinates not updated on rotating image using fabricjs

老子叫甜甜 提交于 2019-12-02 12:27:38

As you can see from the snippet below, if you only rotate your object, only the bounding box will be updated, you have to move your object to have the position of your object updated.

var canvas = this.__canvas = new fabric.Canvas('c');
var rect = new fabric.Rect({
  left: 120,
  top: 30,
  width: 100,
  height: 100,
  fill: 'green',
  angle: 20
});
canvas.on("object:rotating", function() {
  var ao = canvas.getActiveObject();
  if(ao){
    console.log('top and left are the same after rotation');
    console.log('top:' + ao.top);
    console.log('left:' + ao.left);
    
    console.log('but not the bounding box');
    var bound = ao.getBoundingRect();
    console.log('bounding box - top:' + bound.top);
    console.log('bounding box - left:' + bound.left);

  }
})
canvas.add(rect);
canvas.renderAll();
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<script src="https://seikichi.github.io/tmp/PDFJS.0.8.715/pdf.min.js"></script>
<canvas id="c" style="border:1px solid black"></canvas>

I guess you are looking for something like this:

canvas.on('object:rotating', function(options) {
  options.target.setCoords();
  var left = options.target.oCoords.tl.x,
      top = options.target.oCoords.tl.y;
  console.log(left, top);
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!