How to Use the Transformations Feature in Your HTML5 Game
Transformations are math operations that can be applied to any drawing or image to change the appearance. There are three major transformations:
translation: Moves a particular amount in X and Y.
rotation: Rotates around a particular point.
scale: Changes the size of the drawing in X and Y.
The canvas element allows all these operations on any type of drawing. However, the way the canvas element does this gets a little closer to math than you may have gotten before. Transformations in the canvas element can be hard to understand until you understand a little about how they really work.
How to perform transformations on a game object
In math, you don’t really transform objects. Instead, you modify the coordinate system and draw your image in the newly transformed coordinate system. It’s common in a vector-drawing application to have several hidden coordinate systems working at once. That’s important, because it’s the way canvas transformations work. Essentially, when you want to perform transformations on an object, you do the following:
Announce the beginning of a temporary coordinate system.
The main image already has its own coordinate system that won’t change. Before you can transform anything, you need to build a new coordinate system to hold those changes. The save() command indicates the beginning of a new coordinate system definition.
Move the center with translate().
The origin (0, 0) starts in the upper-left corner of the canvas by default. Normally, you’ll build your transformed objects on the (new) origin and move the origin to place the object. If you translate (50, 50) and then draw an image at (0, 0), the image will be drawn at the origin of the temporary coordinate system, which will be at (50, 50) in the main canvas.
Rotate the coordinate system with rotate().
The rotate() command rotates the new coordinate system around its origin. The rotation parameter is a degree in radians.
Scale the coordinate system in X and Y.
You can also alter the new coordinate system by applying X and Y scale values. This allows you to create stretched and squashed images.
Create elements in the new coordinate system.
After you’ve applied all the transformations you want, you can use all the ordinary canvas drawing techniques. However, these drawings will be drawn in the virtual coordinate system you just made, not in the canvas’s main coordinate system.
Close the temporary coordinate system.
Generally, you’ll want to apply different transformations to different parts of your canvas. When you’re finished with a particular transformation, use the restore() command to close out the new coordinate system. All subsequent drawing commands will use the default coordinate system of the canvas object.
How to transform a game image
It can be hard to understand how mathematical transformations work because they seem so simple on the surface. Build a program to see how this all fits together.
This program does the normal canvas setup and then creates a transformation that translates the image to the center of the canvas, rotates the image, and changes the image’s size:
Create a page with a canvas.
Normally, simpleGame will create the canvas for you, but in this case, the canvas element is made by hand.
Do all the normal setup stuff.
This involves the regular housekeeping: getting access to the canvas and its context and creating the image.
Begin a new coordinate system.
The save() command doesn’t really save anything. It indicates the beginning of a new coordinate system. Any drawing commands that occur between this save() statement and the matching restore() will follow transformation functions.
Translate the new system.
Move the coordinate system to (100, 100), which is the center of the canvas.
Rotate the new system.
Rotate the image by pi / 4 radians, which is 45 percent.
Scale the new system.
Multiply the X values by 3 and the Y values by 1.5.
Draw an image.
Because this image is drawn inside a save() / restore() block, it’s drawn with the transformations intact.
End the subsystem with restore().
The restore() command closes up the temporary coordinate system so all subsequent commands will refer to the parent coordinate system.
Draw a red rectangle in the default system.
The red stroked rectangle is drawn outside the normal coordinate system, so it’s not scaled or rotated.
The main design of the Sprite object is an image surrounded by a transformation. When you create a sprite, it builds the image object, and it defines a transformation with translation, rotation, and scale.
As you manipulate the position, angle, and speed of the sprite, you’re really simply changing the values sent to the transform. The image is offset so the (x, y) properties of the sprite specify the center of the sprite. That way, sprites rotate around their center, which gives a more natural appearance.