I want to get the 64 base encode of the canvas. Below code throw an security exception: Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
<canvas id="canvas" width="345" height="345"></canvas>
var
canvas = document.getElementById("canvas"),
ctx = null,
img = new Image(),
dataUrl = null;
var init = function() {
img.onload = function () {
ctx.drawImage(img, 0, 0);
dataUrl = canvas.toDataURL();
};
/*img.crossOrigin = "Anonymous";*/
img.src = "./img/s1.jpg";
console.log(dataUrl);
};
if (Modernizr.canvas) {
ctx = canvas.getContext("2d");
init();
}
I also tried to add img.crossOrigin = "Anonymous";
but then i get this exception Image from origin 'file://' has been blocked from loading by Cross-Origin Resource Sharing policy: Invalid response. Origin 'null' is therefore not allowed access.