
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to use images with HTML5 canvas?
The HTML5 <canvas> tag is used to draw graphics, animations, etc. using scripting. It is a new tag introduced in HTML5. To use images with HTML5 canvas, use the drawImage() method. This method draws the given image onto the canvas.
You can try to run the following code to learn how to use images with HTML Canvas. Here, the image is a reference to an image or canvas object. x and y form the coordinate on the target canvas where our image should be placed.
Example
<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> function drawShape() { // get the canvas element using the DOM var canvas = document.getElementById('mycanvas'); // Make sure we don't execute when canvas isn't supported if (canvas.getContext) { // use getContext to use the canvas for drawing var ctx = canvas.getContext('2d'); // Draw shapes var img = new Image(); img.src = '/images/backdrop.jpg'; img.onload = function() { ctx.drawImage(img,0,0); ctx.beginPath(); ctx.moveTo(30,96); ctx.lineTo(70,66); ctx.lineTo(103,76); ctx.lineTo(170,15); ctx.stroke(); } } else { alert('You need Safari or Firefox 1.5+ to see this demo.'); } } </script> </head> <body onload="drawShape();"> <canvas id="mycanvas"></canvas> </body> </html>
- Related Articles
- Blending two images with HTML5 canvas
- How to use SVG images in HTML5?
- How to use multiple click event on HTML5 canvas?
- Why to use canvas tag in HTML5?
- How to allow cross-origin use of images and canvas in HTML?
- How to actually work with HTML5 Canvas camera/viewport?
- How to handle mousedown and mouseup with HTML5 Canvas
- How to draw a Bezier Curve with HTML5 Canvas?
- Drop Shadow with HTML5 Canvas
- How can I use the HTML5 canvas element in IE?
- How to use GoJS HTML5 Canvas Library for drawing diagrams and graphs?
- How to center canvas in HTML5?
- Create a pattern with HTML5 Canvas
- Draw Bezier Curve with HTML5 Canvas
- Draw a shadow with HTML5 Canvas

Advertisements