UNPKG

fabric

Version:

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

1 lines 3.48 kB
{"version":3,"file":"parseCoords.min.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":"4HA0CA,SAAS,EAAS,EAAwB,EAAA,CACxC,OAAO,EAAG,aAAa,EAAA,CAuBzB,SAAgB,EAAY,EAAwB,EAAA,CAClD,OA9DF,SAIE,EAAA,CACA,MAAE,EAAA,OAAO,EAAA,cAAQ,GAAA,CAEjB,IAAI,EACJ,OAAQ,OAAO,QAAQ,EAAA,CAA4C,QAChE,EAAA,CAAM,EAAM,KAAA,CACX,GAAI,IAAc,WAChB,EAAa,UACJ,IAAc,YACvB,EAAa,MACR,CACL,IAAM,EAAgC,OAAd,GAAc,SACtC,EAAa,EAAW,WAAW,EAAA,CAAa,EAC5C,GAAY,EAAU,EAAA,GACxB,GAAc,IACV,IAAkB,WAEhB,IAAS,MAAQ,IAAS,MAAQ,IAAS,OAC7C,GAAc,GAEZ,IAAS,MAAQ,IAAS,OAC5B,GAAc,KAMtB,MADA,GAAI,GAAQ,EACL,GAET,EAAA,CAAA,EA8BA,EAAU,EAAA,GAAQ,SAtBtB,SAAkC,EAAA,CAChC,MAAO,CACL,GAAI,EAAS,EAAI,KAAA,EAAS,EAC1B,GAAI,EAAS,EAAI,KAAA,EAAS,EAC1B,GAAI,EAAS,EAAI,KAAA,EAAS,OAC1B,GAAI,EAAS,EAAI,KAAA,EAAS,EAAA,EAiBqB,EAAA,CAbnD,SAAkC,EAAA,CAChC,MAAO,CACL,GAAI,EAAS,EAAI,KAAA,EAAS,EAAS,EAAI,KAAA,EAAS,MAChD,GAAI,EAAS,EAAI,KAAA,EAAS,EAAS,EAAI,KAAA,EAAS,MAChD,GAAI,EACJ,GAAI,EAAS,EAAI,KAAA,EAAS,MAC1B,GAAI,EAAS,EAAI,KAAA,EAAS,MAC1B,GAAI,EAAS,EAAI,IAAA,EAAQ,MAAA,EAM8C,EAAA,CACvE,CAAA,GACK,EACH,cAAe,EAAmB,EAAA,CAAA,CAAA,CAAA,OAAA,KAAA"}