There are many approaches to this. But, basically, you want to draw these buttons in, say, GIMP, and then put them on screen using a canvas. Then, register an onclick event to the canvas, and when it fires you want to iterate through an array of buttons (I suggest you make a constructor function for that; that would be a class in other languages), and when you see that a button has been clicked, you execute it's onclick function. The button's onclick function can be something like new Game();
, given that you wrapped your game inside a function like I always do.
For that, you need to know the position of the canvas in the web page, because onclick tells you the mouse's absolute position, and you need a position relative to the canvas. That is simple to solve. First, you calculate the offset of your canvas with this:
function findPos(obj)
{
var curleft = curtop = 0;
if (obj.offsetParent)
{
do
{
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
}
while (obj = obj.offsetParent);
}
return [curleft,curtop];
}
CO = findPos(canvas); //CO stands for canvas offset
Once you have that, this should be how you calculate the actual click position:
var mouse = [0, 0];
function onclick(e)
{
mouse[0] = e.pageX - self.CO[0];
mouse[1] = e.pageY - self.CO[1];
/*Rest of the code*/
}
canvas.addEventListener("click", onclick);
This is all if you actually want to create your start menu using a canvas. You might not want to do that, because it would be easier with just HTML. This has several advantages though, like animation and so forth...
Also, there is a way to find out the text size. The height of the text is simply the font size, but to get the width of the text to be drawn use this:
context.measureText("some random text").width