JavaScript example of a grid module
I made a canvas example a while back on how to go about making a grid, but that post was more so on drawing one, not having something that is a state object, and methods that act on that state object. So then in this JavaScript example post today I thought I would go about writing about a simple javaScript grid module.
There are many basic features that a grid module should have, such as a public method that can be used to get a cell by way of a canvas pixel position for example. There are also more advanced features such as path detection that maybe should be a part of a grid module, or maybe a part of an optional module that can be used on top of a grid module. In any case for this example I am going to just be sticking with the very basics of this sort of thing. However do not let that fool you, even when it comes to the very basics of a grid module there is still a fare about of ground to cover.
1 - The grid module
So then the first thing I would like to get to is the current state of the grid module that I have made for this post. When it comes to making a choice as to what kind of pattern I should go with when it comes to making a javaScript module I went with an IIFE.
1.1 - The create method
The main basic method of interest is the create method of this grid module that will create and return a new grid object. So right off the bat there is the question of how to go about structuring a grid object as there are so many different ways of going about doing that sort of thing in javaScript. For example many javaScript developers might like to create a grid as an array of arrays, however others present company included like to use a linear array and use expressions to get and set values of cells. This alone can prove to end up being a situation that can get a developer stuck on something that is trivial, however I often think it is basic to just make a decision and move forward when it comes to things like this. Also I can alter on make additional create methods that can be used to create my standard grid object from other kinds of gird object formats if I end up in a situation in which I need to.
So then the start of my grid module is the beginnings of an IIFE, and the create method.
|
|
1.2 - The get cell by pixel position method
A must have method for a grid module is a method where I pass a canvas relative pixel position, and what is returned is a cell that is at that position. There are a number of ways to go about making this kind of method, one way might be to loop over all of the cells and using bounding box collision detection. However It might be better to use some examples that just involves some quick expressions.
|
|
1.3 - Selected check method
I have a selected cell feature for this grid module so far. There is also the idea of selecting more than one cell also though. So this is a method that I might revise at some point.
|
|
2 - The utils module for this over all example
I often have a general utility module when it comes to making an over all javaScript project of some kind. This module serves as just a general dumping ground for methods that I might use in one or more additional modules, and I can not think of any other place to put them. The state of this kind of module will differ a little from one project to the next, but I have wrote a post in which I am going over a version of this kind of module that has many of the usual suspect methods.
|
|
3 - The draw file for the example
I have just a few draw methods that I will be using for this example. For now I just want a method to draw a simple solid background, and of course a method to draw the current state of the grid.
|
|
4 - The main JavaScript file of this example
I then also have a main javaScript file that will make used of my grid module, as well as all the other JavaScript files that I have covered in this post. In this file I have what is the beginnings of a state machine, but this this example I will only need one state objects so I am not going to do anything to advanced when it comes to that sort of thing here. Still when it comes to making a full project of some kind a state machine is an important part of a project that might prove to be a little involved. I have mad a canvas example in which I am getting more into the topic of state machines, and I have a lot of other examples of this sort of thing. Like that of a grid module a state machine is another examples of something that I find myself recreating from the ground up a lot.
In the main.js file of an example I will often have a main app loop, that will make use of the request animation frame method as a way to have such a loop. There are a few other options when it comes to amking a main app loop, but when working with canvas request animation frame might be the best option.
|
|
5 - Conclusion
That will be it for now when it comes to this grid module, however this might prove to be one of my many javaScript posts in which I will be coming back to do some editing now and then. There are a lot of other features that I would want to add to this kind of module, but the features will differ a little from one project to the next. Still this is the kind of thing that I have grown tied to making all over again each time I start a new vanilla javaScript project that calls for a module such as this. So it would be good to get a solid module for this sort of thing together and be done with this once and for all.