Lets break it down into pieces, shall we?
function presize($obj, w, h)
{
Accept a jQuery object ($obj) with a resize width and height. How do I know it requires a jQuery object? Functions width()
and height()
aren't native for dom elements.
var nw = $obj.width(),
nh = $obj.height();
Create two new variables corresponding to object's current width and height.
if ((nw > w) && w > 0) {
nw = w;
nh = (w / $obj.width()) * $obj.height();
}
If object's current width is greater than resize width (and assuming resize width has some meaningful value), nw
is reassigned to resize width w
, and nh
is assigned to a height proportionate to width resize (w / $obj.width()
would be the proportion of new width with respect to old width. If these were the same, it would be 1 and hence not chance height.
if ((nh > h) && h > 0) {
nh = h;
nw = (h / $obj.height()) * $obj.width();
}
Do same for height, assuming new height is greater than resize height h
and passed height is some meaningful value.
xscale = $obj.width() / nw;
yscale = $obj.height() / nh;
Create or assign global variables xscale
and yscale
to inverse of newly created proportions. In other words, if you halved width and height, xscale would be 200% and yscale would be 200%. This is likely in order to know how to reverse the operation later, though that's just speculative.
This is a function side effect, and this is generally bad practice since it can be glanced over too easily, however I'd be lying if I claimed I never needed to do something of this nature. Call me lazy.
$obj.width(nw).height(nh);
Attempt to assign object's new width and height to calculated width and height. If this were an image, this would likely work out well, however, this is by no means guaranteeing that the resulting width and height will be the calculated width and height.
}
Hope that helps.