Maths/Javascript – How to calculate thumbnail image size to fit in fixed gallery area
Question:
Hi.
I am writing a media gallery javascript class. One function, if max thumbnail sizes are not specified, and the gallery area size is specified, is to calculate the size of thumbnail images so they fit the gallery area.
How do I do this please, given that the gallery area can be any width and height? The thumbnails will be square.. Currently I work it out like below, but this algorithm is flawed and works only on the width..:
1) Gallery.height x Gallery.width = Square pixels available (a)
2) (a) divided by the number of thumbnails gives square pixels max for each thumb (b)
3) Gallery.width divided by the square root of (b) gives exact number of thumbs across (c)
4) To ensure _whole_ thumbnails, round (c) up to nearest integer (d)
5) Gallery.width divided by (d) gives thumbnail width/height
Generally this works “ok”, but is not right, especially if the Gallery.height is much bigger than the Gallery.width.
Maths is not my strongest point, so I would appreciate any better ideas!!
Thanks, TheFoot
Solution:
I suck at math too…
var a = Gallery.width = pixels available across
var b = Gallery.height = pixels available down
var c = a times b = area of gallery in square pixels
var x = c divided by number of thumbnails = size of thumb in square pixels
var widthPerThumb = Math.round(square root of x)
No?













Comments (0)
Trackbacks - Pingbacks (0)
Leave a Reply