Created
April 23, 2022 19:02
-
-
Save audinue/5962335665d83145927cd405769ab41a to your computer and use it in GitHub Desktop.
Resize fit/fill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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