InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

HTML5 Canvas

  • canvas introduction
  • canvas line
  • canvas rectangle
  • canvas clear rectangle
  • canvas to png
  • canvas draw image
  • canvas draw image using base 64 string
  • canvas draw image using svg string
  • canvas svg xml text to png image
 
  • Home
  • > Tutorials
  • > HTML
  • > HTML5 Canvas

How to convert HTML canvas to png image using javascript

By admin | Last updated on Jan 29, 2016

HTML canvas element (HTML5) be used to draw 2D shapes using Javascript. Once a shape is drawn, it can also be converted to an image (png, jpeg, etc.) and can be embedded in the HTML page using <img> tag and hence can also be downloaded. Here are the steps to achieve this:

  1. First create an empty canvas element as shown below:
    <canvas id="canvasid" width="200" height="100"></canvas>
  2. Then draw some basic shape in it. Here is the javascript code to draw a simple square:
    var canvas = document.getElementById('canvasid');
    var context = canvas.getContext('2d');
    context.fillStyle="#ff0000";
    context.fillRect(10,10,60,60);
    
  3. Now using canvas toDataURL("image/png") get the png image url and populate the src attribute of required image. You may also want to see Canvas element reference. Here is how the code looks like:
    <img id="canvasimg" src="" />
    <script type="text/javascript">
    $("#canvasimg").attr("src", $("#canvasid").get(0).toDataURL("img/png"));
    </script>
    
     Note that the value returned by toDataURL("image/png") looks something like this:

    data:image/png;base64,iVBORw0KGgo.....
  4. Here is how the final image looks like:
    canvas-to-png-image-exampleYou can see the live demo at Canvas to image example.
  5. To download the image, you can right click on it and download as a regular image.

Notes on browsers compatibility

Canvas being HTML5 element is not supported by old browsers like IE8. It should work in browsers like Chrome, Firefox, IE9, etc. which support HTML5.

Suggested posts:

  1. HTML5 canvas draw image examples
  2. Javascript DOMContentLoaded listener example
  3. HTML5 canvas – draw image using base 64 encoded png string
  4. Convert svg xml text to png image using canvas
  5. PHP – call a function with arguments in array examples
  6. HTML5 canvas line example
  7. HTML5 shadow dom example
  8. HTML5 canvas rectangle example
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged HTML, HTML5, HTML5 Canvas, Javascript, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress