UNPKG

fabric

Version:

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

1 lines 1.8 kB
{"version":3,"file":"parsePointsAttribute.mjs","sources":["../../../src/parser/parsePointsAttribute.ts"],"sourcesContent":["import type { XY } from '../Point';\n\n/**\n * Parses \"points\" attribute, returning an array of values\n * @static\n * @memberOf fabric\n * @param {String} points points attribute string\n * @return {Array} array of points\n */\nexport function parsePointsAttribute(points: string | null): XY[] {\n // points attribute is required and must not be empty\n if (!points) {\n return [];\n }\n\n // replace commas with whitespace and remove bookending whitespace\n const pointsSplit: string[] = points.replace(/,/g, ' ').trim().split(/\\s+/);\n\n const parsedPoints = [];\n\n for (let i = 0; i < pointsSplit.length; i += 2) {\n parsedPoints.push({\n x: parseFloat(pointsSplit[i]),\n y: parseFloat(pointsSplit[i + 1]),\n });\n }\n\n // odd number of points is an error\n // if (parsedPoints.length % 2 !== 0) {\n // return null;\n // }\n return parsedPoints;\n}\n"],"names":["parsePointsAttribute","points","pointsSplit","replace","trim","split","parsedPoints","i","length","push","x","parseFloat","y"],"mappings":"AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,oBAAoBA,CAACC,MAAqB,EAAQ;AAChE;EACA,IAAI,CAACA,MAAM,EAAE;AACX,IAAA,OAAO,EAAE,CAAA;AACX,GAAA;;AAEA;AACA,EAAA,MAAMC,WAAqB,GAAGD,MAAM,CAACE,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAACC,IAAI,EAAE,CAACC,KAAK,CAAC,KAAK,CAAC,CAAA;EAE3E,MAAMC,YAAY,GAAG,EAAE,CAAA;AAEvB,EAAA,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGL,WAAW,CAACM,MAAM,EAAED,CAAC,IAAI,CAAC,EAAE;IAC9CD,YAAY,CAACG,IAAI,CAAC;AAChBC,MAAAA,CAAC,EAAEC,UAAU,CAACT,WAAW,CAACK,CAAC,CAAC,CAAC;MAC7BK,CAAC,EAAED,UAAU,CAACT,WAAW,CAACK,CAAC,GAAG,CAAC,CAAC,CAAA;AAClC,KAAC,CAAC,CAAA;AACJ,GAAA;;AAEA;AACA;AACA;AACA;AACA,EAAA,OAAOD,YAAY,CAAA;AACrB;;;;"}