The HTMLCanvasElement.toDataURL() method returns a data URI containing a representation of the image in the format specified by the type parameter (defaults to PNG). The returned image is in a resolution of 96 dpi.

  • If the height or width of the canvas is 0, the string "data:," is returned.
  • If the requested type is not image/png, but the returned value starts with data:image/png, then the requested type is not supported.
  • Chrome also supports the image/webp type.


<var>canvas.toDataURL(type, encoderOptions);</var>


type Optional
A DOMString indicating the image format. The default type is image/png.
encoderOptions Optional
A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
If this argument is anything else, the default value for image quality is used. The default value is 0.92. Other arguments are ignored.

Return value

A DOMString containing the requested data URI.


Given this <canvas> element:

<canvas id="canvas" width="5" height="5"></canvas>

You can get a data-URL of the canvas with the following lines:

var canvas = document.getElementById("canvas");
var dataURL = canvas.toDataURL();
// "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby

Setting image quality with jpegs

var fullQuality = canvas.toDataURL("image/jpeg", 1.0);
// data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ...9oADAMBAAIRAxEAPwD/AD/6AP/Z"
var mediumQuality = canvas.toDataURL("image/jpeg", 0.5);
var lowQuality = canvas.toDataURL("image/jpeg", 0.1);

Example: Dynamically change images

You can use this technique in association with mouse events in order to dynamically change images (gray-scale versus color in this example):


<img class="grayscale" src="myPicture.png" alt="Description of my picture" />


window.addEventListener("load", removeColors);

function showColorImg() { = "none"; = "inline";

function showGrayImg() { = "inline"; = "none";

function removeColors() {
  var aImages = document.getElementsByClassName("grayscale"),
      nImgsLen = aImages.length,
      oCanvas = document.createElement("canvas"),
      oCtx = oCanvas.getContext("2d");
  for (var nWidth, nHeight, oImgData, oGrayImg, nPixel, aPix, nPixLen, nImgId = 0; nImgId < nImgsLen; nImgId++) {
    oColorImg = aImages[nImgId];
    nWidth = oColorImg.offsetWidth;
    nHeight = oColorImg.offsetHeight;
    oCanvas.width = nWidth;
    oCanvas.height = nHeight;
    oCtx.drawImage(oColorImg, 0, 0);
    oImgData = oCtx.getImageData(0, 0, nWidth, nHeight);
    aPix =;
    nPixLen = aPix.length;
    for (nPixel = 0; nPixel < nPixLen; nPixel += 4) {
      aPix[nPixel + 2] = aPix[nPixel + 1] = aPix[nPixel] = (aPix[nPixel] + aPix[nPixel + 1] + aPix[nPixel + 2]) / 3;
    oCtx.putImageData(oImgData, 0, 0);
    oGrayImg = new Image();
    oGrayImg.src = oCanvas.toDataURL();
    oGrayImg.onmouseover = showColorImg;
    oColorImg.onmouseout = showGrayImg;
    oCtx.clearRect(0, 0, nWidth, nHeight); = "none";
    oColorImg.parentNode.insertBefore(oGrayImg, oColorImg);


Specification Status Comment
WHATWG HTML Living Standard
The definition of 'HTMLCanvasElement.toDataURL' in that specification.
Living Standard No change since the latest snapshot, HTML5
The definition of 'HTMLCanvasElement.toDataURL' in that specification.
Working Draft  
The definition of 'HTMLCanvasElement.toDataURL' in that specification.
Recommendation Snapshot of the WHATWG HTML Living Standard containing the initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4 3.6 (1.9.2) 9 9 4.0
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 3.2 18 1.0 (1.9.2) (Yes) 19 3.0

See also


© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.

API Canvas HTMLCanvasElement Method Reference Référence