Three.js从相邻文件加载纹理

时间:2017-11-19 22:49:59

标签: javascript image three.js xmlhttprequest cross-domain

我正在开发一个three.js项目,我正在尝试加载一个新纹理,但我收到cross-origin个错误。

切换到localhost时,此功能正常,但我也需要将其与static HTML配合使用。

我试过这样做,但我仍然得到同样的错误

function getImage(path) {
    return new Promise(function (resolve, reject) {
        try {
            var image = document.createElement("img");
            image.setAttribute("src", path);
            image.crossOrigin = "Anonymous";
            image.addEventListener("load", function (event) {
                var canvas = document.createElement("canvas");
                var context = canvas.getContext("2d");
                canvas.width = image.width;
                canvas.height = image.height;
                context.clearRect(0, 0, canvas.width, canvas.height);
                context.drawImage(image, 0, 0);
                resolve(canvas.toDataURL("image/png"));
            });
            image.addEventListener("error", function (event) {
                event.preventDefault();
                reject();
            });
        } catch (err) {
            reject(err);
        }
    });
}

0 个答案:

没有答案