【发布时间】:2012-03-09 19:33:23
【问题描述】:
<!doctype html>
<canvas id="canvas" height="200" width="200"></canvas>
<div id="new"></div>
<script>
var div = document.getElementById("new");
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var img = new Image();
img.src = 'http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png';
//img.src = 'local.png';
img.onload = function(){
//draws the image on the canvas (works)
ctx.drawImage(img,0,0,200,200);
//creates an image from the canvas (works only for local.png)
var sourceStr = canvas.toDataURL();
//creates the img-tag and adds it to the div-container
var newImage = document.createElement("img");
newImage.src = sourceStr;
div.appendChild(newImage);
}
</script>
此脚本创建一个带有 html5-logo 的画布。我想从这个画布上创建一个图像,使用“toDataURL()”方法。在这里我得到一个安全错误。
Firefox 说 - 错误:未捕获的异常:[异常...“安全错误”代码:“1000”nsresult:“0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)”
Chrome 说 - 未捕获的错误:SECURITY_ERR: DOM Exception 18
如果我在服务器上使用带有图像的脚本,它可以正常工作。所以它认为这一次它真的是一个特性而不是一个错误。 有谁知道我如何使用画布创建图像而无需其他服务器的图像? 顺便说一句:如果您在没有网络服务器的情况下将站点作为本地文件运行,则会发生错误。
【问题讨论】:
-
你找到答案了吗?我有同样的问题,它甚至发生在来自同一服务器的图像上。一旦任何图像被绘制到画布中,整个画布就变得无法访问。
标签: javascript firefox google-chrome html5-canvas