Get height and width of Fabric.js object after modifying wrong

社会主义新天地 提交于 2019-12-11 07:58:32

问题


Here is my code.

<canvas id="c" width="240" height="300" style="border:1px solid #000000"></canvas>

<script>
    var canvas = new fabric.Canvas('c');
     canvas.backgroundColor = 'yellow';


     canvas.renderAll();

     fabric.Image.fromURL(<?php echo '"' . $url . '"'; ?>, function(myImg) {
      var img1 = myImg.set({ left: 0, top: 0 ,width:150,height:150});
      canvas.add(img1); 
     });

     function objectMovedListener(ev) {
      let target = ev.target;
      console.log('left', target.left, 'top', target.top, 'width', target.width * target.scaleX, 'height', target.height * target.scaleY);
     }

    canvas.on('object:modified', objectMovedListener);
</script>

I don't understand what's going wrong. object position TOP & LEFT is showing correct but height & width is shoving wrong.

Please help me.


回答1:


Use getScaledWidth() and getScaledHeight() to get transformed width and height of an object respectively.




回答2:


Check target.scaleX and target.scaleY. If are different than 1 you need to multiply the target.width and target.height with scale



来源:https://stackoverflow.com/questions/52985469/get-height-and-width-of-fabric-js-object-after-modifying-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!