我正在尝试制作一个webapp (使用impact js游戏引擎开发),可以在本地运行,而不需要本地主机(使用file:///C:/...) )。我被要求让它在铬上工作。
它不能在chrome上工作的主要问题是,由于CORS问题,chrome阻止从媒体文件夹加载我的媒体(主要是png/jpg中的图像)。
在花了几天时间阅读和尝试一些方法之后,我无法解决这个问题。任何在这方面有经验的人,请告诉我是否有可能,如果有,我应该采取什么方法。
方法我尝试过:
1)设置img.crossOrigin =“匿名”(失败,这仍然被铬阻止)
2)使用标志--允许文件访问--文件打开chrome (有效,但对最终用户来说不是一个可行的方法)。
3)读取图像并将其转换为数据uri格式(失败,由于固有的CORs问题,数据uri转换似乎无法工作)
4)试图使用appcache将所有图像缓存到浏览器缓存中(失败,似乎无法工作,因为它不是从all服务器访问的)
更新:我现在正在尝试编辑impact.image源代码,尝试在加载到图像时将src转换为数据url。
load: function( loadCallback ) {
function getBase64Image(img) {
// Create an empty canvas element
var img2 = document.createElement("img");
var canvas2 = document.createElement("canvas");
// Copy the image contents to the canvas
var ctx2 = canvas2.getContext("2d");
img2.onload = function(){
canvas2.width = img2.width;
canvas2.height = img2.height;
};
img2.onerror = function() {console.log("Image failed!");};
img2.src = img + '?' + Date.now();
ctx2.drawImage(img2, 0, 0, img2.width, img2.height);
return canvas2.toDataURL("image/png");
}
if( this.loaded ) {
if( loadCallback ) {
loadCallback( this.path, true );
}
return;
}
else if( !this.loaded && ig.ready ) {
this.loadCallback = loadCallback || null;
this.data = new Image();
this.data.onload = this.onload.bind(this);
this.data.onerror = this.onerror.bind(this);
//this.data.src = ig.prefix + this.path + ig.nocache;
//old src sets to local file, new src sets to data url generated
this.data.src = getBase64Image(this.path);
}
else {
ig.addResource( this );
}
ig.Image.cache[this.path] = this;
},由于某些原因,图像没有加载到函数中,即使我将图像加载到getBase64Image函数中,它也能工作吗?
发布于 2014-01-16 06:13:20
除非将everything保存为预生成的Bas-64数据-uris,然后将其烘烤到JS文件中,或者保存在"index.HTML“页面上的脚本标记,否则这里不会有什么好运气--特别是如果您的意图是将其分发给与for服务器断开连接的用户(至少为appcache提供一个域)。
请注意,为了生成数据-uris,您自己可能需要localhost (或构建工具)。
https://stackoverflow.com/questions/21152747
复制相似问题