UNPKG

fabric

Version:

Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.

3 lines (2 loc) 1.9 kB
import{svgViewBoxElementsRegEx as t,reViewBoxAttrValue as e,svgNS as i}from"./constants.min.mjs";import{parseUnit as r,parsePreserveAspectRatioAttribute as n}from"../util/misc/svgParsing.min.mjs";import{NONE as o}from"../constants.min.mjs";function a(a){if(!t.test(a.nodeName))return{};const s=a.getAttribute("viewBox");let m,h,d=1,g=1,u=0,l=0;const c=a.getAttribute("width"),p=a.getAttribute("height"),f=a.getAttribute("x")||0,A=a.getAttribute("y")||0,b=!(s&&e.test(s)),w=!c||!p||"100%"===c||"100%"===p;let v="",x=0,N=0;if(b&&(f||A)&&a.parentNode&&"#document"!==a.parentNode.nodeName&&(v=" translate("+r(f||"0")+" "+r(A||"0")+") ",m=(a.getAttribute("transform")||"")+v,a.setAttribute("transform",m),a.removeAttribute("x"),a.removeAttribute("y")),b&&w)return{width:0,height:0};const C={width:0,height:0};if(b)return C.width=r(c),C.height=r(p),C;const F=s.match(e);u=-parseFloat(F[1]),l=-parseFloat(F[2]);const M=parseFloat(F[3]),X=parseFloat(F[4]);C.minX=u,C.minY=l,C.viewBoxWidth=M,C.viewBoxHeight=X,w?(C.width=M,C.height=X):(C.width=r(c),C.height=r(p),d=C.width/M,g=C.height/X);const j=n(a.getAttribute("preserveAspectRatio")||"");if(j.alignX!==o&&("meet"===j.meetOrSlice&&(g=d=d>g?g:d),"slice"===j.meetOrSlice&&(g=d=d>g?d:g),x=C.width-M*d,N=C.height-X*d,"Mid"===j.alignX&&(x/=2),"Mid"===j.alignY&&(N/=2),"Min"===j.alignX&&(x=0),"Min"===j.alignY&&(N=0)),1===d&&1===g&&0===u&&0===l&&0===f&&0===A)return C;if((f||A)&&"#document"!==a.parentNode.nodeName&&(v=" translate("+r(f||"0")+" "+r(A||"0")+") "),m=v+" matrix("+d+" 0 0 "+g+" "+(u*d+x)+" "+(l*g+N)+") ","svg"===a.nodeName){for(h=a.ownerDocument.createElementNS(i,"g");a.firstChild;)h.appendChild(a.firstChild);a.appendChild(h)}else h=a,h.removeAttribute("x"),h.removeAttribute("y"),m=h.getAttribute("transform")+m;return h.setAttribute("transform",m),C}export{a as applyViewboxTransform}; //# sourceMappingURL=applyViewboxTransform.min.mjs.map