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

标签 javascript html canvas cross-domain

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

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"));
}

抛出这个错误:

SECURITY_ERR: DOM Exception 18

这绝对行不通!谁能解释一下,好吗?

最佳答案

specs它说:

Whenever the toDataURL() method of a canvas element whose origin-clean flag is set to false is called, the method must raise a SECURITY_ERR exception.

如果图像来自另一台服务器,我认为您不能使用 toDataURL()

关于javascript - 为什么 canvas.toDataURL() 抛出安全异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2390232/

相关文章:

javascript - 基于java脚本变量动态改变html的header标签

java - CSS 文件不会在 JSP 页面中下载

JavaScript 碰撞检测似乎不起作用?

html - 移动设备上的悬停效果

javafx - 如何在javaFX中使 Canvas 可调整大小?

javascript - 如何在不模糊的情况下拉伸(stretch) WebGL Canvas ?样式 "image-rendering"不起作用

javascript - 替换推文按钮图标

javascript - Bootstrap 选项卡和 Meteor

javascript - 部分中的 Angular 绑定(bind)

html - 响应式固定位置侧div