
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to capture HTML Canvas as gif/jpg/png/pdf with JavaScript?
We can Use the canvas.toDataURL() to capture HTML canvas as different file formats, such as gif, png, jpg, webp, etc.
Capture HTML Canvas as png
Example
Herein, after using canvas.DataURL(), set the type to image/png to allow saving the image as PNG −
<!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="300" height="170" style="border:2px solid #d3d3d3;"></canvas> <script> window.onload = function() { var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); context.fillStyle = "red"; context.fillRect(50, 50, 100, 100); window.location = canvas.toDataURL("image/png"); } </script> </body> </html>
Output

Right-click in the canvas and you will get an option to save it -

Click Save image as… and now you can save the image. Let us save it on the Desktop as mycanva.png. Under Save as type:, PNG Image is visible because in the code, we wrote image/png −

Capture HTML Canvas as webp
Example
Herein, after using canvas.DataURL(), set the type to image/webp to allow saving the image as WebP. WebP is an image file format developed by Google projected as a replacement for JPEG, PNG, and GIF file formats −
<!DOCTYPE html> <html> <body> <canvas id="myCanvas" width="300" height="170" style="border:2px solid #d3d3d3;"></canvas> <script> window.onload = function() { var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); context.fillStyle = "red"; context.fillRect(50, 50, 100, 100); window.location = canvas.toDataURL("image/webp"); } </script> </body> </html>
Output

Right-click in the canvas and you will get an option to save it −

Click Save image as… and now you can save the image. Let us save it on the Desktop as mycanva.web Under Save as type:, webP Image is visible because in the code, we wrote image/webp −
- Related Articles
- Difference between GIF and PNG
- How to save a canvas as PNG in Selenium?
- HTML5 Canvas to PNG File
- How to save HTML Canvas as an Image with canvas.toDataURL()?
- How to draw a png image on a Python tkinter canvas?
- How to convert HTML to PDF using Python
- How to add HTML and CSS to PDF?
- How to completely fill web page with HTML canvas?
- How to convert html pages to pdf using wkhtml2pdf
- How to draw on the canvas with JavaScript?
- How to Generate a PDF from an HTML Webpage?
- How to save as PDF on Chrome using Selenium
- Detect area of a PNG that is not transparent with HTML
- Recommended way to embed PDF in HTML?
- How to save DIV as Image with HTM5 canvas to image with the extension?
