Search code examples
javascriptcanvaskonvajs

How to get the actual width of a rotated canvas rectangle


I'm using konva js to make a transformable rectangle. I'm able to access the width of the rectangle using rect1.getClientRect().height . However, when I rotate the rectangle, the width property is changing. I believe width property gives the overall horizontal length of the container of the rectangle canvas. Is there any way I can get the actual width of the rotated rectangle. Here's the jsfiddle


Solution

  • The Konva-object has an attrs attribute: rect1.attrs.width * rect1.attrs.scaleX

    https://jsfiddle.net/8gack34s/4/

    there is also attrs.rotation that you can use to calculate it mathematically.

    Konva warning: Konva.Transformer is currently experimental and may have bugs. Please report any issues to GitHub repo.