Creating an animating clock
Now that we know the basics of working with Math.sin and Math.cos it's time for us to continue and develop a clicking analog clock.
Keynotes:
- Animating Canvas elements.
- Creating an updating clock.
- Working with radians.
Video Transcript
00:00Taking advantage that we know what sine, cosine,
00:03and radians are, we're gonna go ahead
00:05and create an animating clock in this lecture.
00:09We're gonna go ahead and create an an canvas element
00:12that refreshes, that redraws itself.
00:15We're gonna create an updating clock.
00:18And last but not least, we're gonna work with radians
00:20and really, for that matter,
00:21auto all the other concepts that we were talking about.
00:25So let's jump right into it.
00:26Let's just create the sample
00:27that's gonna really help us solidify everything
00:30we talked about in the last lecture.
00:33All right, now that we got the basics of cosine and sine,
00:36let's get ahead and start actually using it.
00:39So I'm gonna go ahead here
00:40and just delete all this extra logic up here.
00:42You know what, I'll just leave it.
00:43But I'm gonna go ahead and continue my process.
00:46So the first thing I'm gonna do
00:48is I'm gonna really need a variable
00:49that's gonna tell me, oh, so what's the current time?
00:52So to do that, I'm just gonna add another date variable
00:55and just set it to be the new date.
00:56And I'm gonna go ahead and also add a couple of variables
00:59to help me starting with an hour one,
01:01which would get me the current get hours.
01:06And I'm gonna also just make sure
01:07that the value will be up to 12 and not more than that.
01:10So I'll mold do it by 12.
01:12So I'll only get the leftover of a division by 12.
01:15I'm gonna go ahead and also get my minutes.
01:18And I'm gonna call that date get minutes.
01:23And last but not least,
01:24we'll do that for the seconds as well.
01:26And I'm gonna get the seconds.
01:29All right, so now I have a reference,
01:30a few reference variables that should help me figure out
01:34and work inside of this creation process.
01:37Let me just add one more variable
01:39before we continue down this path.
01:41And that variable is gonna be degree
01:43because it's easier for us as humans
01:45because we're used to degrees.
01:46To work with degrees,
01:47I'm gonna assume that to be a degree.
01:49And I'm gonna go to the math.py
01:51and just divide it by 180, which would give us one degree.
01:55And that one degree, we could then more easily
01:57then use and configure to change
01:59and manipulate our data as we see fit.
02:01And I'm gonna also create another item
02:03which will be called current degree.
02:05And I'm just gonna set it to be zero by default.
02:07And we're gonna use that throughout this lectures.
02:11So let's go ahead and start with our seconds.
02:13I wanna go ahead and create the seconds dial.
02:16To do that, I'm gonna go ahead here
02:18and when our creation process begins,
02:21and I'm just gonna go ahead here in this creation process,
02:24I'm just gonna go ahead and delete this and start it over.
02:27What do I wanna do?
02:27Where do I wanna go?
02:28First of all, it's true.
02:30Our first beginning point,
02:31we're gonna move to our X and Y base coordinates.
02:35We wanna start from the basic point, that makes sense.
02:38The next step for us that we really wanna do
02:40is we wanna actually get to the actual seconds.
02:43To do that, I'm gonna go ahead and create
02:45and use that current degree.
02:48And I'm gonna go ahead and start calculating.
02:50So first of all, I wanna duplicate the actual degrees
02:52or those radians with the values.
02:55So first of all, what I wanna do
02:56is I wanna create a starting point
02:58which would be an offset of 90 degrees
03:00because I'm gonna be using the cosine first
03:05and then the sine.
03:06So I'm gonna subtract 90 no matter what.
03:09Once I subtracted that 90,
03:10what I wanna do is I also wanna add in our seconds.
03:14But each second, how much of a degree does it represent?
03:18I'm gonna need to use a calculator for that.
03:20I'm gonna go ahead here and just take 360 degrees
03:23and I wanna divide it by the 60 seconds there is in a minute
03:27and I'm gonna get the value of six.
03:29So for every second, it was represented by six degrees.
03:33So that is gonna be the value of the current degree
03:36that we're basically trying to reach.
03:39All that's left for me to do
03:40is once we've configured that current degree
03:42is also go ahead and actually use it.
03:45So I'm gonna go ahead and just for the sake of order,
03:47I'm gonna put that up here before
03:49and I'm gonna go into my context
03:51and I'm gonna create a line two.
03:53And this time around, again,
03:54I'm gonna set it to my X and Y
03:56but I'm also gonna add to the X and Y.
03:58What do I wanna add?
03:59Just like we did previously the math, that cosine
04:03and I'm gonna send into it the current degree
04:06and then duplicate all of that by our radius.
04:10Now I'm gonna do the exact same thing for the Y
04:12but instead of using cosine, I'm gonna use sine.
04:17Now the only thing that's really critical here
04:18is make sure that you create the right offset.
04:20So before you even do anything, just put zero as the value.
04:23Make sure that your zero correlates to a clock's zero
04:27and then you could start working with the extra logic.
04:30So now that we created this line two logic,
04:32if I go back into my application and click on refresh,
04:34we should see that our element
04:35is gonna go right in the current time.
04:37So if I go and click on refresh,
04:39we'll see that the time will tick along.
04:41Let me also to make it a little bit easier
04:43for us to view, I'm gonna go ahead into my console
04:45and I'm just gonna log out our hour, minutes and seconds
04:50so we could actually see them.
04:52Now beyond that, I also wanna add one more thing
04:54before we continue our creation process.
04:56I wanna call a set timeout
04:58because this is never going to be killed.
05:00I'm gonna go ahead and create time,
05:02call the same function that I'm in,
05:04call it once a second and just pass through
05:06our current canvas that was the only property
05:08that was sent inside of our create time.
05:11By doing this once a second,
05:13our function should recall itself.
05:15So let's go ahead and click here on refresh
05:17and we should see that once a second,
05:19our clock will continue ticking.
05:21Now we do have a few issues with our logic
05:23now that we're recalling it.
05:24We're gonna have to add here a little bit more things.
05:27So the first thing that we're gonna wanna do
05:29is we're gonna wanna go back
05:30to the beginning of our code here
05:32and make sure that we're not taking something
05:36that was already there before
05:37because when we were done with this begin here,
05:39we had a few lines going on here.
05:41So we wanna go ahead and make sure
05:43that the first thing we do before we start
05:45doing anything on our screen
05:47is we wanna go to our context
05:49and we wanna tell our context
05:51that by the way, we wanna clear you out.
05:54To do that, what I'm gonna do
05:55is I'm just gonna make sure
05:56that I'm gonna call a new begin path
05:58that's gonna be called before anything is done.
06:01Now that I've done that,
06:02we won't see this triangle shadow following along
06:06as the clock is ticking
06:08and the second style will continue ticking.
06:11So let's continue down this path.
06:12Now that we've created the seconds dial,
06:15I'm gonna go ahead
06:15and I'm gonna continue into the next dial.
06:17I'm just gonna take all this
06:19to bounce it out and push it together.
06:22So we have here a new path,
06:23we set a stroke style
06:24and I'm gonna go ahead
06:25and create a new style
06:27right after we created that stroke.
06:29Same color so we don't have to change the color
06:31but I'm gonna make our dial a tad smaller
06:34or other way around,
06:37we'll make our seconds really small
06:39and our minutes a little bit larger.
06:42All right, so the next stage is
06:43I really wanna create the dial for the minutes.
06:46I'm gonna go ahead here in the dial of the minutes
06:48and let's go ahead and just copy this logic
06:51because really the only thing
06:51that we're gonna change is the degrees themselves.
06:54So for the minutes themselves,
06:56the minutes except the same exact type of logic.
06:59So the only thing I had to really change here
07:01is to change it from seconds to minutes
07:02to represent that.
07:04Now I'm gonna also go to the radius
07:06and I'm just gonna duplicate it by dot eight
07:08to make sure that my length is a little bit shorter
07:11than the length of the seconds.
07:13If I go ahead here and click on refresh,
07:15I should now see O2,
07:17oh, and I have to make sure
07:18that I actually call the stroke.
07:20So let me go ahead here
07:20and also add a stroke right below here
07:23once we're done with that logic.
07:25And let me just also put here a comment here
07:26that this is seconds
07:30and let's put a little comment here
07:32that this is minutes
07:35and in a second we're gonna create O2 of the hours.
07:38So let's go ahead here and click on refresh
07:40and we could see that now we have here the hours
07:42and we have here the seconds.
07:43Let's go ahead and also create the hours.
07:45We created the minutes and the seconds
07:47and the hours is literally the exact same type
07:49of basic logic.
07:51I'm just gonna copy everything from here.
07:54The only thing that we're gonna have to change here
07:55is that we're gonna wanna add inside of here
07:58a few modifications to our actual degree calculation.
08:02So first of all, let's bump up our width to be five
08:04and let's make it even smaller.
08:06So we're gonna make it in half of the radius,
08:08the actual area for our hours.
08:10Now the logic that we wanna put here,
08:12we wanna put here hours
08:13and contrary to our hours that are 12 of them
08:17and not 360 so we're gonna divide our 360 by 12
08:21to get 30 and then duplicate our hours by 30.
08:25Now just this is all I had to do
08:26to finish creating my clock
08:29but if I wanted to be even more accurate
08:31because the time right now is 12 and six minutes,
08:34my 12 dials shouldn't be exactly snap
08:37on the top there.
08:38It really should be a little bit in between
08:39on its way to the one o'clock.
08:41To do that, I'm gonna wanna create
08:43a little bit more logic here,
08:44basically taking that offset.
08:46So I really don't care about the seconds offset that much
08:50but I do care about the hours offset.
08:52So to be able to take care of the hours offset,
08:54I really want to add inside of here
08:56one more representation of an hour.
08:59To do that, I'm gonna go ahead
09:01and what I really wanna do is I wanna get a reference
09:03to that minute area
09:06and I wanna take that minute that we had there,
09:08divide that minute by 60 to be able to get the reference
09:12of how much is it, that value
09:14when I'm actually gonna have it inside of an hour.
09:17And then I would duplicate that value by 30 again
09:20or take that value and add it into our hours.
09:23So I could go ahead here
09:24instead of creating two duplications,
09:26just put that hour inside of a round bracket
09:30and then add inside of our minutes divided by 60
09:33giving us the actual hour and leftover
09:35that there is there before
09:36that we're duplicating the value by 30.
09:39By doing that, if I click here on refresh,
09:40we'll see that now the digit is gonna be tilted
09:43towards that hour, although it's early in the hour.
09:46As you check it, you'll see that it's gonna always move
09:48so every minute that will pass,
09:50every time the dial of the minute will move,
09:51the hour will move just a bit as well.
09:55In this lecture, we solidified everything we talked about
09:58in the previous lecture
09:59by creating a clock that is ticking.
10:02In the next lecture, we'll continue using cosine
10:04and sine and radians,
10:06but this time we're gonna move our attention
10:08to complex shapes and how could we take advantage
10:11of this logic to create even more complicated shapes.
