我正在开发这个应用程序,我需要从 url 和用户的硬盘驱动器将图像上传到 html5 画布。我已经为用户从硬盘驱动器输入实现了功能,但我不知道如何从 url 添加图像。
我用来加载的代码input type file
都在输入的事件侦听器中。它有很多功能,因为应用程序也需要text
在图像上输入。因此image from url
也应该具有相同的功能。
请注意,imgObj
在事件侦听器内部。我尝试将其更改img.src
为 url 以对其进行测试,但即使图像加载,我也无法再下载它。控制台抛出此错误:
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
at HTMLButtonElement.<anonymous>
这是代码减去一些不需要在这里显示的函数:
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const btnDownload = document.getElementById('btnDownload');
const fileUpload = document.getElementById('file-upload');
fileUpload.addEventListener('change', function(e) {
let imgObj = new Image();
imgObj.onload = draw;
imgObj.onerror = failed;
imgObj.src = URL.createObjectURL(this.files[0]);
// imgObj.src = 'https://static.vecteezy.com/system/resources/previews/000/094/478/original/free-abstract-pattern-vector.jpg';
// some functions to write on image
});
function draw() {
canvas.width = this.naturalWidth;
canvas.height = this.naturalHeight;
const nw = this.naturalWidth;
const nh = this.naturalHeight;
ctx.drawImage(this, 0, 0, nw, nh);
};
function failed() {
console.error("The provided file couldn't be loaded as an Image media");
};
btnDownload.addEventListener('click', () => {
const a = document.createElement('a');
document.body.appendChild(a);
a.href = canvas.toDataURL();
a.download = "canvas-image.png";
a.click();
document.body.removeChild(a);
});
<canvas id="canvas" width="800" height="500"></canvas>
<input type="file" id="file-upload" />
<button id="btnDownload">Download</button>
简而言之,如何upload from url
通过保留功能来添加upload from input
功能?
请注意,输入侦听器内部的upload from url
功能将是相同的。imgObj
所以可能我必须对and做点什么img.src
。