Drawing the Image Window
To draw the image window, we will simply modify the standard context.drawImage()
function call using the
values in the four variables we just defined:
context
.
drawImage
(
photo
,
windowX
,
windowY
,
windowWidth
,
windowHeight
,
0
,
0
,
windowWidth
,
windowHeight
);
Let’s take a closer look at this for a refresher on how the
drawImage()
function operates. The
values are passed in order:
photo
The image instance we are going to use as our source for painting onto the canvas
windowX
The top-left
x
location to start copying from the source imagewindowY
The top-left
y
location to start copying from the source imagewindowWidth
The width of the rectangle to start copying from the source image
windowHeight
The height of the rectangle to start copying from the source image
0
The top-left
x
destination location for the image on the canvas0
The top-left
y
destination location for the image on the canvasviewPortWidth
The width in pixels for the destination copy (can be modified to scale the image)
viewPortHeight
The height in pixels for the destination copy (can be modified to scale the image)
When we draw from the image to the canvas, we will be modifying
the windowX
and windowY
values to create a panning effect.
Example 4-11 demonstrates
how to get the image onto the canvas with the window location set to 0
,0
. Figure 4-12 shows an example of
the output for Example 4-11.
Example 4-11. Placing an image on the canvas in a logical window
var
photo
=
new
Image
();
photo
.
addEventListener
(
'load'
,
eventPhotoLoaded ...
Get HTML5 Canvas, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.