Skip to content

Instantly share code, notes, and snippets.

@audinue
Created April 23, 2022 19:02
Show Gist options
  • Save audinue/5962335665d83145927cd405769ab41a to your computer and use it in GitHub Desktop.
Save audinue/5962335665d83145927cd405769ab41a to your computer and use it in GitHub Desktop.
Resize fit/fill
/**
* @param {[number, number]} src
* @param {[number, number]} max
* @return {[number, number]}
*/
function fit (src, max) {
var ratio = Math.min(max[0] / src[0], max[1] / src[1])
return [src[0] * ratio, src[1] * ratio]
}
/**
* @param {[number, number]} src
* @param {[number, number]} max
* @return {[number, number]}
*/
function fill (src, max) {
var ratio = Math.max(max[0] / src[0], max[1] / src[1])
return [src[0] * ratio, src[1] * ratio]
}
/**
* @param {[number, number]} obj (width, height)
* @param {[number, number]} container (width, height)
* @return {[number, number]} (x, y)
*/
function center (obj, container) {
return [container[0] / 2 - obj[0] / 2, container[1] / 2 - obj[1] / 2]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment