For today I would like to write another post about a canvas example, because canvas is fun and life is short. Todays post on canvas examples will be an example of a basic clock. Making clocks is fun because doing so is easy, but there is also lots of room when it comes to doing things that are original with it.
So lets start out with a very basic canvas clock example. This will involve a get clock method that will create a clock object based on the passed date given as the first argument. The object will contain properties that are then used in one or more draw methods that are used to render the current state of that clock object.
So here is the get clock methods that I would out for this canvas example, and this section of this post. The aim here is to work out the basic idea of what I should be doing when it comes to making a canvas clock. I want to keep the state of the clock separate from methods that are used to draw it, and also I would like to have a get clock method designed in a way in which the same state object is returned for the same arguments that are given. Also in the event that no arguments are given it will always return a state object that reflects the same point in time. In other words a pure function of sorts, even though it is a state object that is returned.
So now that I have my get clock method I will n ow want to work out at least one method that will render the state of this clock object.
Here I have a draw method that will render the clock object that is returned with my get clock method. For now with this canvas example the draw method just displays the timeText property of the clock object. In additional examples I will add properties to my clock object when it comes to making a more advanced get clock method, but for now I want to keep things basic and just have a digital clock.
Here I have the main app loop of the canvas example, I am just getting a clock object of the current time, clearing the canvas, and then calling my draw clock text method.
So then I have the rest of the code that gets a reference to a canvas element that I have in the HTML of the example, sets the size, and then makes use of what I have worked out by just calling the loop method at a set time interval.
So then when I have this canvas example up and running it just displays a simple digital clock. Nothing to interesting, but it is just a start, and any more interesting clock is just going to be a more advanced examples of this. When it comes to my get clock method I will want that to always give me the same clock object for the same date that is passed each time. That object can have all kinds of things attached to it that can be used in the render process. Including things like display objects the position of which change depending on the time of day, the time of year, or even over all time if I want to get that nuts.
This is a post on making a basic canvas clock example, but lets look at least one more example of this to get a better idea of what this can lead to and why it can be really fun to get into this sort of thing.
In this section I am going to be going over a slightly more advanced version of my get clock method. It is still the same basic idea, pass a date object, and get a state object for that date object every time. However now I am adding just one more property that is a value that reflects the percentage of time that has passed for the current date based on the passed date object. In other words it will display zero percent if it is midnight, and will process to one hundred percent as it approaches midnight of the next day.
Here Is the get clock method that gives the same propertyy as before, but now also gives a day percetn property that is a value between 0 and 1 depending on the time of day.
Now I have not one but two draw methods one of which displays the clock text as before so there is not much to say there. However I also now have another draw methods that will render a circle around the text which will also reflect the amount of time that has elapsed sense midnight and how much time is left for the day.
So now I just need an updated loop that calls both draw methods, other than that the canvas example is the same as before.
So making clocks in canvas can be pretty fun, and there is some much room for originality.