简体   繁体   English

为 WebGL 纹理加载图像,但由于 CORS 而失败

[英]Load an image for WebGL texture but failed because of CORS

I'm reading a tutorial on WebGL ( link ).我正在阅读有关 WebGL 的教程( 链接)。 In this tutorial, we load a texture locally.在本教程中,我们在本地加载纹理。 However, because of cross-origin issue for WebGL texture, we must add img.crossOrigin="anonymous" .但是,由于 WebGL 纹理的跨域问题,我们必须添加img.crossOrigin="anonymous"

Unfortunatly for me, it invokes onerror event.对我来说不幸的是,它调用了 onerror 事件。

Here's the code:这是代码:

const image = new Image();
image.onload = () => {
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
    gl.generateMipmap(gl.TEXTURE_2D);
    console.log("Texture loaded.");
};
image.onerror = () => {
    console.log("Texture error!");
}
image.crossOrigin = "anonymous";
image.src = url;

What did I missed?我错过了什么?

To load images locally you need to run a server and NOT set image.crossOrigin要在本地加载图像, 您需要运行服务器而不是设置image.crossOrigin

I recommend you start with this one or one of the many here我建议您从这里的众多之一或其中之一开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM