HTML5 Canvas Navigation menu with Fire
Join the DZone community and get the full member experience.
Join For Free
html5 canvas navigation menu with fire
have you ever thought about creating some interactive navigation
menu in html5 directly in the canvas element? yes, this is a strange idea,
but it is possible. i prepared our first pure html5 canvas menu (basically –
this is a set of buttons). we will create these buttons with a fire effect at the bottom. you will be able to set custom click actions
for menu elements.
live demo
preview
step 1. html markup
basically our html markup is very easy today:
<!doctype html> <html lang="en" > <head> <meta charset="utf-8" /> <title>html5 canvas navigation menu with fire | script tutorials</title> <link rel="stylesheet" href="css/main.css" type="text/css" /> <script src="js/vector2d.js"></script> <script src="js/fire_menu.js"></script> </head> <body> <canvas id="panel" width="1000px" height="100px">html5 compliant browser required</canvas> <img id="image" src="images/bg.jpg" /> </body> </html>
there is only single canvas element. plus – one image directly after this canvas.
step 2. js
now, we should create a new empty file ‘js/fire_menu.js’ and put next code inside:
// button object function button(x, y, w, h, state, image, text) { this.x = x; this.y = y; this.w = w; this.h = h; this.state = state; this.imageshift = 0; this.image = image; this.text = text; } // draw button function function drawbutton(ctx, button) { // draw button image ctx.drawimage(button.image, 0, button.imageshift, button.w, button.h, button.x, button.y, button.w, button.h); // and text ctx.filltext(button.text, button.x + button.w / 2, 5 + button.y + button.h / 2); } // get mouse position function function getmouseposition(e){ if (!e){ var e = window.event; } if (e.pagex || e.pagey){ return new vector2d(e.pagex, e.pagey); } else if (e.clientx || e.clienty){ return new vector2d(e.clientx, e.clienty); } } // inner variables var canvas, ctx; var data_width; var data_height; var colors = []; var out_data = []; var buttons = []; // fill new array with certain value function fill_new_array(len, val) { var rv = new array(len); while (--len >= 0) { rv[len] = val; } return rv; } // prepare palette function function prepare_palette() { for (var i = 0; i < 64; ++i) { colors[i + 0] = {r: 0, g: 0, b: i << 1, a: i}; colors[i + 64] = {r: i << 3, g: 0, b: 128 - (i << 2), a: i+64}; colors[i + 128] = {r: 255, g: i << 1, b: 0, a: i+128}; colors[i + 192] = {r: 255, g: 255, b: i << 2, a: i+192}; } } // draw main scene function drawscene() { ctx.clearrect(0, 0, ctx.canvas.width, ctx.canvas.height); // clear canvas // draw fire var data_cnt = data_width * (data_height - 1); for (var i = 0; i < data_width; i++) { out_data[data_cnt + i] = (0.6 > math.random()) ? 255 : 20; } for (var y = 0; y < 100; y++){ for (var x = 10; x < data_width - 10; x++){ var s = data_cnt + x; var temp_data = out_data[s] + out_data[s + 1] + out_data[s - 1] + out_data[s - data_width]; temp_data >>= 2; if (temp_data > 1){ temp_data -= 1; } temp_data <<= 0; out_data[s - data_width] = temp_data; var id = s << 2; img_data.data[id + 0] = colors[temp_data].r; img_data.data[id + 1] = colors[temp_data].g; img_data.data[id + 2] = colors[temp_data].b; img_data.data[id + 3] = colors[temp_data].a; } data_cnt -= data_width; } ctx.putimagedata(img_data, 0, 0); // prepare font ctx.font = '26px ds-digital'; ctx.fillstyle = '#000000'; ctx.textalign = "center"; // draw all the buttons for (var ib = 0; ib < buttons.length; ib++) { // drawbutton(ctx, buttons[ib]); } } // window onload event handler if (window.attachevent) { window.attachevent('onload', main_init); } else { if(window.onload) { var curronload = window.onload; var newonload = function() { curronload(); main_init(); }; window.onload = newonload; } else { window.onload = main_init; } } // main initialization function main_init() { // create canvas and context objects canvas = document.getelementbyid('panel'); ctx = canvas.getcontext('2d'); // prepare data for our fire object and prepare palette img_data = ctx.createimagedata(canvas.width, canvas.height); data_width = img_data.width, data_height = img_data.height, prepare_palette(); // fill new array with 0 out_data = fill_new_array(data_width * data_height, 0) // prepare image to buttons var buttonimage = new image(); buttonimage.src="images/button.png"; buttonimage.onload = function() {}; // prepare 3 different buttons buttons.push(new button(0, 10, 245, 62, 'normal', buttonimage, 'button #1')); buttons.push(new button(250, 10, 245, 62, 'normal', buttonimage, 'button #2')); buttons.push(new button(500, 10, 245, 62, 'normal', buttonimage, 'button #3')); buttons.push(new button(750, 10, 245, 62, 'normal', buttonimage, 'button #4')); // loop main scene setinterval(drawscene, 40); // onmousemove event handler canvas.onmousemove = function(e) { var mouse = getmouseposition(e).sub(new vector2d(canvas.offsetleft, canvas.offsettop)); for (var i = 0; i < buttons.length; i++) { // apply 'hover' state for buttons if (buttons[i].state != 'pressed') { buttons[i].state = 'normal'; buttons[i].imageshift = 0; if (mouse.x > buttons[i].x && mouse.x < buttons[i].x+buttons[i].w && mouse.y > buttons[i].y && mouse.y < buttons[i].y+buttons[i].h) { buttons[i].state = 'hover'; buttons[i].imageshift = 136; } } } } // onmousedown event handler canvas.onmousedown = function(e) { var mouse = getmouseposition(e).sub(new vector2d(canvas.offsetleft, canvas.offsettop)); for (var i = 0; i < buttons.length; i++) { // apply 'pressed' state for buttons if (mouse.x > buttons[i].x && mouse.x < buttons[i].x+buttons[i].w && mouse.y > buttons[i].y && mouse.y < buttons[i].y+buttons[i].h) { buttons[i].state = 'pressed'; buttons[i].imageshift = 68; } } } // onmouseup event handler canvas.onmouseup = function(e) { var mouse = getmouseposition(e).sub(new vector2d(canvas.offsetleft, canvas.offsettop)); for (var i = 0; i < buttons.length; i++) { // reset states for buttons if (mouse.x > buttons[i].x && mouse.x < buttons[i].x+buttons[i].w && mouse.y > buttons[i].y && mouse.y < buttons[i].y+buttons[i].h) { alert(buttons[i].text + ' is pushed'); } buttons[i].state = 'normal'; buttons[i].imageshift = 0; } } }
the main idea of our menu – put several buttons at canvas element, and then – add event handlers. also, as extra feature – animated fire at the background. this demonstration can bring you new ideas for your projects.
step 3. extra files
it’s possible that you have noticed that i use the addition files in our project: main.css, ds-digib.ttf, bg.jpg, button.png and vector2d.js. all these files are available in our download package.
live demo
conclusion
i hope that you like our html5 experiments. welcome back.
Opinions expressed by DZone contributors are their own.
Comments