在纹理中加载跨域图像 [英] Load cross domain image in texture

查看:39
本文介绍了在纹理中加载跨域图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在textureloader 中加载跨域图像并且我收到无法在'WebGLRenderingContext' 上执行'texImage2D':跨域..."错误

How to load cross domain image in textureloader and im getting "Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin..." error

var loader = new THREE.TextureLoader();
loader.load('http://myurl.com/img/box.png', onTextureLoaded);

推荐答案

首选

为资产添加 CORS 标头,允许跨域请求:

Add a CORS header to the asset, permitting cross-domain requests:

Access-Control-Allow-Origin: *

第二种选择

您可以创建一个服务器端代理,然后像这样加载资产

You can create a server side proxy, and then load the asset like this

loader.load('myproxy?url=http://myurl.com/img/box.png', onTextureLoaded);

但是您需要小心,因为代理可能会占用大量带宽,并且您在设置时需要非常彻底,以免意外打开服务器受到某些代码注入攻击.

However you need to be careful, as proxies can use up a lot of bandwidth and you need to be very thorough when setting it up so you don't accidentally open your server to some code injection attacks.

这篇关于在纹理中加载跨域图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆