Skip to content

Instantly share code, notes, and snippets.

@ryan-digbil
Created November 5, 2014 02:46
Show Gist options
  • Save ryan-digbil/21b6b0fc9974b3ab29be to your computer and use it in GitHub Desktop.
Save ryan-digbil/21b6b0fc9974b3ab29be 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