我想保存我的画布到img。我有这样一个函数:
function save() {
document.getElementById("canvasimg").style.border = "2px solid";
var dataURL = canvas.toDataURL();
document.getElementById("canvasimg").src = dataURL;
document.getElementById("canvasimg").style.display = "inline";
}
它给出了错误:
Uncaught SecurityError:未能在“HTMLCanvasElement”上执行“toDataURL”:受污染的画布可能无法导出。
我该怎么办?
出于安全原因,您的本地驱动器被声明为“other-domain”,并将污染画布。
(这是因为你最敏感的信息可能在你的本地驱动器上!)
在测试时尝试以下变通方法:
Put all page related files (.html, .jpg, .js, .css, etc) on your desktop (not in sub-folders).
Post your images to a site that supports cross-domain sharing (like dropbox.com or GitHub). Be sure you put your images in dropbox's public folder and also set the cross origin flag when downloading the image (var img=new Image(); img.crossOrigin="anonymous" ...)
Install a webserver on your development computer (IIS and PHP web servers both have free editions that work nicely on a local computer).
出于安全原因,您的本地驱动器被声明为“other-domain”,并将污染画布。
(这是因为你最敏感的信息可能在你的本地驱动器上!)
在测试时尝试以下变通方法:
Put all page related files (.html, .jpg, .js, .css, etc) on your desktop (not in sub-folders).
Post your images to a site that supports cross-domain sharing (like dropbox.com or GitHub). Be sure you put your images in dropbox's public folder and also set the cross origin flag when downloading the image (var img=new Image(); img.crossOrigin="anonymous" ...)
Install a webserver on your development computer (IIS and PHP web servers both have free editions that work nicely on a local computer).
就像@mark的回答一样。你可以通过本地服务器服务你的网站。在本地服务器上不会出现此错误。
如果你的电脑上已经安装了PHP(一些较旧的MacOS版本已经预安装了PHP):
打开终端/cmd
导航到你的网站文件所在的文件夹
在此文件夹中,运行命令php -S localhost:3000
打开浏览器,在URL栏中进入localhost:3000。你的网站应该在那里运行。
or
如果你的电脑上安装了Node.js:
打开终端/cmd
导航到你的网站文件所在的文件夹
在这个文件夹中,运行命令npm init -y
在mac上运行npm install live-server -g或sudo npm install live-server -g
运行live-server,它应该会自动在浏览器中打开一个新选项卡,打开你的网站。
注意:记得在文件夹的根目录下有一个index.html文件,否则你可能会遇到一些问题。