Skip to content

Instantly share code, notes, and snippets.

@audinue
Created November 27, 2022 01:25
Show Gist options
  • Save audinue/397e0bbbf0d084600b862cb3b15ad0e8 to your computer and use it in GitHub Desktop.
Save audinue/397e0bbbf0d084600b862cb3b15ad0e8 to your computer and use it in GitHub Desktop.
Scale to fit
function scaleToFit (
contentWidth,
contentHeight,
containerWidth,
containerHeight
) {
var scale = Math.min(containerWidth / contentWidth, containerHeight / contentHeight)
return {
x: containerWidth / 2 - contentWidth * scale / 2,
y: containerHeight / 2 - contentHeight * scale / 2,
scale: scale
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment