@cloudcome/utils-browser
Version:
cloudcome utils for browser
36 lines (35 loc) • 943 B
JavaScript
import { setStyle } from "./dom.mjs";
async function imageLoad(url) {
return new Promise((resolve, reject) => {
const image = new Image();
const onFinish = (isError) => {
image.onload = image.onerror = null;
document.body.removeChild(image);
isError ? reject(new Error("图片加载失败")) : resolve(image);
};
image.onload = () => onFinish();
image.onerror = () => onFinish(true);
image.crossOrigin = "anonymous";
image.src = url;
setStyle(image, {
visibility: "hidden",
position: "absolute",
top: "-99999%",
left: "-99999%",
maxWidth: "none",
maxHeight: "none",
border: "0",
width: "auto",
height: "auto",
margin: "0",
padding: "0",
transform: ""
});
document.body.appendChild(image);
if (image.complete && image.width > 0) onFinish();
});
}
export {
imageLoad
};
//# sourceMappingURL=image.mjs.map