UNPKG

fabric

Version:

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

34 lines (28 loc) 857 B
import type { XY } from '../Point'; /** * Parses "points" attribute, returning an array of values * @static * @memberOf fabric * @param {String} points points attribute string * @return {Array} array of points */ export function parsePointsAttribute(points: string | null): XY[] { // points attribute is required and must not be empty if (!points) { return []; } // replace commas with whitespace and remove bookending whitespace const pointsSplit: string[] = points.replace(/,/g, ' ').trim().split(/\s+/); const parsedPoints = []; for (let i = 0; i < pointsSplit.length; i += 2) { parsedPoints.push({ x: parseFloat(pointsSplit[i]), y: parseFloat(pointsSplit[i + 1]), }); } // odd number of points is an error // if (parsedPoints.length % 2 !== 0) { // return null; // } return parsedPoints; }