javascriptrotationlimitfabricjs

fabric.js limit rotation to x degrees with rotation handle


I am using fabric.js and trying to allow rotation for any object on my canvas, not freely 360 degrees, but only 15 degrees at a time, using the rotation handle. I searched really hard but couldn't find an answer so far. Is this possible?


Solution

  • Shorter solution:

    canvas.on('object:rotating', function(options) {
      var step = 15;
      options.target.angle = Math.round(options.target.angle / step) * step;
    });
    

    Since 1.6.7 you can just use fabric.Object.snapAngle property:

    someFabricObject.snapAngle = 15;