
- 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 save canvas data to file in HTML5?
A Canvas is just a rectangular area on the HTML page. We can draw graphics in this rectangular area (Canvas) with the help of JavaScript.
Canvas can be created in HTML5 as −
<canvas id = ”canvas1” width = ”250” height = ”150”></canvas>
This creates an empty canvas with name canvas1 with width=200 and height=100.
To draw graphics in it, we use JavaScript −
var canvas = document.getElementById("Canvas1"); var ctx1 = canvas.getContext("2d"); ctx1.moveTo(0,0); ctx1.lineTo(300,200); ctx1.stroke(); // This method actually draw graphics as per context object
To save this graphic, we need to save it as some data url like img.png or img.jpg
For this, we will write −
var imgurl= canvas.toDataURL( ) ; / / This method saves graphics in png document.getElementById(‘cimg’).src = imgurl; // This will set img src to dataurl(png) so that it can be saved as image.
In this way, we can save canvas data to file in HTML5.
- Related Articles
- How to save HTML5 canvas data to file?
- How to save and restore the HTML5 Canvas states?
- HTML5 Canvas to PNG File
- How to draw an SVG file on an HTML5 canvas?
- What are save and restore methods in HTML5 Canvas?
- How to save an R data frame as txt file?
- How to center canvas in HTML5?
- Cross-origin data in HTML5 Canvas
- How to save a canvas as PNG in Selenium?
- How to detect point on canvas after canvas rotation in HTML5
- Drawing an SVG file on an HTML5 canvas
- How to draw an oval in HTML5 canvas?
- How to apply antialiasing in HTML5 canvas drawImage()?
- How to draw lines using HTML5 Canvas?
- How to use images with HTML5 canvas?

Advertisements