UNPKG

fabric

Version:

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

1 lines 3.53 kB
{"version":3,"file":"parseCoords.mjs","names":[],"sources":["../../../../src/gradient/parser/parseCoords.ts"],"sourcesContent":["import { isPercent } from '../../parser/percent';\nimport type { TSize } from '../../typedefs';\nimport type { GradientCoords, GradientType, GradientUnits } from '../typedefs';\nimport { parseGradientUnits, parseType } from './misc';\n\nfunction convertPercentUnitsToValues<\n T extends GradientType,\n K extends keyof GradientCoords<T>,\n>(\n valuesToConvert: Record<K, string | number>,\n { width, height, gradientUnits }: TSize & { gradientUnits: GradientUnits },\n) {\n let finalValue;\n return (Object.entries(valuesToConvert) as [K, string | number][]).reduce(\n (acc, [prop, propValue]) => {\n if (propValue === 'Infinity') {\n finalValue = 1;\n } else if (propValue === '-Infinity') {\n finalValue = 0;\n } else {\n const isString = typeof propValue === 'string';\n finalValue = isString ? parseFloat(propValue) : propValue;\n if (isString && isPercent(propValue)) {\n finalValue *= 0.01;\n if (gradientUnits === 'pixels') {\n // then we need to fix those percentages here in svg parsing\n if (prop === 'x1' || prop === 'x2' || prop === 'r2') {\n finalValue *= width;\n }\n if (prop === 'y1' || prop === 'y2') {\n finalValue *= height;\n }\n }\n }\n }\n acc[prop] = finalValue;\n return acc;\n },\n {} as Record<K, number>,\n );\n}\n\nfunction getValue(el: SVGGradientElement, key: string) {\n return el.getAttribute(key);\n}\n\nexport function parseLinearCoords(el: SVGGradientElement) {\n return {\n x1: getValue(el, 'x1') || 0,\n y1: getValue(el, 'y1') || 0,\n x2: getValue(el, 'x2') || '100%',\n y2: getValue(el, 'y2') || 0,\n };\n}\n\nexport function parseRadialCoords(el: SVGGradientElement) {\n return {\n x1: getValue(el, 'fx') || getValue(el, 'cx') || '50%',\n y1: getValue(el, 'fy') || getValue(el, 'cy') || '50%',\n r1: 0,\n x2: getValue(el, 'cx') || '50%',\n y2: getValue(el, 'cy') || '50%',\n r2: getValue(el, 'r') || '50%',\n };\n}\n\nexport function parseCoords(el: SVGGradientElement, size: TSize) {\n return convertPercentUnitsToValues(\n parseType(el) === 'linear' ? parseLinearCoords(el) : parseRadialCoords(el),\n {\n ...size,\n gradientUnits: parseGradientUnits(el),\n },\n );\n}\n"],"mappings":";;;AAKA,SAAS,4BAIP,iBACA,EAAE,OAAO,QAAQ,iBACjB;CACA,IAAI;AACJ,QAAQ,OAAO,QAAQ,gBAAgB,CAA4B,QAChE,KAAK,CAAC,MAAM,eAAe;AAC1B,MAAI,cAAc,WAChB,cAAa;WACJ,cAAc,YACvB,cAAa;OACR;GACL,MAAM,WAAW,OAAO,cAAc;AACtC,gBAAa,WAAW,WAAW,UAAU,GAAG;AAChD,OAAI,YAAY,UAAU,UAAU,EAAE;AACpC,kBAAc;AACd,QAAI,kBAAkB,UAAU;AAE9B,SAAI,SAAS,QAAQ,SAAS,QAAQ,SAAS,KAC7C,eAAc;AAEhB,SAAI,SAAS,QAAQ,SAAS,KAC5B,eAAc;;;;AAKtB,MAAI,QAAQ;AACZ,SAAO;IAET,EAAE,CACH;;AAGH,SAAS,SAAS,IAAwB,KAAa;AACrD,QAAO,GAAG,aAAa,IAAI;;AAG7B,SAAgB,kBAAkB,IAAwB;AACxD,QAAO;EACL,IAAI,SAAS,IAAI,KAAK,IAAI;EAC1B,IAAI,SAAS,IAAI,KAAK,IAAI;EAC1B,IAAI,SAAS,IAAI,KAAK,IAAI;EAC1B,IAAI,SAAS,IAAI,KAAK,IAAI;EAC3B;;AAGH,SAAgB,kBAAkB,IAAwB;AACxD,QAAO;EACL,IAAI,SAAS,IAAI,KAAK,IAAI,SAAS,IAAI,KAAK,IAAI;EAChD,IAAI,SAAS,IAAI,KAAK,IAAI,SAAS,IAAI,KAAK,IAAI;EAChD,IAAI;EACJ,IAAI,SAAS,IAAI,KAAK,IAAI;EAC1B,IAAI,SAAS,IAAI,KAAK,IAAI;EAC1B,IAAI,SAAS,IAAI,IAAI,IAAI;EAC1B;;AAGH,SAAgB,YAAY,IAAwB,MAAa;AAC/D,QAAO,4BACL,UAAU,GAAG,KAAK,WAAW,kBAAkB,GAAG,GAAG,kBAAkB,GAAG,EAC1E;EACE,GAAG;EACH,eAAe,mBAAmB,GAAG;EACtC,CACF"}