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