This canvas example will be of a game spinner. In other words a virtual from of one of those things that you get in many board games that functions as an alternative to dice that has a spinner or arrow type thing attached to the center of a disk with a bunch of sections on it. So this canvas example will involve a module that can be used to create a state object for this sort of thing, and like aways a draw module that is used to draw the state of one of these to a canvas element.
So in this section I will be going over all the features of my spinner.js file that can be used to create an instance of a spinner state object. This state object can then be rendered to a canvas element using a draw.js module for this spinner state object that I will be getting to in a later section in this post.
I start off the module with an IIFE that will contain just a few internals that as of this writing is just a get section helper method, and a value that holds the value of PI time two. If I where to continue working on this one I would expand this with more helpers and such, but for now I think I would like to keep this canvas example simple and to the point.
The module contains a public API that is used to create an instance of one of these spinner state objects, and then a bunch of methods that are used to set up a new spin, and update the state of that spinner object on each frame tick. So the general idea here is to use the create method to create a spinner state object once, and then use the start spin method in the body of an event hander that will start the spin. Then the update method will be called in an app loop update method to update the state of the spinner on a frame by frame basis.
The core of the idea I had in mind with this module is that I wanted something that can be used to create more than one instance of this spinner state object, and to have a way to customize it. In the state object there is the sections array that will hold the possible values for each type of section and then the section indices array that will hold index value for each element in the sections array that can be used as a way to map what values go where in the spinner. the reason for this is that I might have a spinner that contains more than one section with the same value, so I have an array of values, and an array of index value for those values. Maybe this makes the state object a little more complex, but is an exercise of something that might help to make this a little better organized so that I do not repeat values.
In any case I am sure if I where to start using this in a real project I would be adding a lot more to it. However a lot of what would change would vary depending on the nature of the project in which I would be using it.
This is a canvas example so like always I will want a module that is packed with methods that I can call to render the state of a state object to a canvas element in this case a spinner state object. Here I went with an IIFE module pattern yet again even though I am making everything public, I often go with IIFES anyway when it comes to these kinds of modules and modules in general because sometimes I add private helpers later on. However hear I do have one such private helper method thus far so it still makes sense to go with an IIFE rather than the object literal pattern. I also have it so I pass the drawing context, canvas, and any other objects that I need to draw in the given methods rather than grabbing at globals for these things.
So I have broken the process of drawing a spinner into a few steps. First I would want to draw the sections of the spinner object, then the current state of the arrow, and finally the debug info of the spinner state object. So now that I have a state module and a way to draw a state object created with that state module, lets look at just a little more code that puts all of this into action.
And the html for the example.
So then the result of this is a spinner at the center of the canvas element when it click the canvas the spinner starts moving and will start to slow down also. Eventually it will land on a section the value of which is displayed via the use of my draw info method in the draw module.
In a real project I would change the index values for the sections, as well as what the sections are. In some cases I could have an array of objects rather than numbers for the sections that would contain style into for the sections when it comes to further skinning the spinner.
So this example is more or less what I had in mind but I did not take the time to create any kind of actual game around it. It is still a nice quick canvas example project though when it comes to making this sort of thing. There is more that I could do with this when it comes to use case examples, and adding additional features to it, however I also have many other canvas examples that I thing deserve a higher degree of attention when it comes to further work to be done on these canvas examples of mind, as the basic idea is very much there with this one.