@tldraw/editor
Version:
tldraw infinite canvas SDK (editor).
8 lines (7 loc) • 2.48 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/lib/utils/getSvgPathFromPoints.ts"],
"sourcesContent": ["import { VecLike } from '../primitives/Vec'\nimport { average, precise } from '../primitives/utils'\n\n/**\n * Turn an array of points into a path of quadradic curves.\n *\n * @param points - The points returned from perfect-freehand\n * @param closed - Whether the stroke is closed\n *\n * @public\n */\nexport function getSvgPathFromPoints(points: VecLike[], closed = true): string {\n\tconst len = points.length\n\n\tif (len < 2) {\n\t\treturn ''\n\t}\n\n\tlet a = points[0]\n\tlet b = points[1]\n\n\tif (len === 2) {\n\t\t// If only two points, just draw a line\n\t\treturn `M${precise(a)}L${precise(b)}`\n\t}\n\n\tlet result = ''\n\n\tfor (let i = 2, max = len - 1; i < max; i++) {\n\t\ta = points[i]\n\t\tb = points[i + 1]\n\t\tresult += average(a, b)\n\t}\n\n\tif (closed) {\n\t\t// If closed, draw a curve from the last point to the first\n\t\treturn `M${average(points[0], points[1])}Q${precise(points[1])}${average(\n\t\t\tpoints[1],\n\t\t\tpoints[2]\n\t\t)}T${result}${average(points[len - 1], points[0])}${average(points[0], points[1])}Z`\n\t} else {\n\t\t// If not closed, draw a curve starting at the first point and\n\t\t// ending at the midpoint of the last and second-last point, then\n\t\t// complete the curve with a line segment to the last point.\n\t\treturn `M${precise(points[0])}Q${precise(points[1])}${average(points[1], points[2])}${\n\t\t\tpoints.length > 3 ? 'T' : ''\n\t\t}${result}L${precise(points[len - 1])}`\n\t}\n}\n"],
"mappings": "AACA,SAAS,SAAS,eAAe;AAU1B,SAAS,qBAAqB,QAAmB,SAAS,MAAc;AAC9E,QAAM,MAAM,OAAO;AAEnB,MAAI,MAAM,GAAG;AACZ,WAAO;AAAA,EACR;AAEA,MAAI,IAAI,OAAO,CAAC;AAChB,MAAI,IAAI,OAAO,CAAC;AAEhB,MAAI,QAAQ,GAAG;AAEd,WAAO,IAAI,QAAQ,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC;AAAA,EACpC;AAEA,MAAI,SAAS;AAEb,WAAS,IAAI,GAAG,MAAM,MAAM,GAAG,IAAI,KAAK,KAAK;AAC5C,QAAI,OAAO,CAAC;AACZ,QAAI,OAAO,IAAI,CAAC;AAChB,cAAU,QAAQ,GAAG,CAAC;AAAA,EACvB;AAEA,MAAI,QAAQ;AAEX,WAAO,IAAI,QAAQ,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,QAAQ,OAAO,CAAC,CAAC,CAAC,GAAG;AAAA,MAChE,OAAO,CAAC;AAAA,MACR,OAAO,CAAC;AAAA,IACT,CAAC,IAAI,MAAM,GAAG,QAAQ,OAAO,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,QAAQ,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;AAAA,EAClF,OAAO;AAIN,WAAO,IAAI,QAAQ,OAAO,CAAC,CAAC,CAAC,IAAI,QAAQ,OAAO,CAAC,CAAC,CAAC,GAAG,QAAQ,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAClF,OAAO,SAAS,IAAI,MAAM,EAC3B,GAAG,MAAM,IAAI,QAAQ,OAAO,MAAM,CAAC,CAAC,CAAC;AAAA,EACtC;AACD;",
"names": []
}