Canvas drawImage: Projecting Pixels onto a Canvas

drawImage is a versatile projector for your canvas, letting you place, scale, and even slice images before drawing them. It's used for rendering game sprites from a sprite sheet, displaying video frames, or compositing images.
Why it exists
The Canvas API needs a way to render existing pixel data—from images, videos, or even other canvases—onto the drawing surface. Instead of multiple specialized methods, drawImage provides a single, powerful interface for positioning, scaling, and clipping any image source.
The mental model
Think of drawImage as a digital projector and the canvas as your screen. The 'image' argument is your slide. The simplest call projects the whole slide onto the screen. More complex calls let you resize the projection or even zoom in on a specific part of the slide before projecting it. This is perfect for sprite sheets, where one image contains many smaller graphics.
How it works
The drawImage method has three main forms, or overloads, for different levels of control.
First, drawImage(image, dx, dy) simply draws the entire source 'image' at its original size onto the canvas at coordinates (dx, dy).
Second, drawImage(image, dx, dy, dWidth, dHeight) draws the entire 'image' but scales it to fit the specified destination width and height.
Third, drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) is the most powerful. It extracts a rectangular slice from the source image (defined by the 's' arguments) and draws that slice into a destination rectangle on the canvas (defined by the 'd' arguments), scaling it in the process.
When to use it
Use drawImage whenever you need to render an existing image-like object onto a canvas. It is essential for 2D game development for rendering characters and tiles from sprite sheets, for video effects by painting individual frames to a canvas, and for building image editors that composite multiple layers.
When not to use it
Do not use drawImage for creating primitive shapes like rectangles, lines, or circles from scratch. The canvas context has dedicated, more efficient methods for that, such as fillRect(), stroke(), and arc(). Use drawImage for rendering existing pixel data, not for generating new vector graphics.
One canonical example
A common use case is drawing a character sprite from a sprite sheet. Imagine a 128x32px sheet with four 32x32px animation frames in a row. To draw the second frame onto the canvas at position (50, 50), you would use the 9-argument version: ctx.drawImage(spriteSheet, 32, 0, 32, 32, 50, 50, 32, 32). Here, the source rectangle starts at x=32, y=0 with a size of 32x32, capturing just the second frame.
Interview question
Which scenario best demonstrates the unique power of the drawImage method?
- a.Extracting a character from a sprite sheet and scaling it to fit a specific area.Correct
- b.Filling a complex shape defined by a series of path commands.
- c.Drawing a solid blue square at specific canvas coordinates.
- d.Creating a smooth, curved line connecting multiple points.
Why? this is the answer
The drawImage method is uniquely designed for rendering existing pixel data, such as slicing a portion of a sprite sheet and scaling it. Other options describe creating vector graphics or primitive shapes, for which the canvas API provides dedicated, more efficient methods.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #canvas
- #web apis
- #graphics
- #javascript
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles