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:
photoThe image instance we are going to use as our source for painting onto the canvas
windowXThe top-left
xlocation to start copying from the source imagewindowYThe top-left
ylocation to start copying from the source imagewindowWidthThe width of the rectangle to start copying from the source image
windowHeightThe height of the rectangle to start copying from the source image
0The top-left
xdestination location for the image on the canvas0The top-left
ydestination location for the image on the canvasviewPortWidthThe width in pixels for the destination copy (can be modified to scale the image)
viewPortHeightThe 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
varphoto=newImage();photo.addEventListener('load',eventPhotoLoaded ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access