執行toDataURL後,我借鑑的圖像(本地文件)的畫布,我試着將它與命令ctx.canvas.toDataURL("image/png")
Failto上HTMLCanvasElement
出口,但有一個錯誤:
DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
我已經搜查在谷歌。他們說這是Cross的問題。所以,我加了命令:
image.crossOrigin = '*';
但是這對我的項目沒用。其實,我的項目是建立在沒有任何服務器的本地。所以,我不知道爲什麼存在跨域問題。
function loadImageAsync(url) {
return new Promise(function(resolve, reject) {
var image = new Image();
image.onload = function() {
image.crossOrigin = '*';
resolve(image);
};
image.onerror = function() {
reject(new Error('Could not load image at ' + url));
};
image.src = url;
});
generate() {
var p1 = loadImageAsync(this.textures[1]);
var p2 = loadImageAsync(this.textures[2]);
var p3 = loadImageAsync(this.textures[3]);
var ctx = document.createElement("canvas")
.getContext("2d");
ctx.canvas.width = this.width;
ctx.canvas.height = this.height;
var rows = ~~(this.width/70);
var cols = ~~(this.height/70);
Promise.all([p1, p2, p3])
.then(imgs => {
for (let x = 0, i = 0; i < rows; x += 70, i++) {
for (let y = 630, j = 0; j < cols; y -= 70, j++) {
this.resource[i].forEach(item => {
switch (item) {
case 1:
ctx.drawImage(imgs[0], x, y, 70, 70);
break;
case 2:
ctx.drawImage(imgs[1], x, y, 70, 70);
break;
case 3:
ctx.drawImage(imgs[2], x, y, 70, 70);
break;
default:
}
});
}
}
//window.ctx = ctx;
this.image.crossOrigin = '*';
this.image.src = ctx.canvas.toDataURL("image/png");
});
};
您是否正在從不同的網站載入圖片? –
@moáois我試圖從本地或服務器加載圖像。有相同的問題 –
你運行任何服務器像XAMPP或WAMP? –