Rotation and Scaling controls are off after using setAngle() in Fabric.js

前端 未结 2 1273
悲哀的现实
悲哀的现实 2021-01-15 22:44

I want to let users rotate objects on the Fabric.js powered canvas, but limit their rotation to 90 deg increments. Idea is that as they rotate and then stop, the object woul

相关标签:
2条回答
  • 2021-01-15 23:15

    Run setCoords() after setAngle(). This will update the position of the controls. Tried it in your fiddle. Works fine.

       canvas.on("object:modified", function(modEvtData) {
         // modified fires after object has been rotated 
         var modifiedObj = modEvtData.target;
         if (modifiedObj.angle && snapAfterRotate) {
             modifiedObj.setAngle(lastClosestAngle).setCoords();
             snapAfterRotate = false;
             canvas.renderAll();
         }
       })
    

    regards, Benick

    0 讨论(0)
  • 2021-01-15 23:30

    FabricJS now supports this natively through 'setAngle', along with 'snapThreshold' property.

    https://github.com/kangax/fabric.js/pull/3383

    Example:

    canvas.add(new fabric.Circle({
      snapAngle: 90,
      snapThreshold: 45,
      left: 100, 
      top: 100, 
      radius:50,
      fill: '#9f9', 
      centeredRotation: true
    }));
    

    https://jsfiddle.net/2t2d1zj9/1/

    0 讨论(0)
提交回复
热议问题