我正在开发一个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);
}
});
}