On an <canvas>, images are painted with drawImage(). Sources can be an HTMLImageElement, another canvas, a video frame, ImageBitmap, and more.
Two common workflows: geometry (place, scale, crop with drawImage) and pixels (read/edit RGBA with getImageData / putImageData). Always wait for external images to load before drawing.
Baseline · Widely available.
drawImage, getImageData, and putImageData are stable Canvas 2D features across modern browsers since about July 2015.
Security · Tainted canvas.
Drawing a cross-origin image without CORS permission taints the canvas. Then getImageData, toDataURL, and toBlob throw. Prefer same-origin assets or set crossOrigin = "anonymous" when the host allows it.
Write back with putImageData — yellow becomes dark blue-ish, blue becomes yellow-ish.
Edge Cases & Pitfalls
Most image bugs are load timing, CORS, or confusing the crop arguments.
load
Draw after load
For new Image() / <img>, call drawImage in the load handler (or after decode()). Drawing too early often paints nothing.
cors
Tainted canvas blocks pixels
Cross-origin images without CORS break getImageData / export. Use same-origin files or proper crossOrigin + server headers.
args
9-arg order matters
Source crop first (sx…sHeight), then destination (dx…dHeight). Swapping them crops the wrong region.
size
Bitmap vs CSS size
HTML width/height set the bitmap. CSS-only sizing scales the display and can blur images.
smooth
Scaling quality
imageSmoothingEnabled (default true) softens scaled images. Set false for a pixelated look when zooming sprites.
Compatibility
Browser Support
drawImage and ImageData APIs are part of Canvas 2D in every modern browser.
✓ Baseline · Widely available
drawImage & ImageData
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support placing, scaling, cropping, and pixel read/write. Internet Explorer 9+ supports the core methods.
98%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetAll modern
Full support
Internet Explorer9+
Supported
drawImage / getImageDataWidely supported
Bottom line: Safe for production image drawing and same-origin pixel edits on Canvas 2D everywhere modern browsers run.
Remember
Key Takeaways
Place / scale / crop: 3-, 5-, or 9-arg drawImage.
Pixels:getImageData → edit RGBA → putImageData.
Wait for load when using URL-based images.
CORS: tainted canvas blocks pixel reads and exports.
One line: ready source → drawImage → optional pixel loop.
UA style
Default CSS Settings
Image drawing is a Canvas API concern. The <canvas> element still uses typical user-agent CSS:
default.css
canvas {
display: inline;
}
Set bitmap size with HTML width/height. CSS-only sizing scales the bitmap and can blur drawn images.
Frequently Asked Questions
Load or create a source (HTMLImageElement, another canvas, video frame, etc.), wait until it is ready, then call ctx.drawImage(source, dx, dy) or a scaled/cropped overload.
3 arguments place the image; 5 arguments also set destination width/height (scale); 9 arguments crop a source rectangle then draw it into a destination rectangle.
Cross-origin images without CORS permission taint the canvas. Same-origin or CORS-allowed sources (and crossOrigin = "anonymous" when the server allows it) keep pixel reads working.
drawImage the source, getImageData, average each pixel’s R/G/B (or use a weighted average), write those values back, then putImageData.
Yes. HTMLCanvasElement is a valid drawImage source. Paint the source canvas first, then drawImage it onto the target.
Always wait when using Image() or <img> with a URL. Drawing before load often does nothing; older browsers may throw.