HTMLCanvasElement.toDataURL() 方法返回 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 or larger than the maximum canvas size ,字符串 "data:," 被返回。
  • 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 类型。

句法

canvas.toDataURL(type, encoderOptions);
					

参数

type 可选
DOMString indicating the image format. The default format type is image/png .
encoderOptions 可选
Number between 0 and 1 indicating the image quality to use for image formats that use lossy compression such as image/jpeg and 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.

返回值

A DOMString containing the requested data URI .

异常

SecurityError

The canvas's bitmap is not origin clean; at least some of its contents have or may have been loaded from a site other than the one from which the document itself was loaded.

范例

Given this <canvas> 元素:

<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();
console.log(dataURL);
// "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby
// blAAAADElEQVQImWNgoBMAAABpAAFEI8ARAAAAAElFTkSuQmCC"
					

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 coordination with mouse events in order to dynamically change images (gray-scale vs. color in this example):

HTML

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

JavaScript

window.addEventListener('load', removeColors);
function showColorImg() {
  this.style.display = 'none';
  this.nextSibling.style.display = 'inline';
}
function showGrayImg() {
  this.previousSibling.style.display = 'inline';
  this.style.display = '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 = oImgData.data;
    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);
    oColorImg.style.display = "none";
    oColorImg.parentNode.insertBefore(oGrayImg, oColorImg);
  }
}
					

规范

规范 状态 注释
HTML 实时标准
The definition of 'HTMLCanvasElement.toDataURL' in that specification.
实时标准 无变化从最新快照起, HTML5
HTML 5.1
The definition of 'HTMLCanvasElement.toDataURL' in that specification.
推荐
HTML5
The definition of 'HTMLCanvasElement.toDataURL' in that specification.
推荐 Snapshot of the HTML 实时标准 containing the initial definition.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
toDataURL Chrome 1 Edge 12 Firefox 2 IE 9 Opera 9 Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 3.2 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: