Calculating height based on width and aspect ratio?

T

Tuxedo

How can a height value based on a width and an aspect ratio be calculated
in Javascript?

In a basic html situation, where the width of an object is known first, say
an html div by its ID scrollWidth property, how can the height of that div
(or some other object) be calculated and set by an aspect ratio?

For example, an 834 pixels wide x 246 pixels tall rectangle, or a 500
pixels wide x 147 pixels tall rectancle, both have an aspect ratio of
139:41, at least according to a nifty Aspect Ratio Calculator application
at: http://andrew.hedges.name/experiments/aspect_ratio

Only knowing the exact width to start with, the resulting height may be
rounded to the nearest pixel value of a given aspect ratio.

How can this best be calculated in Javascript?

Many thanks for any ideas!
Tuxedo
 
E

Evertjan.

Tuxedo wrote on 26 sep 2011 in comp.lang.javascript:
How can a height value based on a width and an aspect ratio be
calculated in Javascript?

In a basic html situation, where the width of an object is known
first, say an html div by its ID scrollWidth property, how can the
height of that div (or some other object) be calculated and set by an
aspect ratio?

For example, an 834 pixels wide x 246 pixels tall rectangle, or a 500
pixels wide x 147 pixels tall rectancle, both have an aspect ratio of
139:41, at least according to a nifty Aspect Ratio Calculator
application at: http://andrew.hedges.name/experiments/aspect_ratio

Only knowing the exact width to start with, the resulting height may
be rounded to the nearest pixel value of a given aspect ratio.

How can this best be calculated in Javascript?

Meseems this has nothing to do with Javascript specific,
as it is simple aritmetic, multiplication and division.

Why would you need an Aspect Ratio Calculator,
as that is simple aritmetic too,
just use the calculator on your pc, or equivalent.

Why would you want us to do your simple aritmetic?
 
S

Scott Sauyet

How can a height value based on a width and an aspect ratio be calculated
in Javascript?

height = Math.round(width / aspectRatio)
height = Math.round(width * aspectRatioDenominator /
aspectRatioNumerator);

// e.g. height = Math.round(500 * 41 / 139) // => 147

As Evertjan said, this is simple arithmetic.

-- Scott
 
T

Tuxedo

Evertjan. said:
Tuxedo wrote on 26 sep 2011 in comp.lang.javascript:


Meseems this has nothing to do with Javascript specific,
as it is simple aritmetic, multiplication and division.

Why would you need an Aspect Ratio Calculator,
as that is simple aritmetic too,
just use the calculator on your pc, or equivalent.

Why would you want us to do your simple aritmetic?

Yes, thanks for pointing this out. So indeed it is not a specific
Javascript question. I was not sure what builtin features may or may not
exist in Javascript for performing this particular type of simple
calculation. That said, do you happen to know the aritmetic formula for
finding out a height value, based on a known width and aspect ratio?

Many thanks,
Tuxedo
 
T

Tuxedo

Scott said:
height = Math.round(width / aspectRatio)
height = Math.round(width * aspectRatioDenominator /
aspectRatioNumerator);

// e.g. height = Math.round(500 * 41 / 139) // => 147

As Evertjan said, this is simple arithmetic.

-- Scott

Many thanks for posting this very simply example. It indeed works!

Tuxedo
 
E

Evertjan.

Tuxedo wrote on 26 sep 2011 in comp.lang.javascript:
Yes, thanks for pointing this out. So indeed it is not a specific
Javascript question. I was not sure what builtin features may or may
not exist in Javascript for performing this particular type of simple
calculation.

Sure:
* for multiplication
/ for division
That said, do you happen to know the aritmetic formula
for finding out a height value, based on a known width and aspect
ratio?

Indeed, I happen to know the aritmetic,
it is not a specific fomula,
there are many ways to do it.

Better you try to figure out how,
as this seems like you are asking me to do your schoolwork.
 
E

Evertjan.

Tuxedo wrote on 26 sep 2011 in comp.lang.javascript:
Many thanks for posting this very simply example. It indeed works!

How would you know it works [correctly]?
Just by the formula not throwing an error?

If you use the result in the DOM,
there would be no need for rounding,
the DOM taking care of that.
 
T

Tuxedo

Evertjan. said:
Tuxedo wrote on 26 sep 2011 in comp.lang.javascript:
Many thanks for posting this very simply example. It indeed works!

How would you know it works [correctly]?
Just by the formula not throwing an error?

I tested it and it returns the correct height for a given aspect ratio. It
is as you and Evertjan pointed out, simple arithmetic.
If you use the result in the DOM,
there would be no need for rounding,
the DOM taking care of that.

Good to know! However, I guess the Javascript can also take care of the
rounding, as DOM otherwise would. As far as I see, it works fine for my
simple purpose, with or without the DOM taking care of the rounding.

Thanks again for the example!
Tuxedo
 
P

P E Schoen

"Evertjan." wrote in message
Tuxedo wrote on 26 sep 2011 in comp.lang.javascript:
Better you try to figure out how,
as this seems like you are asking me to do your schoolwork.

Everything needed is in the JavaScript source of the calculator, but it's
probably more complicated than it needs to be. The author has included some
past comments others have submitted, and they are worth reading for laughs.
But it's sad because I'm sure there actually are people "out there" who are
so clueless and/or rude. For some, apparently, this is "higher math". One
said, "I don't understand ratios"!

Paul
 
T

Tuxedo

Evertjan. wrote:

[...]
Indeed, I happen to know the aritmetic,
it is not a specific fomula,
there are many ways to do it.

Better you try to figure out how,
as this seems like you are asking me to do your schoolwork.

Not at all, it was just a question I simply didn't know the answer to,
although perhaps too simple for some. But now I too figured one way how
this can easily be done in Javascript, partly so with your help.

Thanks,
Tuxedu
 
S

Swifty

this seems like you are asking me to do your schoolwork.

Remember, there are probably more people over 60 than there are people
doing "schoolwork", and we also need to ask simple questions from time
to time. :)

Still, I enjoy it when I get mistaken for a schoolboy; it's not
something that happens in real life.
 
T

Thomas 'PointedEars' Lahn

Swifty said:
"Evertjan.":

Remember, there are probably more people over 60 than there are people
doing "schoolwork", and we also need to ask simple questions from time
to time. :)

Still, I enjoy it when I get mistaken for a schoolboy; it's not
something that happens in real life.

Probably "homework" was meant. You should remember that the necessity of
"doing one's homework" – figuratively – applies to people with a job as
well, no matter their age.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top