Search code examples
html5-canvastransformer-modelkonvajs

Konva Transformer restrict shape size


I don't think it's possible currently to restrict the size of a shape when using Transformer to make it bigger or smaller but I really need to add this capability.

What is the best way to go about doing this ourselves (giving the size of shapes min/max which Transformer will honour) ?

Thanks.


Solution

  • You can do something like this:

    var MAX_WIDTH = 200;
    rect.on('transform', function() {
      var width = rect.width() * rect.scaleX();
      if (width > MAX_WIDTH) {
        var scaleX = MAX_WIDTH / rect.width();
        rect.scaleX(scaleX);
      }
    })
    

    Demo: http://jsbin.com/xowihazaza/2/edit?html,js,output