Shortening JavaScripts with Math

I just went through an exercise for a DOM scripting course with the YUI and had the task to write a function that takes any element and centers it at the current cursor position. I also wanted to make sure that the displayed object never causes scrollbars or gets cut off when the cursor is too high up the document.

Getting the size of the object and the browser constraints was easy with the YUI. I sent the object as o and e is the event:

var size = YAHOO.util.Dom.getRegion(o);
var oHeight = size.bottom - size.top;
var oWidth = size.right - size.left;
var screen = [YAHOO.util.Dom.getViewportWidth(), YAHOO.util.Dom.getViewportHeight()];
var curpos = [YAHOO.util.Event.getPageX(e), YAHOO.util.Event.getPageY(e)]

Now I had the problem of keeping the values in the constrains. Centering the element was easy, you just substract half of the height from the vertical position and half of the width from the vertical cursor position.

var x = curpos[0]- oWidth/2;
var y = curpos[1]- oHeight/2;

I then had to compare both with their constraints and set them to the appropriate values, which was a lot of if statements:

if(x < 0){
  x =0;
}
if(x + oWidth > screen[0]){
  x = screen[0] - oWidth;
}
if(y < 0){
  y =0;
}
if(y + oHeight > screen[1]){
  y = screen[1] - oHeight;
}

clunky, and I shortened it using the ternary notation:

var x = curpos[0] - oWidth/2;
x = x < 0 ? 0 : x;
x = x + oWidth > screen[0] ? screen[0]-oWidth : x;

var y = curpos[1] - oHeight/2;
y = y < 0 ? 0 : y;
y = y + oHeight > screen[1] ? screen[1]-oHeight : y;

lt felt terrible. I then remembered the Math object in JavaScript and that it has two methods that are terribly useful in this case: min() and max(). Both return a value that is either the smaller or the larger value, which means you can use it to constrain a value to a certain range. Using Math, the whole logic can be done in two lines of code:

var x = Math.min(Math.max(curpos[0] - oWidth/2, 0), screen[0] - oWidth);
var y = Math.min(Math.max(curpos[1] - oHeight/2 ,0), screen[1] - oHeight);

That is not the end of it, though. If you know that you should get back a number, you can use max() to normalize browser differences, like the Dom utility of the YUI does:

var scrollTop=Math.max(doc.documentElement.scrollTop,doc.body.scrollTop);

This works around the MSIE issue of reporting different values for scrollTop depending on which rendering mode you are in.

10 Responses to “Shortening JavaScripts with Math”

  1. Web Dev Digest Episode 2 | ara pehlivanian—Web Standards, Web Culture, Web Everything.™ Says:

    [...] sten to this post Skip to comments Web Dev Digest Episode 2 Show links: Writing elegant JavaScript using Math Internet Explorer’s onResize Event Server-side Pre-processing [...]

  2. Shortening JavaScripts with Math « [REF] Says:

    [...] ject never causes scrollbars or gets cut off when the cursor is too high up the document. Link [Via Wait Till I Come] Entry Filed under: math, javascript, +geek Leave a Comme [...]

  3. Ara Pehlivanian Says:

    Wow! Very, very elegant (and smart) use of math to avoid unsightly if/else statements. This one’s a definite keeper.

  4. Nate Klaiber Says:

    Very nice! That definitely keeps things neat and tidy and eliminates the conditional statements over several lines.

    Nice work!

  5. Thom Shannon Says:

    That’s pretty cool, but I think there’s something to be said for keeping your code clearly legible too

  6. Tamlyn Rhodes Says:

    “I think there’s something to be said for keeping your code clearly legible too”

    I would say the x/y example is more legible using min/max and the scrollTop example only takes a couple of seconds to work out what’s going on. Neat stuff.

  7. Blair M Says:

    I’ve been using this technique in a jQuery plugin I wrote a few months ago. When I first thought of it I was surprised so few people used it as a quick and elegant range limiting equation.

  8. Lance Fisher Says:

    I like it. Even if you add a big ‘ol two line comment in there, it’s still shorter and nicer. Math is cool.

  9. chrissie brown Says:

    As an oldschool C-Programmer, i also love the ternary operator very much and use it very often.

    Unfortunately, there are some recommendations out not to use it because most programmers do not know this and you can generate unreadable code especially if you nest ternary operators :-)

    I also think your math logic ist best, but you have to have even more knowledge about the functions and stuff …

  10. suma valluru Says:

    I have been using this technique from long time..and iam really liking it…

    Cheers,
    Suma valluru

Leave a Reply

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).