When I am starting out with a canvas project there is often a need to have some kind of system in place for creating a simple user interface html canvas buttons that consists of just a bunch of buttons. These buttons can end up preforming all kinds of actions when clicked, and it sometimes might be nessecry to create a fairly complex module for them. You would think that this would be a simple task when it comes to canvas, but things in canvas are not like things are with html outside of the canvas element where one can just add an input element.
Sure an input element and an event handler for it will work just fine in a pinch, but what if I want display objects rendered in the canvas to be what the button is for buttons? Doing so is more or less the same thing as creating display objects in general, it is just that we are creating a special kind of display object that is a user interface button. So in this canvas example post, I will be going over a basic button layout solution for a canvas project.
This button layout will working okay when it comes to certain projects, however in others I might want to do additional things to make it so that the buttons move in and out of the canvas. There is a fine line between a user interface button and a display object that is used to represent an enemy or play object in a game for example. In some projects I guess the only difference might be how a button is skinned compared to other display objects and what happens when you click or touch one.
In order to have a functioning button rendered in the canvas element as a display object, I am going to need a way to detect if an area has been clicked or not. So I will need a utility library of sorts that has at least a bounding box hit detection method as one of its methods. I will also want to have a method that will process a mouse or touch event so that it returns a canvas relative rather than window relative position of a click or touch event.
In addition to some basic methods for hit detection and working with event objects, I will also have a method in here that can be used to create a button layout object. This object will contain the current state of all buttons, and what is to happen when one of them is clicked.
To work with buttons that are actually display objects in a canvas element rather than html elements positioned over a canvas, I am going to need to get canvas relative mouse and or touch positions as well as a means of collision or hit detection.
I also have a method that will return a canvas relative position from a mouse or touch event. The canvas relative position can then be used in conjunction with another method that has to do with bounding box collision detection inside yet another method in this utility library that will be used to find out if a button is clicked or not.
So now that I have covered a few usual suspects for many of the custom utility library for my canvas examples, it is now time to move on to the next, and last method in this library. this final method in the library is what is used to create a button layout for a project.
So here I have my make button layout method that can be used to make a button layout object of sorts. The object that this method helps create contains an array of button objects. Each button object has properties such as and x and y position as well as a width and height. The properties of the button object can be used in conjunction with the processes pointer event positions to find out if a button has been clicked or not.
In the event that a button is clicked then an on action function that is also a property of a button object will be called. This on action method can contain custom logic that is to be fired when a button is clicked that has to do with the nature of the project in which this is used.
For now I made it just part of the general utility library, but if I keep working on this project it will likely be pulled into its own module. Anyway now that I have this make button method worked out I should have a way to draw the current state of a button layout object, and get together a basic demo of this to help confirm that it is working. So lets look at a draw module that I made for this canvas example, and move on to a little demo code that will make used of all of this.
For this canvas example I will of course have a draw module that will contain methods that can be used to draw to a canvas element. For this example I just have a draw background method and a method to draw the current state of a button layout object that I can create with mu utility library.
So now for just a quick example where I am testing out this utility library method that will create a simple button layout. Here in the main.js file of this example I create and inject the actually canvas element into a container element as I do in just about every other canvas example that I have made thus far for this site. I then have references to the canvas element, as well as the drawing context to which I pass to my draw module when it comes to rendering in a main app loop.
So the basic idea that I had in mind for this project is working, but there is much more to do when it comes to this. There is of course having more than one instance of a button layout object that will change depending on an application state. In addition there is maybe adding more features that have to do with the movement of button objects in and out of the canvas as menus change. I might get around to expanding this post and project when it comes to all of that, but yet again maybe that is all a matter for another post.