UNPKG

fabric

Version:

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

1 lines 3.34 kB
{"version":3,"file":"index.min.mjs","names":[],"sources":["../../../../../src/util/misc/projectStroke/index.ts"],"sourcesContent":["import { Point, type XY } from '../../../Point';\nimport { findIndexRight } from '../../internals/findRight';\nimport { StrokeLineCapProjections } from './StrokeLineCapProjections';\nimport { StrokeLineJoinProjections } from './StrokeLineJoinProjections';\nimport type { TProjection, TProjectStrokeOnPointsOptions } from './types';\n\nexport type * from './types';\n\n/**\n *\n * Used to calculate object's bounding box\n *\n * @see https://github.com/fabricjs/fabric.js/pull/8344\n *\n */\nexport const projectStrokeOnPoints = (\n points: XY[],\n options: TProjectStrokeOnPointsOptions,\n openPath = false,\n): TProjection[] => {\n const projections: TProjection[] = [];\n\n if (points.length === 0) {\n return projections;\n }\n\n // first we remove duplicate neighboring points\n const reduced = points.reduce(\n (reduced, point) => {\n if (!reduced[reduced.length - 1].eq(point)) {\n reduced.push(new Point(point));\n }\n return reduced;\n },\n [new Point(points[0])],\n );\n\n if (reduced.length === 1) {\n openPath = true;\n } else if (!openPath) {\n // remove points from end in case they equal the first point\n // in order to correctly project the first point\n const start = reduced[0];\n const index = findIndexRight(reduced, (point) => !point.eq(start));\n reduced.splice(index + 1);\n }\n\n reduced.forEach((A, index, points) => {\n let B: XY, C: XY;\n if (index === 0) {\n C = points[1];\n B = openPath ? A : points[points.length - 1];\n } else if (index === points.length - 1) {\n B = points[index - 1];\n C = openPath ? A : points[0];\n } else {\n B = points[index - 1];\n C = points[index + 1];\n }\n\n if (openPath && points.length === 1) {\n projections.push(\n ...new StrokeLineCapProjections(A, A, options).project(),\n );\n } else if (openPath && (index === 0 || index === points.length - 1)) {\n projections.push(\n ...new StrokeLineCapProjections(\n A,\n index === 0 ? C : B,\n options,\n ).project(),\n );\n } else {\n projections.push(\n ...new StrokeLineJoinProjections(A, B, C, options).project(),\n );\n }\n });\n\n return projections;\n};\n"],"mappings":"gRAeA,MAAa,GACX,EACA,EACA,EAAA,CAAW,IAAA,CAEX,IAAM,EAA6B,EAAA,CAEnC,GAAI,EAAO,SAAW,EACpB,OAAO,EAIT,IAAM,EAAU,EAAO,QACpB,EAAS,KACH,EAAQ,EAAQ,OAAS,GAAG,GAAG,EAAA,EAClC,EAAQ,KAAK,IAAI,EAAM,EAAA,CAAA,CAElB,GAET,CAAC,IAAI,EAAM,EAAO,GAAA,CAAA,CAAA,CAGpB,GAAI,EAAQ,SAAW,EACrB,EAAA,CAAW,UAAA,CACD,EAAU,CAGpB,IAAM,EAAQ,EAAQ,GAChB,EAAQ,EAAe,EAAU,GAAA,CAAW,EAAM,GAAG,EAAA,CAAA,CAC3D,EAAQ,OAAO,EAAQ,EAAA,CAmCzB,OAhCA,EAAQ,SAAS,EAAG,EAAO,IAAA,CACzB,IAAI,EAAO,EACP,IAAU,GACZ,EAAI,EAAO,GACX,EAAI,EAAW,EAAI,EAAO,EAAO,OAAS,IACjC,IAAU,EAAO,OAAS,GACnC,EAAI,EAAO,EAAQ,GACnB,EAAI,EAAW,EAAI,EAAO,KAE1B,EAAI,EAAO,EAAQ,GACnB,EAAI,EAAO,EAAQ,IAGjB,GAAY,EAAO,SAAW,EAChC,EAAY,KAAA,GACP,IAAI,EAAyB,EAAG,EAAG,EAAA,CAAS,SAAA,CAAA,CAAA,CAExC,GAAa,IAAU,GAAK,IAAU,EAAO,OAAS,EAS/D,EAAY,KAAA,GACP,IAAI,EAA0B,EAAG,EAAG,EAAG,EAAA,CAAS,SAAA,CAAA,CATrD,EAAY,KAAA,GACP,IAAI,EACL,EACA,IAAU,EAAI,EAAI,EAClB,EAAA,CACA,SAAA,CAAA,EAAA,CASD,GAAA,OAAA,KAAA"}