xtreme-ui
Version:
Collection of reusable components that can be used in web projects
51 lines (50 loc) • 1.66 kB
JavaScript
const u = async (a) => new Promise((e, r) => {
fetch(a).then((t) => (t.ok || r(t.status), t.blob())).then((t) => e(URL.createObjectURL(t)));
}), w = async (a) => new Promise((e, r) => {
const t = new FileReader();
t.onloadend = () => {
var o;
const i = (o = t == null ? void 0 : t.result) == null ? void 0 : o.toString();
i ? e(i) : r();
}, t.onerror = r, t.readAsDataURL(a);
}), h = (a) => new Promise((e, r) => {
const t = new Image();
t.addEventListener("load", () => e(t)), t.addEventListener("error", (i) => r(i)), t.src = a;
}), d = (a) => a * Math.PI / 180, l = async (a) => new Promise((e) => {
h(a).then((r) => {
e({
width: r.width,
height: r.height
});
});
}), I = async (a, e, r = 0) => {
if (!a || !e) return null;
const t = await h(a);
return new Promise((i) => {
const o = document.createElement("canvas"), s = o.getContext("2d");
if (!s) return null;
const n = 2 * (Math.max(t.width, t.height) / 2 * Math.sqrt(2));
o.width = n, o.height = n, s.translate(n / 2, n / 2), s.rotate(d(r)), s.translate(-n / 2, -n / 2), s.drawImage(
t,
n / 2 - t.width * 0.5,
n / 2 - t.height * 0.5
);
const g = s.getImageData(0, 0, n, n);
o.width = e.width, o.height = e.height, s.putImageData(
g,
Math.round(0 - n / 2 + t.width * 0.5 - e.x),
Math.round(0 - n / 2 + t.height * 0.5 - e.y)
), o.toBlob((c) => i({
blob: c ?? void 0,
base64: o.toDataURL("image/jpeg")
}), "image/jpeg");
});
};
export {
h as createImage,
I as getCroppedImg,
l as getImageSize,
d as getRadianAngle,
w as readImageFile,
u as readImageSrc
};