Working with HTML5 Canvas
So far we've worked with pre-existing art and the limited amount of what we created ourselves was vector art. In this lesson, we'll crack into the world of HTML5 canvas. Because we don't have enough time to cover the many layers of working in Canvas this will be a rudimentary introduction. To learn more of canvas check out my book. There is a reason why I want you to know about Canvas. Throughout this chapter, it will help us understand how animating curves work.
Video Transcript
00:00This chapter is all about curves and lines and no better place to start from
00:06looking at the new HTML5 canvas and this is based off of my book and my book has
00:12over 400 pages it's I think I'm not sure well it's very close to 400 pages
00:16it's massive it's basically almost not almost it's like the Bible to how to
00:20how to draw things in code and how to create data blah blah blah check out the
00:26book if that sounds interesting when you're done with this chapter. Anyways
00:29let's jump in there I just want to show you how canvas works because it's gonna
00:32make our life easier when it comes to coding which might surprise you but
00:36actually no because we're getting into a really complicated section which all
00:42this course almost all the course is gonna be dedicated to Bezier curves and
00:46for us to really comprehend what they do we want to actually draw out the
00:51path where our animation is going so this is kind of going to be the ease
00:55into it and this chapter is gonna be a heavy one but a very rewarding one
00:59by the end of it. We're starting it off with a completely empty white slate
01:04page now if you go into the source code itself you're gonna see that I already
01:08loaded up our CSS for chapter 5 that currently is empty I've also loaded up
01:14jQuery I loaded up tweenlight I loaded up chapter 5 JavaScript file which is
01:19also kind of empty and last but not least I also loaded up our CSS plug-in our
01:26tweenlight CSS plug-in now the goal in the next few lessons and really in many
01:31ways the main subject of this chapter is to understand more deeply how things
01:36animate and how do we control those animations more than we have so far so
01:43to do that I'm gonna start with a really basic animation but for us to be
01:46able to do it I really want to have a really powerful visualization now by the
01:51way a lot of the visualizations that we're gonna do throughout this chapter
01:55are basically based off of my book on canvas which you could find a link to
02:00probably in below on this page and probably throughout the chapter which
02:04that book focuses on canvas and how to draw in general while this folk with
02:09this chapter is it courses in general about animation I wanted to dedicate a
02:13few a few I would say lectures on how to draw because it's gonna help you
02:19animate if you understand how to draw lines we're only gonna look into lines
02:22because that's gonna help us understand how could we also animate things in a
02:28direct line for example a diagonal something that has more of a curvature
02:34and we're gonna look into that right away all right so let's get started with
02:38a simple line and to do that I want to create a new canvas area and put a
02:42little bit of art in it in the canvas we're gonna draw our path and on the
02:48object itself we're gonna animate it so I'm gonna start off by basically what
02:52I literally want to start off with is I want to start off by creating that art
02:56element and the art element itself is going to be basically an image and I'm
03:01gonna give it an idea of me because it's actually gonna be a picture of me so
03:05you're gonna enjoy animating me but don't worry it's gonna be an
03:07illustration of my face and obviously influenced by you guessed it I'm sure
03:15have you well you'll find out in a second all right so that's our image
03:19if you go back into our browser and click on refresh now we should have my
03:22image in here which is basically me as a rabbit and with glasses and probably a
03:27mustache but I'm sure you could see the imagination because actually that's one
03:31of my real jackets and that's actually a real background or it used to be yeah I
03:37don't know why I'm excited okay because basically that was a photo that uh was
03:42it I think like LinkedIn I was walking in the street in New York and
03:46LinkedIn stopped me and said hey do you want a professional photo for your
03:49profile is like yeah yeah yeah so they took a photo but I personally like the
03:53photo away more as a rabbit all right so back to it and we'll continue so I'm
03:59gonna go ahead and I want to add my canvas now we're not gonna really
04:03talk deeply about canvas but this lesson is gonna really be about canvas
04:07what I want to do is I want to give my canvas an ID and I'm just gonna call it
04:11my canvas and I'm gonna go ahead and give it a width as well and I'm gonna
04:16set its width to be 800 and I'm gonna set its height to be 300 now if I go
04:23ahead and save this and go to the browser and click on refresh if you
04:25haven't worked with canvas you're not gonna see much besides that my image is
04:29now be getting pushed down because my canvas itself is really here but we
04:32just can't see it because nothing is inside of it so what I'm gonna do is I
04:36want to one thing is I want to make sure that they're both aligned to the
04:39top and their and their values are absolute and for that I'm gonna go
04:43ahead and into my CSS and I'm gonna just add for our me I want to add I want
04:49to add a rule and I also want to add a rule for my canvas but it's the same
04:54rule basically and the rule for both of them is gonna be that want to have your
04:57position that is absolute all right so that's gonna make sure that they're not
05:03gonna compete over space but they're gonna be one on top of each other so
05:07now both items are one on top of each other I could see already that I have a
05:10spacing here that I don't want but even before I fix it how about we add a style
05:14around our canvas so it won't be invisible so I'm gonna go ahead here and
05:18just add a another rule a few rules for my canvas and one is I want to set a
05:24border style style and I'm gonna set the style to be dotted and I'm gonna go
05:35ahead and set here my border color and I'll set the color to be some sort of
05:41a gray so let's go for like a mid-tone gray here one two three four five six
05:45and I'm gonna also set my width of my border and border and I'm gonna set it
05:51to be two pixels so if I say this go back into my application click on
05:55refresh now we should be able to see where our canvas is and we'll see that
05:58we have here an extra padding or an extra margin actually that we don't want
06:01so I'm gonna go ahead and tell my browser get rid of that padding that our
06:06body has so I'm gonna go or more accurately our margin so I'm gonna go
06:10to my body I'm gonna tell my body hey don't do any margin so margin 0px so that
06:16way we'll have both our items flushed to the top now next thing I'm gonna do is
06:21this lesson is gonna be dedicated to showing you how to actually create draw
06:25a line in canvas there's a few steps involved with it so now that we've
06:29created our stage I'm gonna go ahead and draw my canvas zone area and let's
06:35do it we're gonna go into our JavaScript into our script area and now
06:40that we are in our script area it's time for us to actually approach our
06:43canvas and I'm gonna approach both items so first of all I want to approach me
06:47which is gonna be my image and I'm just gonna go ahead and fetch it with the
06:51jQuery which is it has an ID of me next thing is I want to fetch oto the canvas
06:58but I want to actually fetch the canvas itself could draw in 2d 3d and maybe
07:03in other versions and things as well in the future but really what I want to
07:07say is I want to get that the API for my canvas that is 2d now to do that I have
07:13to approach my and I'm gonna just call this context because that's the
07:16terminology and I'm gonna go ahead and we go my document and I'm gonna ask for
07:21that element by calling get element by ID and I'm gonna get my canvas so
07:27that's gonna give me my canvas and then from directly from my Dom I'm gonna
07:31ask to get the context and what context do I want I want a 2d context context
07:38which basically means that the API for 2d drawing is gonna be sent into this
07:42context function if I wanted a different API I would get a different API so
07:47really there's two layers here one is we're fetching our ID of our element
07:50very similar to the way we did with jQuery but if we would have done it
07:54with jQuery we wouldn't have direct access to the actual method we would
07:58have to dig deeper into it to gain access to it because jQuery gives us a
08:01wrapper in our case we don't need a wrapper we want to have access to the
08:05function that is associated with our canvas that enables us to get its
08:09context so we're not even using jQuery and the state set scenario and the next
08:16step is we're actually telling it hey I want to get the specific context that
08:19enables me to draw in 2d and then I get that into that variable now we're
08:23gonna we talk a lot about this in my in my book about HTML5 graphing and data
08:29visualization which is literally just a book about canvas and a book about how
08:33to visualize data how to take data and turn it into something visual it's a
08:38perfect complement to this course if you are into data and you're into
08:42drawing as well programmatically drawing but let's continue so now that I've
08:47set my context and I got it I could start giving it directions so I could go
08:51to my context and say hey I wanted to give my strokes a specific style so I'm
08:56gonna call the stroke style and I'm gonna set a stroke style to it so I'm
09:00gonna give it a some gray one two three four five six so you have your nice
09:04little gray the next next item I could I need to move my pointer because by
09:08default as soon as you start drawing because actually it draws things
09:12directly onto the stage as soon as you start drawing our dot is gonna be at
09:16the zero zero position and the zero zero position grows you know our Y grows to
09:22this direction our X grows into this direction so I want to basically just
09:26move it to somewhere in the center of our stage so to do that I'm gonna I'm
09:30just gonna call the command context move to and I'm gonna say I want to move
09:35you to position 100 150 which basically would mean I'm moving it basically 100
09:41pixels in and the Y is gonna be in the center because our total height is
09:45300 pixels so I'm basically centering off centering this line that I'm gonna
09:50create immediately right now by calling the line to and with the large I'm gonna
09:56say to where do I want to draw it in this case I want to draw it to 700 150
10:00that means I'm sticking to the same Y position and I'm just making a 600 pixel
10:05length line now before we could actually see it the last thing that we
10:08need to do is also set our context dot stroke and that will be basically draw
10:16basically draw it if I don't put that in and I go back into my application there
10:20is nothing now by the way once I created it once I created the stroke there is
10:25no way to access it anymore it's baked it's baked in just like an image and
10:30it's saved and it's baked in and that's it it exists contrary to vector art and
10:35by the way we see here that I wrote your stroke instead of stroke so let's
10:38fix that stroke and give it one more chance and here we go now we have our
10:44line just the way we wanted it to be now again so I was saying a big difference
10:48between CSS and SVG and a lot of other and I'm drawing at things enables to draw
10:56in vector canvas enables enables us to draw in bitmap which means once we draw
11:01it that's it it's not accessible again we can't modify it the only way to modify
11:05is by deleting the whole screen and drawing it in again now the next thing
11:10that I want to do is I want to go ahead and I want to first of all I'm
11:13gonna do two things one is I want to show you also how could you animate
11:16something that is hard-coded that is not technically animatable right now we're
11:22gonna do that in the next lesson before we move on to more advanced topics but
11:25what I really want to do right now is position my element to be exactly in the
11:30center point of this line now because both of them are sitting in the same
11:33scope in the same I would say this the CSS works with the left right top
11:40left and the HTML or canvas works with X and Y but what I could do is literally
11:46just go ahead and offset the image because right now my problem is is this
11:50image its exposition is in the top here I want to create that offsets I know what
11:55size is the height and width that I have to offset so I could align it to
11:58be exactly in the same spot now if I just go ahead and I'm just gonna show
12:02that to you if I just go ahead and I just go to my tween library and I go
12:06and I say tween light set and I'm gonna say take me and I want to basically set
12:13a CSS rule and I want to say hey from the top position I want you to be
12:18basically 150 because that's basically the value that we had there and from
12:22our left I want to set the value to be 100 because that's the value that we
12:28had above here if I just go ahead and click on save save notice what is
12:32gonna happen it's more or less gonna be in the right spot but not really
12:36because we have this extra offset that we need to think about when we're doing
12:39these type of stuff so to sort this out what I want to do is I want to go back
12:44and create an offset variable here so I'm actually gonna go right ahead up
12:48here and what I want to do is I want to create a few offset variables so I'm
12:52gonna go ahead here and I'm gonna create a new variable and I'm gonna call
12:55this offset with and the offset width is gonna be basically me with divide by
13:03two oh it's a function divide by two and we want to divide it actually by minus two
13:09because we want to turn it into a negative or we could just set here
13:11minus all that value that we got back same will go for our height I'm gonna
13:16offset height that's important the reason why I'm showing this to you is
13:20because you will find out a lot of times that you forget to think about offsets
13:25and then your animations are not gonna look the way you wanted them to and
13:28they get quite complicated if you don't think about it so now that we
13:33created our offset and we have our offset width then we want to offset our
13:37width by adding that into our left property and for our height we want to
13:42add that into our top property and by doing that we should get our element
13:46snapping or sitting exactly in the center of our line that we created so
13:51just to wrap up this video I just want to animate the element from this
13:55point all the way to the end here and to do that is quite simple now
13:58that we got the concept of offsetting all that's left for us to do is
14:01literally just go ahead here and I'm just gonna copy this and I'm gonna
14:05create here a 2 animation it's gonna take two seconds and last but not least we
14:13also want to set a new position which was similar to the one we had here which
14:17was 700 with that offset and we're sticking to the same spot I'm gonna
14:22animate it even though we're not really animating anything right now just
14:25for the sake of illustration and if we go ahead and click on refresh we
14:28should basically if there's no errors we should see that our element is
14:31going to animate from point 1 to point B now this isn't a crazy advance yet but
14:35throughout the next few lectures this is going to get more complex and more
14:39complex as we look at different types of animations so that's it for our first
14:44lecture as we integrate a little bit of canvas into our animation work in the
14:48next lesson what I really want to do is I want to show you how could we also
14:51animate that canvas line and what's the process involved with that so we'll
14:56see you in the next lesson when we do that
