Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Applying a custom transformation

From: HTML5: Graphics and Animation with Canvas

Video: Applying a custom transformation

The last transform we're going to take a look at is the custom transform. Now in addition to the transforms that are built into the canvas, you can also define your own. Transforms are defined as a matrix. A matrix has the format that you see here. There are six variables. There is a, b, c, d, e, and f. And to use a transform, a new point is found by applying that transformation matrix to each point in the image that's being transformed. Now, you don't need to panic.

Applying a custom transformation

The last transform we're going to take a look at is the custom transform. Now in addition to the transforms that are built into the canvas, you can also define your own. Transforms are defined as a matrix. A matrix has the format that you see here. There are six variables. There is a, b, c, d, e, and f. And to use a transform, a new point is found by applying that transformation matrix to each point in the image that's being transformed. Now, you don't need to panic.

I'm not going to make you learn matrix math, but this does come in handy when you want to apply a transformation that is not built into the canvas. So, for example, canvas gives you transforms for rotation, scaling, and translating, but there are plenty of other transforms out there that you might want to use, and since they are not built in, you would have to use a matrix like this to define one. So we'll see examples of that in a moment, but the way that you apply a custom transform is by using one of the two transform functions. The first one is simply called transform, and it takes these six arguments, which correspond to the variables you see in this matrix here.

The transform function takes the transform you're defining here and adds it to whatever the current transform of the canvas currently is. So if the canvas already has a transform, like a rotation or a scale or a resulting transform from a series of transforms that came before it, then this one will simply be added to that one, because remember, transforms are additive. The second one is called setTransform, and it takes the same six arguments. The main difference between setTransform and transform is that setTransform resets the canvas to be what's called the identity transform, which is a transform that just doesn't do anything.

So in other words, it erases all the previous transforms and then applies the one you're defining here. So this one is additive. This one is not additive. This one resets the canvas and then apply the transform that you're defining. Let's take a look in the code to see how this stuff works. So here I am in the code. I've got my snippets file open, and I'm at the custom transform area, so let's open up our custom transform example. It's called transform_start.

Let's do something simple that we've already seen before. So, let's first copy these two lines, and we'll paste those in. Now, you've seen this already. Basically, we just take a blue rectangle and put it on the canvas, so let's just make sure that that works, and it does work. That's good. All right, so let's go back to the code. What we're going to do now is recreate the translate transform using our custom transform. So we'll copy this and we'll paste it over, and I'll explain how it works.

So a translation matrix for a translate transform looks like this. Those six variables are right here. And the first row is 1 0 and then the amount you want to translate the x by, and then the second row is 0 1 and the amount you want to translate y by. So remember, the variables go in the order of a, b, c, d, e, and f. So let's define that transform. We're going to make the new rectangle red, so that's the fillStyle there. Just like in the translate example, we're going to translate the rectangle to the center of the canvas.

So tx is going to be the width of the canvas divided by 2. The y will be the canvas height divided by 2-- that gives us the middle of the canvas. And then instead of using the translate function, we're just going to call the transform function, and we pass in 1, 0-- that's these two right here--then 0, 1--that's these two right here--and then tx and then ty. That will add this translate transform to the canvas, then we'll fill the rectangle, and then we'll call the reverse transform, which translates us back to the upper left of the canvas--just in case we had any drawing that came after this.

So we save and then let's go to the browser and let's refresh. And you can see that the blue rectangle is drawn at 0, 0. The red rectangle is also drawn at 0, 0, but the new origin has now been translated to right there. So that seems to have worked fine. Let's go back to the code. So you can see right there, even though the rectangle is placed at 0, 0, because of the translation, the origin was moved to the middle of the canvas. Let's create a new transform. So now we're going to go back to snippets, and we're going to copy this example.

Okay, so we copy and we'll come over here and we'll paste. In this example, we're going to create what's called a skew transform. Now a skew transform--sometimes it's called a shear transform--and in this case we're going to have a green rectangle and we're going to shear the objects using various directions x and y. So, a skew transform is defined by the number 1 and then the amount you want to skew in the y direction, then the amount you want to skew in the x direction, the number 1 here, and then these two are left at 0.

So we're going to use a green rectangle for this one. And the skewing factor is defined by a multiplication factor, so I'm going to multiply by 0.2 in the x direction and I'm going to leave the y direction alone. Now in this case, I'm going to use the setTransform function, and remember, this resets whatever the transform is that's currently on the canvas. So, I don't have to worry about any transforms that came before me. And then we're going to fill the rectangle at this location, the point 250 by 20, and it will be 100 wide and 50 high.

So let's save, and let's go back to the browser and let's refresh. You can see now that I've drawn a green rectangle that's been skewed in the X axis direction, so it's been sheared a little bit. Let's make some changes and see how that works. Let's go back to the code. I can increase the amount of shear. I can say this is 0.4, and let's refresh the browser now. You can see that the shear amount got a little bit more. All right, let's try it in the y direction. So I'll set this to 0, and we'll make this 0.3 and we'll save and we'll refresh.

And you can see that this time the rectangle got sheared in the Y axis. Now what we have done here is create a new transform that is not built into the canvas. And just for good measure, let's make sure it works in other browsers. So we'll bring this up in Firefox, and you can see, the effect is the same. Let's bring it up in Chrome, and you can see the effect is the same there, too. So what we have learned here is how to use the custom transform matrix to recreate a transform we already know how to use, but we also saw how to create a new transform that's not built into the canvas.

You can look up all kinds of transform matrices on the Internet and just plug in the values into that transform function. And doing so, you can create transforms that are not built into the canvas that you can use in your own drawings.

Show transcript

This video is part of

Image for HTML5: Graphics and Animation with Canvas
HTML5: Graphics and Animation with Canvas

37 video lessons · 18488 viewers

Joe Marini
Author

 
Expand all | Collapse all
  1. 4m 54s
    1. Welcome
      1m 3s
    2. Using the exercise files
      2m 3s
    3. Using the HTML5 Canvas element in the real world
      1m 48s
  2. 10m 31s
    1. Real-world example: CanvasMol
      2m 9s
    2. Real-world example: Raphaël-JavaScript Library
      1m 47s
    3. Real-world example: The Wilderness Downtown
      4m 1s
    4. Real-world example: Sketchpad
      1m 10s
    5. Real-world example: Pirates Love Daisies
      1m 24s
  3. 3m 28s
    1. Installing the tools
      1m 29s
    2. Exploring the Canvas examples used in this course
      1m 59s
  4. 8m 58s
    1. Introducing the Canvas tag
      6m 30s
    2. Understanding the differences between Canvas and SVG
      2m 28s
  5. 5m 36s
    1. Identifying the Canvas element's methods and properties
      1m 40s
    2. Using the Canvas drawing context
      3m 56s
  6. 43m 14s
    1. Setting and using colors and styles
      3m 19s
    2. Drawing basic shapes: Rectangles and lines
      10m 21s
    3. Understanding the Canvas state
      5m 15s
    4. Drawing complex shapes: Arcs and paths
      9m 15s
    5. Drawing complex shapes: Bézier and quadratic curves
      5m 46s
    6. Rendering text
      9m 18s
  7. 32m 35s
    1. Creating shadows
      6m 41s
    2. Drawing with patterns
      7m 20s
    3. Drawing with gradients
      6m 18s
    4. Using clipping paths
      4m 46s
    5. Drawing images and video
      7m 30s
  8. 35m 42s
    1. Transforming objects using the translate tag
      4m 18s
    2. Scaling objects with the scale transformation
      4m 7s
    3. Rotating objects with the rotate transformation
      4m 33s
    4. Applying a custom transformation
      6m 58s
    5. Compositing in Canvas using globalAlpha
      6m 36s
    6. Manipulating raw pixels
      9m 10s
  9. 41m 23s
    1. Building an image slideshow control
      4m 24s
    2. Using smooth transitions in a slideshow
      4m 28s
    3. Creating a basic animation
      5m 42s
    4. Creating animation with double buffering
      13m 13s
    5. Incorporating Canvas into a real page
      13m 36s
  10. 48s
    1. Goodbye
      48s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5: Graphics and Animation with Canvas.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.