Liquid menu: Better proportions with Math.sqrt (4)

, under actionscript, Code snippets.

Square rootFound out something useful, which I’d like to share with you. It’s about calculating the width of menu-items perfectly.

Imaging you are creating a liquid website with a menu. If you want all items to have the same width, you could set the width of each item to totalMenuWidth / items.length. In most cases this will work out. But what is your menu is very small or the items doesn’t fit in that fixed width because the text is too long?

Well you could hardcode the width’s to make your own perfect proportions and spacing, but if your menu is xml-driven and you/someone else adds new menu-items, you have to re-assign the hardcoded widths and you feel unhappy 🙂 Most hardcoded stuff is evil anyway because it is laziness. (I use it a lot :P)

Well, I’d like to calculate the perfect scaling menu, so I don’t have to worry the menu will break. You can use the text-length of the menu-item as input for the width calculation. The more letters, the bigger the menu-item, right? So, If you multiply all menu-text-lengths and divide this to the current text-length, you’ll get a ratio which can be divided from the total menu width. I like this theory. So we need to do this in 2 steps; first get the ratios and get the total menu-text-lengths (with a loop), then calculate and apply this on each item.
Pseudocode:

itemwidth = fullMenuWidth / (totalTextLength / menuitem.text.length)

This works fine, and its actually very cool. Now we have a function which actually use some proportions to create a liquid menu. After a while I realized that the proportions weren’t really right. If the menu text have odd text-length differences (eg 5 letters vs 25 letters), it looks weird. Larger texts take too much width. I wanted the same proportions kinda like a html-table.

So the menu-items needs to have better proportions. Bigger text should be a smaller and the smaller text should be bigger. After some searching I ‘discovered’ Square root, a.k.a. Math.sqrt() in actionscript. With this function you could ‘normalize’ numbers. Bigger numbers are getting smaller, smaller numbers are getting bigger. This was exactly what I needed!

You can see the result below:

You can see the differences between the menubars and you’ll agree to me that the last one is the best 🙂 I think this theory could be applied to a lot more things, like graphs or grids.

In case anyone is interested in the code; it can be downloaded here (AS3).

4 responses to “Liquid menu: Better proportions with Math.sqrt”

  1. Caio says:

    Great post!
    Just wish you had found that like 7 months ago =P

  2. Alex says:

    Very Cool!

  3. Melvyn says:

    Interresting!

    I tried with another method:

    // space between text and item border (xSpacing) is equal to:
    // total width: totalWidth
    // minus borders: (totalItems – 1) * borderSpace
    // minus total text width: totalTextWidth
    // divided by 2 spaces per item: (2 * totalItems)
    xSpacing = (totalWidth – (totalItems – 1) * borderSpace – totalTextWidth) / (2 * totalItems);

    Check the source : http://melvynhills.be/public/Menu.fla.zip

  4. Rob says:

    Fascinating – great post!

Say something interesting

Please link to code from an external resource, like gist.github.com.