I have a canvas element CA
where I'm drawing some fancy stuff on. Then I run kind of a filter over the ImageData IDA
from canvas CA
and create new, modified ImageData IDB
. Now, I want this IDB
to be converted to a base64 string without the need of a second helper canvas CB
.
Everything I've found on this topic shows a whole canvas being converted to a dataURL. Is it possible without CB? How?
var CA = document.getElementById("CA");
var ctx = CA.getContext("2d");
ctx.beginPath();
ctx.fillRect(100, 100, 100, 100);
ctx.stroke();
ctx.closePath();
var srcImg = ctx.getImageData(0, 0, CA.width, CA.height),
srcData = srcImg.data,
dstImg = ctx.createImageData(srcImg),
dstData = dstImg.data;
for (var i=0, maxI=srcData.length; i<maxI; i+=4) {
//invert
if (srcData[i+3] > 0) {
dstData[i] = dstData[i+1] = dstData[i+2] = 255;
dstData[i+3] = 0;
}
else {
dstData[i] = dstData[i+1] = dstData[i+2] = 0;
dstData[i+3] = 255;
}
}
//how to create base64 from dstImg??
//...
//just for visualization
document.getElementById("CB").getContext("2d").putImageData(dstImg, 0, 0);
canvas {
border : 1px solid;
}
<canvas id="CA" width="400" height="300"></canvas>
<!-- I'd like to avoid this helper canvas CB -->
<canvas id="CB" width="400" height="300"></canvas>