lean-qr
Version:
minimal QR code generation
2 lines (1 loc) • 1.35 kB
JavaScript
const t="http://www.w3.org/2000/svg",e=/[^-a-zA-Z0-9 .,:/#%()]/g,n=(e,n,o,i=[],r=e.createElementNS(t,n))=>(Object.entries(o).map((([t,e])=>r.setAttribute(t,e))),r.replaceChildren(...i),r),o=(t,n,o,i=[])=>[`<${n}`,...Object.entries(o).map((([t,n])=>` ${t}="${`${n}`.replace(e,"")}"`)),">",...i,`</${n}>`].join(""),i=t=>{const e=new Map,n=[],o=t=>{const n=e.get(t);return e.delete(t),n};for(let i=0;i<=t.size;++i)for(let r=0;r<=t.size;++r){const s=t.get(r,i),a=[t.get(r-1,i)^s&&r+" "+(i+1),r+" "+i,t.get(r,i-1)^s&&r+1+" "+i].filter((t=>t));if(a.length>1){s||a.reverse();const t=a.pop(),i=o(a[0])||[],r=o(t);i.push(...a),i===r?n.push(i):(r?r.unshift(...i):e.set(t,i),e.set(i[0],r||i))}}return n.map((t=>`M${t.join("L")}Z`)).join("")},r=(e,{on:n="black",off:o,padX:r=4,padY:s=4,width:a,height:c,scale:l=1},g,h,d)=>{const $=e.size+2*r,w=e.size+2*s;return g(h,"svg",{xmlns:t,version:"1.1",viewBox:`${-r} ${-s} ${$} ${w}`,width:a??$*l,height:c??w*l,"shape-rendering":"crispedges"},[o?g(h,"rect",{x:-r,y:-s,width:$,height:w,fill:o}):"",g(h,"path",{d:i(e),fill:n})],d)},s=(t,e,o={})=>e.body?r(t,o,n,e):r(t,o,n,e.ownerDocument,e),a=(t,e={})=>(e.xmlDeclaration?'<?xml version="1.0" encoding="UTF-8" ?>':"")+r(t,e,o),c=(t,e)=>"data:image/svg+xml;base64,"+btoa(a(t,{xmlDeclaration:1,...e}));export{s as toSvg,c as toSvgDataURL,i as toSvgPath,a as toSvgSource};