Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gastonambrogi/13bb3c1cfa15dcaa2bfd036727b13692 to your computer and use it in GitHub Desktop.
Save gastonambrogi/13bb3c1cfa15dcaa2bfd036727b13692 to your computer and use it in GitHub Desktop.
function getZoomCompensatedBoundingRect(fabricObject){
//fabricObject is the object you want to get the boundingRect from
fabricObject.setCoords();
var boundingRect = fabricObject.getBoundingRect();
var zoom = canvas.getZoom();
var viewportMatrix = canvas.viewportTransform;
//there is a bug in fabric that causes bounding rects to not be transformed by viewport matrix
//this code should compensate for the bug for now
boundingRect.top = (boundingRect.top - viewportMatrix[5]) / zoom;
boundingRect.left = (boundingRect.left - viewportMatrix[4]) / zoom;
boundingRect.width /= zoom;
boundingRect.height /= zoom;
return boundingRect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment