toDataURL:抛出一个安全错误

时间:2016-04-13 21:12:50

标签: javascript html5 canvas html5-canvas

我想获得画布的64位基本编码。下面的代码抛出一个安全例外: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();
}

我还尝试添加img.crossOrigin = "Anonymous";,但后来我得到了这个例外Image from origin 'file://' has been blocked from loading by Cross-Origin Resource Sharing policy: Invalid response. Origin 'null' is therefore not allowed access.

0 个答案:

没有答案