Skip to content

Instantly share code, notes, and snippets.

@marcelaraujo
Created April 14, 2014 01:49
Show Gist options
  • Save marcelaraujo/10610335 to your computer and use it in GitHub Desktop.
Save marcelaraujo/10610335 to your computer and use it in GitHub Desktop.
jQuery window resize
/* Get size of viewport */
layout.winWidth = $(window).width();
layout.winHeight = $(window).height();
/* Get available width by subtracting the size of the rest of the components */
var dwidth = layout.winWidth - 63;
var dheight = layout.winHeight - 111;
/* Check if scrollbars are needed (cheight and cwidth are canvas element's height and width)*/
var vScrollNeeded = dheight < app.cheight;
var hScrollNeeded = dwidth < app.cwidth;
/* Final width */
var fwidth;
var fheight;
/* If the available width is greater than the size of the canvas + size of the scrollbars, if
they're needed, set it to the size of the canvas + size of the scrollbars (if they're needed).
Otherwise, set it to the available space. */
if (dwidth > app.cwidth + (vScrollNeeded?layout.scrollWidth:0)) {
fwidth = app.cwidth + (vScrollNeeded?layout.scrollWidth:0);
} else {
fwidth = dwidth;
}
/* Same with vertical height */
if (dheight > app.cheight + (hScrollNeeded?layout.scrollWidth:0)) {
fheight = app.cheight + (hScrollNeeded?layout.scrollWidth:0);
} else {
fheight = dheight;
}
$(layout.canvas).width(fwidth);
$(layout.canvas).height(fheight);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment