The canvas element is part of HTML 5 and it allows the rendering of 2D shapes and bitmap(also called “raster”) images.
A canvas actually has two sizes:
- the size of the element.
- the size of the element’s drawing surface.
The element’s width and height attributes set both the size of the element and the size of the element’s drawing surface. CSS attributes affect only the element’s size and not the drawing surface.
Example:
<!DOCTYPE html> <html> <body> <p>Image:</p> <img id="forest" width="220" height="277" src= alt="Forest"> <p>Canvas:</p> <canvas id="Canvas" width="300" height="200" style="border:15px solid #000066;"> Your browser not support the HTML5 canvas . </canvas> <script> window.onload = function() { var canvas = document.getElementById("Canvas"); var context = canvas.getContext("2d"); var img = document.getElementById("forest"); context.drawImage(img, 12, 8); }; </script> </body> </html> |
chevron_right
filter_none
Output:

Recommended Posts:
- How to auto-resize an image to fit a div container using CSS?
- Resize image proportionally with CSS
- How to Resize JPEG Image in PHP ?
- p5.js Image resize() Method
- How to save an HTML 5 Canvas as an image on the server ?
- HTML | DOM Style resize Property
- How to create a canvas image using Fabric.js ?
- How to crop an image using canvas ?
- CSS | resize Property
- How to Resize Rotation Radius using CSS ?
- Node Jimp | resize
- Resize an iframe based on the content
- Cross-browser window resize event using JavaScript/jQuery
- Nodejs | GM resize() Function
- How to wait resize end event and then perform an action using JavaScript ?
- How to create auto-resize textarea using JavaScript/jQuery ?
- Convert an image into grayscale image using HTML/CSS
- How to specify an image as a server-side image-map in HTML ?
- HTML | Canvas Basics
- HTML | canvas lineWidth Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


