--- title: HTMLCanvasElement.toDataURL() slug: Web/API/HTMLCanvasElement/toDataURL translation_of: Web/API/HTMLCanvasElement/toDataURL ---
HTMLCanvasElement.toDataURL()
メソッドは、 type
パラメータ(デフォルトはPNG形式)で指定される画像フォーマット形式の data URI を返すメソッドです。返り値となる画像の解像度は96 dpiです。
0
もしくは maximum canvas size より大きい場合、文字列 "data:,"
を返します。image/png
形式を指定したにもかかわらず返り値が data:image/png
で始まる場合、要求された形式には対応していません。image/webp
形式に対応します。canvas.toDataURL(type, encoderOptions);
type
{{optional_inline}}image/png
です。encoderOptions
{{optional_inline}}0
から 1
の間の {{jsxref("Number")}} で示す、 image/jpeg
や image/webp
のような非可逆圧縮を使う画像フォーマットの画質です。0.92
です。その他の引数は無視されます。要求されたdata URIを含む {{domxref("DOMString")}} 。
SecurityError
このような {{HTMLElement("canvas")}} 要素を用意します
<canvas id="canvas" width="5" height="5"></canvas>
以下のコードによりキャンバスのData URLを取得できます。
var canvas = document.getElementById('canvas'); var dataURL = canvas.toDataURL(); console.log(dataURL); // "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby // blAAAADElEQVQImWNgoBMAAABpAAFEI8ARAAAAAElFTkSuQmCC"
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);
You can use this technique in coordination with mouse events in order to dynamically change images (gray-scale vs. color in this example):
<img class="grayscale" src="myPicture.png" alt="Description of my picture" />
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); } }
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "scripting.html#dom-canvas-todataurl", "HTMLCanvasElement.toDataURL")}} | {{Spec2('HTML WHATWG')}} | No change since the latest snapshot, {{SpecName('HTML5 W3C')}} |
{{SpecName('HTML5.1', "scripting-1.html#dom-canvas-todataurl", "HTMLCanvasElement.toDataURL")}} | {{Spec2('HTML5.1')}} | |
{{SpecName('HTML5 W3C', "scripting-1.html#dom-canvas-todataurl", "HTMLCanvasElement.toDataURL")}} | {{Spec2('HTML5 W3C')}} | Snapshot of the {{SpecName('HTML WHATWG')}} containing the initial definition. |
{{Compat("api.HTMLCanvasElement.toDataURL")}}