Created
February 10, 2015 05:54
-
-
Save rodrigopolo/62c21b25a23d34fee6a0 to your computer and use it in GitHub Desktop.
Calc image resize dimensions in JS
This file contains 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
function calcResize(op){ | |
var ratio = Math[(op.fill)?'max':'min'](op.to.width/op.from.width,op.to.height/op.from.height); | |
var r = { | |
width: Math.round(op.from.width * ratio), | |
height: Math.round(op.from.height * ratio) | |
} | |
r.x = Math.round((op.to.width - r.width) / 2); | |
r.y = Math.round((op.to.height - r.height) / 2); | |
return r; | |
} | |
console.log(calcResize({ | |
from: { | |
width: 240, | |
height: 320 | |
}, | |
to: { | |
width: 320, | |
height: 240 | |
}, | |
fill: true | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment