想要做一个相册之类的功能,有相框和相片两个图片,现在把这两个图片画入到一个新的canvas,canvas显示出来是正常的 ,但是想把这个canvas转化成新的图片,但是一直转化不了报
Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
关键的代码是
var newCanvas = document.createElement('canvas');
newCanvas.width = 300;
newCanvas.height = 400;
newxCtx = newCanvas.getContext("2d");
var img = new Image();
img.src = $(".photoWrap_canvas").attr("data-url");
img.setAttribute("crossOrigin", 'anonymous')
img.onload = function () {
// $(".photoWrap_canvas").hide();
newxCtx.drawImage(util.getEl("myCanvas"), 0, 0);
newxCtx.drawImage(img, 0, 0, 300, 400);
$(".result").prepend(newCanvas);
var data = newCanvas.toDataURL('image/jpeg');
<span>alert</span>(data)
var hc_image = new Image();
hc_image.src = data;
$(".result").append(hc_image);
}
主要问题是选择本地文件放到页面上的canvas里的时候也要设置一下跨域