Tuesday, February 7, 2023

Scale rect to fit, including upscaling

From yesterday's post, I forgot the code snippet couldn't upscale, only downscale. This remedies that.
Same downscaling part, but afterwards, if it wasn't downscaled, upscale to the nearest dimension. Basically checking the scale factor for current width -> desired width vs. current height -> desired height. Then we're choosing the smaller ratio, as that would prevent any part of the rect from going outside the bounds.

JsFiddle:

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...