Canvas How to - Export canvas as image








Question

We would like to know how to export canvas as image.

Answer


<!--   w ww. j  a v  a 2  s.  c  o  m-->

<!DOCTYPE html>

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    
    <script type="text/javascript">
      $(document).ready(function() {  
        var canvas = $("#myCanvas");
        var context = canvas.get(0).getContext("2d");
        
        context.save();
        context.fillRect(50, 50, 100, 100);
        
        context.fillStyle = "rgb(255, 0, 0)";
        context.fillRect(100, 100, 100, 100);
        
        context.restore();
        context.fillRect(150, 150, 100, 100);
        
        // Convert the canvas into an image data URL
        var dataURL = canvas.get(0).toDataURL();
        var img = $("<img></img>");
        img.attr("src", dataURL);
        
        // Replace the canvas with the image of the canvas
        canvas.replaceWith(img);
      });
    </script>
  </head>
  
  <body>
    <canvas id="myCanvas" width="500" height="500">
      <!-- Insert fallback content here -->
    </canvas>
  </body>
</html>

The code above is rendered as follows: