【发布时间】:2015-02-26 17:10:18
【问题描述】:
我正在尝试使用带有以下代码的 easeljs 将本地 jpg 添加到我的画布:
var bitmap = new createjs.Bitmap('img/gallery/1.jpg');
stage.addChild(bitmap);
这会将图像放在画布上。然后我想在图像中添加一个 onclick 事件:
var bitmap = new createjs.Bitmap('img/gallery/1.jpg');
bitmap.addEventListener("click", function(){
alert("CLICKED");
});
stage.addChild(bitmap);
这会导致以下错误:
Uncaught An error has occurred. This is most likely due to security restrictions on reading canvas pixel data with local or cross-domain images.
显然是跨源类型问题。我在easeljs 文档中注意到以下内容:
Images loaded cross-origin will throw cross-origin security errors when interacted with using a mouse, using methods such as getObjectUnderPoint, or using filters, or caching. You can get around this by setting crossOrigin flags on your images before passing them to EaselJS, eg: img.crossOrigin="Anonymous";
我试过这个(按照这里的步骤http://developers.spilgames.com/wiki/Developer_Platform_-_Learning_center_-FAQ-_HTML5_API_CreateJS_cross-domain_error)然后得到两个错误:
Image from origin 'file://' has been blocked from loading by Cross-Origin Resource Sharing policy: Received an invalid response. Origin 'null' is therefore not allowed access.
Uncaught InvalidStateError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The HTMLImageElement provided is in the 'broken' state.
由于我正在构建的项目是需要能够离线运行的 Cordova/Phonegap 项目,因此无法从 Web 服务器获取图像,据我所知,图像必须来自文件:/ /。有人对我如何解决这个问题有任何想法吗?
【问题讨论】:
-
您是否尝试过上述错误消息中提供的解决方案?
You can get around this by setting crossOrigin flags on your images before passing them to EaselJS, eg: img.crossOrigin="Anonymous";
标签: google-chrome cors easeljs createjs