Time for action – initializing menu items

Next we will initialize the Color menu to set the background color of each item to the color it represents. We could do that in CSS but it would be cumbersome. Instead we are going to write a JavaScript method to set them all with just a little bit of code:

function initColorMenu()
{
    $("#color-menu li").each(function(i, e) {
        $(e).css("background-color", $(e).data("value"));
    });
}

This gets all of the color menu items and iterates over them using the jQuery each() method. For each item it sets the background color using the jQuery css() method to the value of the data-value custom attribute, which is a CSS color name. Just like that we have a menu of colors.

We want to do something similar for the width menu's items, except we will set the bottom border to the width in the data-value custom attribute to give the user some idea of how big the line will be:

function initWidthMenu()
{
    $("#width-menu li").each(function(i, e) {
        $(e).css("border-bottom",
                 $(e).data("value") + "px solid black");
    });
}

We will call these two methods from the start() method when we're initializing the application.

What just happened?

We changed the styles for the color and width menu items to give them colors and widths respectively so that the user can better see what they are selecting from the menus.

Now if you open the application in the browser you can change the pen's properties. Go ahead and draw a few lines. If you click on Undo, the last line is erased. When you click on Clear, the entire drawing is erased.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.128.129