@g20/svg
Version:
Scalable Vector Graphics Renderer
3 lines (2 loc) • 4.17 kB
JavaScript
import{Commands as e,G20 as t,TreeView as n,GraphicsBoard as r}from"@g20/core";class o{doc;constructor(e){this.doc=e}addEventListener(e,t,n){e.addEventListener(t,n)}appendChild(e,t){e.appendChild(t)}getAttribute(e,t){return e.getAttribute(t)}getBoundingClientRect(e){return e.getBoundingClientRect()}getElementById(e){return document.getElementById(e)}isDocumentBody(e){return e===document.body}removeEventListener(e,t,n){e.removeEventListener(t,n)}}function c(e,t){for(;e<0;)e+=t;return e%t}const l=Math.floor;function s(e){return l(1e6*e)/1e6}function i(e,t){const n=t,r=Object.keys(t);for(let t=0;t<r.length;t++){const o=r[t],c=n[o];/href/.test(r[t])?e.setAttributeNS("http://www.w3.org/1999/xlink",o,c):e.setAttribute(o,c)}}class u{downcast(e){if(e instanceof SVGElement)return e;throw new Error("element is not an SVGElement")}createSVGElement(e,t={}){return function(e,t={}){const n=document.createElementNS("http://www.w3.org/2000/svg",e);return t&&Object.keys(t).length>0&&i(n,t),n}(e,t)}setAttribute(e,t,n){e.setAttribute(t,n)}setAttributes(e,t){i(e,t)}removeAttribute(e,t){e.removeAttribute(t)}removeAttributes(e,t){a.removeAttributes(e,t)}appendChild(e,t){e.appendChild(t)}removeChild(e,t){e.removeChild(t)}setTextContent(e,t){e.textContent=t}getParentNode(e){return e.parentNode}getLastChild(e){return e.lastChild}getElementDefs(e){return function(e){const t=e.children,n=t.length;for(let e=0;e<n;e++){const n=t.item(e);if(n instanceof SVGDefsElement)return n}throw new Error}(e)}setStyle(e,t,n){e.style[t]=n}}const a={ns:"http://www.w3.org/2000/svg",xlink:"http://www.w3.org/1999/xlink",createElement:function(e,t={}){const n=document.createElementNS(a.ns,e);return t&&Object.keys(t).length>0&&a.setAttributes(n,t),n},setAttributes:function(e,t){const n=t,r=Object.keys(t);for(let t=0;t<r.length;t++){const o=r[t],c=n[o];/href/.test(r[t])?e.setAttributeNS(a.xlink,o,c):e.setAttribute(o,c)}return this},removeAttributes:function(e,t){if(e){for(const n in t)e.removeAttribute(n);return this}throw new Error("elem MUST be defined.")},path_from_anchors:function(n,r,o,l,i){const[u,a]=function(e){return e.goofy?e.crazy?[(e,t)=>t,(e,t)=>e]:[(e,t)=>e,(e,t)=>t]:e.crazy?[(e,t)=>e,(e,t)=>t]:[(e,t)=>t,(e,t)=>e]}(n),d=l.length,m=d-1;let y,f="";for(let n=0;n<d;n++){const r=l[n],o=l[i?c(n-1,d):Math.max(n-1,0)];let h,x,g,w,p,b,v,E,A,C,k,S,z,N,V,B=s(u(r.x,r.y)),G=s(a(r.x,r.y));switch(r.command){case e.close:h=e.close;break;case e.arc:k=r.rx,S=r.ry,z=r.xAxisRotation,N=r.largeArcFlag,V=r.sweepFlag,h=e.arc+" "+k+" "+S+" "+z+" "+N+" "+V+" "+B+" "+G;break;case e.curve:v=o.controls&&o.controls.b||t.zero,E=r.controls&&r.controls.a||t.zero,o.relative?(g=s(u(v.x+o.x,v.y+o.y)),w=s(a(v.x+o.x,v.y+o.y))):(g=s(u(v.x,v.y)),w=s(a(v.x,v.y))),r.relative?(p=s(u(E.x+r.x,E.y+r.y)),b=s(a(E.x+r.x,E.y+r.y))):(p=s(u(E.x,E.y)),b=s(a(E.x,E.y))),h=(0===n?e.move:e.curve)+" "+g+" "+w+" "+p+" "+b+" "+B+" "+G;break;case e.move:y=r,h=e.move+" "+B+" "+G;break;default:h=r.command+" "+B+" "+G}n>=m&&i&&(r.command===e.curve&&(x=y,A=r.controls&&r.controls.b||r,C=x.controls&&x.controls.a||x,r.relative?(g=s(u(A.x+r.x,A.y+r.y)),w=s(a(A.x+r.x,A.y+r.y))):(g=s(u(A.x,A.y)),w=s(a(A.x,A.y))),x.relative?(p=s(u(C.x+x.x,C.y+x.y)),b=s(a(C.x+x.x,C.y+x.y))):(p=s(u(C.x,C.y)),b=s(a(C.x,C.y))),B=s(u(x.x,x.y)),G=s(a(x.x,x.y)),h+=" C "+g+" "+w+" "+p+" "+b+" "+B+" "+G),r.command!==e.close&&(h+=" Z")),f+=h+" "}return f},getClip:function(e,t,n){let r=t.zzz.svgClipPathElement;return r||(r=t.zzz.svgClipPathElement=e.createSVGElement("clipPath",{"clip-rule":"nonzero"})),null===e.getParentNode(r)&&e.appendChild(e.getElementDefs(n),r),r},defs:{update:function(e,t){if(function(e){return e._flagUpdate}(t)){const n=Array.prototype.slice.call(t.children,0);for(let r=0;r<n.length;r++){const o=n[r],c=o.id,l=`[fill="url(#${c})"],[stroke="url(#${c})"],[clip-path="url(#${c})"]`;e.querySelector(l)||t.removeChild(o)}!function(e,t){e._flagUpdate=t}(t,!1)}}}};class d{constructor(){}createView(e,t){const r=new u;return new n(r,e,t)}}function m(e,t={}){const n=new o(window.document),c=new u;return new r(e,n,c,new d,t)}export{u as SVGViewDOM,d as SVGViewFactory,m as initBoard};
//# sourceMappingURL=index.min.js.map