02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is

scale, rotate and translate

To wrap up this title we are going to cover three last major methods that manipulate the full canvas.

Keynotes:

  • scaling.
  • rotating.
  • translating.

Video Transcript

00:00Transformations are really scaling, rotating, and translating all done at the same time.
00:05This lesson we're going to break down those steps and do each distinct item separately
00:10and literally go through scaling, rotating, and translating with their distinct functions
00:15when we want to just do one or the other on their own. And this is the last lecture of this title,
00:20so let's jump right into it. Let's jump right into it and I'm going to go right into the source
00:26area. I created the create fin, which in French means end, which relates to our title lesson really,
00:34really closely, obviously. So I already pre-configured a width, height, context, and rad for radius,
00:40and I'm going to go right ahead to show you a few more manipulations you could do to the canvas
00:44directly. As I will probably want to go back to the original state, I'm going to go ahead and call
00:49the context save because I want to get back to the original point of view at some point.
00:55I'm going to go ahead and define a new font and set it to be 120 pixels and a serif,
01:00just like we've seen previously. And I'm going to go ahead and literally go ahead and do a little
01:05bit of alignment here, text align, set it to the center, and I'm going to go ahead and before I
01:14even fill my content, I want to show you the first method. And the first method is called
01:20translate. Now translate works this way, and even before I do, let me go ahead and show you.
01:25If I go ahead to my context and I fill my text, and I send into it the words fin,
01:31and I want to position it, let's say, at 00. Notice what will happen. Well, not content,
01:38content. What will happen is because I'm positioning at position 00, well, it is out of our bounds
01:44because you can see that a little bit of it. If you have a really good site, you'll see a
01:47little piece of that dot right there. But what I really want to happen is I want this content
01:52to be in view. And yes, I could go ahead and manipulate the x and y coordinates, but there's
01:56one more way I could do it. Before calling that fill text, I'm going to go ahead and call the
02:02translate function. The translate function literally moves the 00 point to wherever we want to.
02:08In this case, I'm going to decide to move the 00 point to half of the width and half of the
02:13height. Once I've done that, if I go ahead and run my application one more time, you'll see that
02:21this time around, our copy is going to be slapped in the center, although aligned upwards from the
02:26center. The next thing that I want to do, I want to show you a few more things that we could do
02:31by manipulating the canvas itself. And I'm going to go ahead and right after we're done here,
02:36I'm going to go ahead and call the context again. But this time, I'm going to call a
02:40method called scale. The scale method accepts two parameters that will scale the horizontal
02:46and vertical axis or the x and y coordinates. And I'm going to go ahead and define it as 90%
02:52of what it was before scaling our content downwards. Everything that was already there
02:57before won't change. But now if I go ahead and call the fin function again, and I fill my text
03:03one more time, and let me even before I do that, let's define here a styling. So I'm going
03:09to go ahead here and call the context, I'm going to call fill style, and I'm going to set it to be
03:14let's set it to be a nice red, a nice sharp red. If I go ahead and click here on save,
03:20notice what's going to happen, our content is going to be resized, because every new content
03:26is now going to be 90% of what it was before. Beyond that, if I go ahead here, I want to
03:31explain another important point. If I go ahead here and duplicate this, this assignment,
03:36and do it one more time, only this time set it instead of red, I'm going to set it to blue,
03:42sorry, green RGB. And let me also do it for blue. Now, the really important factor that I wanted
03:48you to notice here, first of all, if I go ahead and do this, and I save this, and I go on and
03:52I click on refresh, you'll see that each time it's going to be slightly smaller by 90 by 10%.
03:58Beyond that, if I wanted once to decide, hey, in the last item, I actually wanted to be the
04:02original, that is not the original, all I'm doing here is scaling it to the scale that it's in,
04:07there is no recollection of the scale that the item is in. The only way for me to revert back to
04:13the original scale is by me going and actually restoring back my point by calling the restore.
04:19In this case, I don't want to, I like the fact that we're getting smaller. The next item I want
04:24to show you is I'm going to go ahead here and another function, and the last function that really
04:29we're going to see on this title, which is called rotate. Now rotate works very similarly, only it
04:34accepts radians, and it's a radians between zero and 360. So in this case, let's say I want to create
04:41a five degree rotation for the second text item. And I want to do that another five degrees,
04:47and another five degrees for each item that comes in. If I go ahead and click here on refresh,
04:52you'll see that now each item from the second item and onwards are going to be tilted. If I
04:57wanted the first item to be tilted as well, I could go ahead before I actually fill out that
05:02text and go ahead and also rotate everything. And now all the content will come in rotated
05:07every single one, every single item, every single element. Last but not least, I want to just go
05:12back and complete everything by a reminder saying that when we want to restore, we just call the
05:17restore. And now everything is back to the original configurations, which I could now decide,
05:22let me go ahead. And I want to complete this nicely. So I'm going to go ahead here and take
05:27this copy. And I want to make it I want to restyle my font to be much smaller, let's make it 20.
05:33I'm going to check take my fill style and make it black to complete our application.
05:39And last but not least, I'm going to say goodbye by typing here context, fill text.
05:48Goodbye. And I'm going to now need to position it in absolute positionings as we've had before.
05:54So let me go ahead here and I'm just going to add about 40 pixels, I believe maybe even 45.
06:00Let me add your 45 so or 46. Why not? And that's it. Goodbye. So I hope you enjoyed this title.
06:08In this lesson, we completed our manipulations of canvas and with it,
06:12you could now pride yourself as someone that masters HTML canvas.
06:16I hope you had a really, really great time. And please check more of my titles with
06:20publishing by searching my name Ben Fala on the packed site.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!