相关疑难解决方法(0)

为什么canvas.toDataURL()会抛出安全异常?

我睡眠不足还是什么?以下代码

var frame=document.getElementById("viewer");
frame.width=100;
frame.height=100;

var ctx=frame.getContext("2d");
var img=new Image();
img.src="http://www.ansearch.com/images/interface/item/small/image.png"

img.onload=function() {
    // draw image
    ctx.drawImage(img, 0, 0)

    // Here's where the error happens:
    window.open(frame.toDataURL("image/png"));
}
Run Code Online (Sandbox Code Playgroud)

抛出这个错误:

SECURITY_ERR: DOM Exception 18
Run Code Online (Sandbox Code Playgroud)

这不可能不起作用!请问有人解释一下吗?

javascript html5 canvas cross-domain

88
推荐指数
4
解决办法
7万
查看次数

canvas.toDataURL()和drawImage()的安全性错误

<!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>
Run Code Online (Sandbox Code Playgroud)

此脚本使用html5徽标创建画布.从这个画布我想使用"toDataURL()" - 方法创建一个图像.在这里我收到一个安全错误.

Firefox说 …

javascript firefox google-chrome html5-canvas

5
推荐指数
2
解决办法
2万
查看次数