dom2canvas
Version:
Convert DOM Elements/strings to canvas | pdf && style whatever you want to show on canvas/img/pdf
43 lines (42 loc) • 1.3 kB
JavaScript
function x(r, t, e) {
let c, g = "";
typeof r == "string" ? c = r : r instanceof HTMLElement ? c = r.outerHTML : c = `
<h1>Hello World124</h1>
`, t && (g = t);
const d = i(c, g, (e == null ? void 0 : e.width) ?? 800);
let a;
e != null && e.canvas ? a = e.canvas : a = document == null ? void 0 : document.createElement("canvas");
const n = a.getContext("2d");
a.width = (e == null ? void 0 : e.width) ?? 800, a.height = (e == null ? void 0 : e.height) ?? 800;
const f = new Blob([d], { type: "image/svg+xml;" }), w = new FileReader();
return new Promise((s) => {
w.readAsDataURL(f), w.onload = function(v) {
var h;
const m = (h = v.target) == null ? void 0 : h.result, l = new Image();
l.src = m, l.onload = function() {
new Promise(() => {
n ? (n.drawImage(l, 0, 0), s(a)) : console.log("canvas error", a);
}).catch((u) => {
console.error(u);
});
};
};
});
}
function i(r, t, e) {
return `
<svg xmlns="http://www.w3.org/2000/svg">
<foreignObject width="${e}" height="100%">
<div
xmlns="http://www.w3.org/1999/xhtml"
>
${t}
${r}
</div>
</foreignObject>
</svg>
`;
}
export {
x as default
};