Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The drawImage() method lets you draw all or part of an image, anywhere inside a canvas, and it lets you scale the image along the way. You can also draw into an offscreen canvas, which lets you do clever things with images, such as panning an image, or fading an image into a canvas. We discuss several uses for offscreen canvases in this chapter.
Let’s start by drawing an image into a canvas, as shown in Figure 4.2 and listed in Example 4.1.
Figure 4.2. Drawing an image into a canvas
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
image = new Image();
image.src = 'fence.png';
image.onload = function(e) {
context.drawImage(image, 0, 0);
};