【发布时间】:2017-12-18 16:59:30
【问题描述】:
我正在尝试渲染具有从不同域获取的背景图像的 THREE.js 场景。
我收到了tainted canvas error,因为该图像未获得 CORS 批准,因此对画布的操作会导致安全错误。
阅读this answer后,我将THREE.TextureLoader()设置为允许跨域加载:
var loader = new THREE.TextureLoader();
//allow cross origin loading
loader.crossOrigin = '';
var texture = loader.load(
url_to_image,
// Function when resource is loaded
function ( texture ) {
var backgroundMesh = new THREE.Mesh(
new THREE.PlaneGeometry(2, 2, 0),
new THREE.MeshBasicMaterial({
map: texture
})
);
backgroundMesh.material.depthTest = false;
backgroundMesh.material.depthWrite = false;
// create your background scene
backgroundScene = new THREE.Scene();
backgroundCamera = new THREE.Camera();
backgroundScene.add( backgroundCamera );
backgroundScene.add( backgroundMesh );
},
// Function called when download progresses
function ( xhr ) {},
// Function called when download errors
function ( xhr ) {}
);
但是,我现在收到 CORS 错误:Access to Image at 'url_to_image' from origin 'my_domain' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'my_domain' is therefore not allowed access.
this answer 的评论似乎表明某些域不允许在 WebGL 中使用浏览器中的图像,我该如何确定是否是这种情况?
如果我从中提取图像的域没有问题,我怎样才能让它工作?
【问题讨论】:
标签: canvas three.js http-headers cors