Baseline Widely available
HTMLCanvasElement.toDataURL()
ã¡ã½ããã¯ã type
弿°ã§æå®ãããå½¢å¼ã§ç»åã表ç¾ãããã®ãå
¥ã£ããã¼ã¿ URL ãè¿ãã¾ãã
叿ãããã¡ã¤ã«å½¢å¼ãç»åå質ãæå®ãããã¨ãã§ãã¾ãã ãã¡ã¤ã«å½¢å¼ãæå®ããªãã£ãå ´åãã¾ãã¯æå®ãããå½¢å¼ã«å¯¾å¿ãã¦ããªãå ´åã¯ããã¼ã¿ã¯ image/png
ã¨ãã¦æ¸ãåºããã¾ãã è¨ãæããã°ãä»ã®ãªã¯ã¨ã¹ãããã type
ã«å¯¾ãã¦ãè¿å¤ã data:image/png
ã§å§ã¾ãå ´åããã®å½¢å¼ã¯å¯¾å¿ãã¦ããªãã¨ãããã¨ã§ãã
ãã©ã¦ã¶ã¼ã¯ image/png
ã«å¯¾å¿ãã¦ããå¿
è¦ãããã¾ããå¤ãã®ãã©ã¦ã¶ã¼ã¯ image/jpeg
ã image/webp
ãªã©ã®ä»ã®å½¢å¼ã«ã対å¿ãã¦ãã¾ãã
使ããç»åãã¼ã¿ã®è§£å度ã¯ãè§£å度ã¡ã¿ãã¼ã¿ã®ã¨ã³ã³ã¼ãã¼ã«å¯¾å¿ãããã¡ã¤ã«å½¢å¼ã®å ´åã 96dpi ã¨ãªãã¾ãã
æ§ætoDataURL()
toDataURL(type)
toDataURL(type, encoderOptions)
弿°
type
çç¥å¯
æååã§ãç»åå½¢å¼ã表ãã¾ãã æ¢å®ã®å½¢å¼ã¯ image/png
ã§ãããã®å½¢å¼ã¯ãæå®ãããå½¢å¼ã«å¯¾å¿ãã¦ããªãå ´åã«ã使ç¨ããã¾ãã
encoderOptions
çç¥å¯
0
ãã 1
ã®éã®æ°å¤ã§ããã使ããç»åãéå¯éå§ç¸®ï¼image/jpeg
ã image/webp
ãªã©ï¼ã§ãã£ãå ´åã®ç»åå質ã示ãã¾ãã ãã®ãªãã·ã§ã³ãæå®ãããªãã£ããã許å¯ããã¦ããç¯å²å¤ã®æ°å¤ã§ãã£ããããå ´åã¯ãã¦ã¼ã¶ã¼ã¨ã¼ã¸ã§ã³ãã¯æ¢å®ã®å質å¤ã使ç¨ãã¾ãã
è¦æ±ããããã¼ã¿ URL ã®å ¥ã£ãæååã§ãã
ãã£ã³ãã¹ã®é«ãã¾ãã¯å¹
ã 0
ã¾ãã¯ãã£ã³ãã¹ã®æå¤§ãµã¤ãºãã大ããå ´åã "data:,"
ã¨ããæååãè¿ããã¾ãã
SecurityError
ãã£ã³ãã¹ã®ããããããããªãªã¸ã³ã¯ãªã¼ã³ã§ã¯ããã¾ããã ãã®ã³ã³ãã³ãã®å°ãªãã¨ãä¸é¨ããææ¸ãã®ãã®ãèªã¿è¾¼ã¾ãããµã¤ãã¨ã¯å¥ã®ãµã¤ãããèªã¿è¾¼ã¾ãããã®ã§ãããããã®å¯è½æ§ãããã¾ãã
ãã®ãã㪠<canvas>
è¦ç´ ãç¨æãã¾ã
<canvas id="canvas" width="5" height="5"></canvas>
以ä¸ã®ã³ã¼ãã«ãããã£ã³ãã¹ã® Data URL ãåå¾ã§ãã¾ãã
const canvas = document.getElementById("canvas");
const dataURL = canvas.toDataURL();
console.log(dataURL);
// "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby
// blAAAADElEQVQImWNgoBMAAABpAAFEI8ARAAAAAElFTkSuQmCC"
JPEG ã®ç»è³ªãè¨å®ãã
const fullQuality = canvas.toDataURL("image/jpeg", 1.0);
// data:image/jpeg;base64,/9j/4AAQSkZJRgABAQâ¦9oADAMBAAIRAxEAPwD/AD/6AP/Z"
const mediumQuality = canvas.toDataURL("image/jpeg", 0.5);
const lowQuality = canvas.toDataURL("image/jpeg", 0.1);
ä¾: åçã«ç»åã夿´
ãã¦ã¹ã¤ãã³ãã¨é£æºãã¦ä½¿ç¨ãããã¨ã§ãç»åãåçã«å¤åããããã¨ãã§ãã¾ãï¼ãã®ä¾ã§ã¯ãã°ã¬ã¼ã¹ã±ã¼ã«ã¨ã«ã©ã¼ï¼ã
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() {
const images = document.getElementsByClassName("grayscale");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
for (const colorImg of images) {
const width = colorImg.offsetWidth;
const height = colorImg.offsetHeight;
canvas.width = width;
canvas.height = height;
ctx.drawImage(colorImg, 0, 0);
const imgData = ctx.getImageData(0, 0, width, height);
const pix = imgData.data;
const pixLen = pix.length;
for (let pixel = 0; pixel < pixLen; pixel += 4) {
pix[pixel + 2] =
pix[pixel + 1] =
pix[pixel] =
(pix[pixel] + pix[pixel + 1] + pix[pixel + 2]) / 3;
}
ctx.putImageData(imgData, 0, 0);
const grayImg = new Image();
grayImg.src = canvas.toDataURL();
grayImg.onmouseover = showColorImg;
colorImg.onmouseout = showGrayImg;
ctx.clearRect(0, 0, width, height);
colorImg.style.display = "none";
colorImg.parentNode.insertBefore(grayImg, colorImg);
}
}
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4