Let's say I have an image that is 1920 x 1280.
I'm using a slider to scale down the image, which goes from 100% to 0%, where 100% is the original size and 0% is the minimum size.
The image can be no smaller than 350 in either height, or width, so 0% should equate to this minimum size.
What formula would I use to calculate the new dimensions? If you could explain the logic, that would be helpful.
I'm certain this is an SAT question that I failed...
The method below will re-size an image with a percentage,
0%
being a minimum value, and the image keeping aspect ratio:The best approach would be to create a
function
, which takes the valuesx
,y
,percentage
andminimum
. Next, create a variable calledratio
which isy
/x
(explained below). Then multiplyx
(minusminimum
, to decrease the range, and therefore make0%
equal350
) by(percentage/100)
andy
(minusminimum
timesratio
, to create a different range fory
, and therefore maintain aspect ratio) by(percentage/100)
, and return anarray
with the re-sizedx
andy
, each plusminimum
(andy
plusminimum
timesratio
to compensate for what we did) so0%
is350
and233.333
, while100%
is1920
and1280
. If the height is larger than the width, the ratio will bey
/x
instad ofx
/y
, andminimum
will be multiplied byratio
forx
, instead ofy
. Have a look at this code:– and run it like this:
Have a look at this working example, using
HTML
,JavaScript
andjQuery
(drag the slider above the image to re-size it):Hope this helps! :)
Percentage between 0 and 1 (can be altered to be 0 - 100 integers instead of 0 to 1 floats). Other words are describing themselves. Maybe useful as a function:
No math is needed for this. Css is already capable of these things.
ok. to calculate the image resizing you can use the following calculation: Max width: 1920 Max height: 1280
width Min: ? height Min: 350
1280 - 350 = 930 930/100 = 9.3 1% equals 9.3px
1920 - 1280 = 640 350 + 640 = 990
new minimum width = 990
1920 - 990 = 930 930/100 = 9.3
1% equal to 9.3
then you will have to use the 9.3 pixel value for every 1%
if you consider that 350 pixel are the minimum values for both dimensions, your image will look strange
After some tweaking and looking through the provided answers, here's some working code that will adjust for differences in height, width, and aspect ratios. With use of some
data
attributes, this is a good start to feed from, and if you are using a different form of communicating the data, just replace variables as needed. First, theHTML
:Simple enough, and when testing, change the
src
attribute of theimg
tag to see how the code handles different sizes. Now the fun part:Few quick pointers, make sure to wrap this in an
onload
or better yet,DomContentLoaded
method like:And keep in mind the
range
type ofinput
isn't supported in older IE, some maybe somehidden
inputs instead? Also, instead ofinput
in theslider
event, it has to bechange
in IE 10 so keep that in mind for cross browser-ness.Breakdown of the code, yeah? Alright, first we declare some variables, where
id
is just to grab by ID, grab the elements, have some more 'global' like variables (d_w
,d_h
, andmin
) whered_w
isdefault width
andd_h
is, yep,default height
. ThereSize()
function grabs the data assigned to the image and uses it for calculations and determining which side is longer, followed bys_h
ands_w
which uses the step to adjust the pixels, some aspect ratio equations, and finally hit thewidth
andheight
of the element with CSS.slider.addEventListener
does a few simple things, like update the percent and run thereSize()
function when it moves. Again, make sure to check for IE 10 and replaceinput
withchange
if you want this to work on that... 'browser'.Finally, the
img
event grabs the defaultwidth
andheight
, assigns them to said variables (d_w
andd_h
), checks which side is longer, and with the help ofPaulo
, calculates the step for each percent based on the offset of the actual size vs the minimum. I tested this on Chrome, FF, and IE 10+ and it's working fine. It'll work on IE 9>, but you'll have to useattachEvent
and norange
element. Hope this helps.EDIT
I put the
slider
event within theimg
load event to prevent premature firing of the code. I could tweak this more and even make it a stand-alone 'class' (well, kinda, JS classes are weird) if you like, let me know and I'd take a swing at it.