xtreme-ui
Version:
Collection of reusable components that can be used in web projects
45 lines (44 loc) • 1.58 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 = () => {
const i = t?.result?.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;
}), c = (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(c(r)), s.translate(-n / 2, -n / 2), s.drawImage(t, n / 2 - t.width * 0.5, n / 2 - t.height * 0.5);
const d = s.getImageData(0, 0, n, n);
o.width = e.width, o.height = e.height, s.putImageData(d, Math.round(0 - n / 2 + t.width * 0.5 - e.x), Math.round(0 - n / 2 + t.height * 0.5 - e.y)), o.toBlob(
(g) => i({
blob: g ?? void 0,
base64: o.toDataURL("image/jpeg")
}),
"image/jpeg"
);
});
};
export {
h as createImage,
I as getCroppedImg,
l as getImageSize,
c as getRadianAngle,
w as readImageFile,
u as readImageSrc
};