fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 6.59 kB
Source Map (JSON)
{"version":3,"file":"StrokeLineCapProjections.min.mjs","names":[],"sources":["../../../../../src/util/misc/projectStroke/StrokeLineCapProjections.ts"],"sourcesContent":["import type { XY } from '../../../Point';\nimport { Point } from '../../../Point';\nimport { getOrthonormalVector, getUnitVector } from '../vectors';\nimport { StrokeLineJoinProjections } from './StrokeLineJoinProjections';\nimport { StrokeProjectionsBase } from './StrokeProjectionsBase';\nimport type { TProjection, TProjectStrokeOnPointsOptions } from './types';\n\n/**\n * class in charge of finding projections for each type of line cap for start/end of an open path\n * @see {@link [Open path projections at #8344](https://github.com/fabricjs/fabric.js/pull/8344#1-open-path)}\n *\n * Reference:\n * - MDN:\n * - https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineCap\n * - https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linecap\n * - Spec: https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-linecap-dev\n * - Playground to understand how the line joins works: https://hypertolosana.github.io/efficient-webgl-stroking/index.html\n * - View the calculated projections for each of the control points: https://codesandbox.io/s/project-stroke-points-with-context-to-trace-b8jc4j?file=/src/index.js\n */\nexport class StrokeLineCapProjections extends StrokeProjectionsBase {\n /**\n * edge point\n */\n declare A: Point;\n /**\n * point next to edge point\n */\n declare T: Point;\n\n constructor(A: XY, T: XY, options: TProjectStrokeOnPointsOptions) {\n super(options);\n this.A = new Point(A);\n this.T = new Point(T);\n }\n\n calcOrthogonalProjection(\n from: Point,\n to: Point,\n magnitude: number = this.strokeProjectionMagnitude,\n ) {\n const vector = this.createSideVector(from, to);\n return this.scaleUnitVector(getOrthonormalVector(vector), magnitude);\n }\n\n /**\n * OPEN PATH START/END - Line cap: Butt\n * Calculation: to find the projections, just find the points orthogonal to the stroke\n *\n * @see https://github.com/fabricjs/fabric.js/pull/8344#1-1-butt\n */\n projectButt() {\n return [\n this.projectOrthogonally(this.A, this.T, this.strokeProjectionMagnitude),\n this.projectOrthogonally(this.A, this.T, -this.strokeProjectionMagnitude),\n ];\n }\n\n /**\n * OPEN PATH START/END - Line cap: Round\n * Calculation: same as stroke line join `round`\n *\n * @see https://github.com/fabricjs/fabric.js/pull/8344#1-2-round\n */\n projectRound() {\n const projections: Point[] = [];\n\n if (!this.isSkewed() && this.A.eq(this.T)) {\n /* 1 point case without `skew`\n When `strokeUniform` is true, scaling has no effect.\n So we divide by scale, to remove its effect.\n */\n const projection = new Point(1, 1)\n .scalarMultiply(this.strokeProjectionMagnitude)\n .multiply(this.strokeUniformScalar);\n projections.push(\n this.applySkew(this.A.add(projection)),\n this.applySkew(this.A.subtract(projection)),\n );\n } else {\n projections.push(\n ...new StrokeLineJoinProjections(\n this.A,\n this.T,\n this.T,\n this.options,\n ).projectRound(),\n );\n }\n\n return projections;\n }\n\n /**\n * OPEN PATH START/END - Line cap: Square\n * Calculation: project a rectangle of points on the stroke in the opposite direction of the vector `AT`\n *\n * @see https://github.com/fabricjs/fabric.js/pull/8344#1-3-square\n */\n projectSquare() {\n const projections: Point[] = [];\n\n if (this.A.eq(this.T)) {\n /* 1 point case without `skew`\n When `strokeUniform` is true, scaling has no effect.\n So we divide by scale, to remove its effect.\n */\n const projection = new Point(1, 1)\n .scalarMultiply(this.strokeProjectionMagnitude)\n .multiply(this.strokeUniformScalar);\n projections.push(this.A.add(projection), this.A.subtract(projection));\n } else {\n const orthogonalProjection = this.calcOrthogonalProjection(\n this.A,\n this.T,\n this.strokeProjectionMagnitude,\n );\n const strokePointingOut = this.scaleUnitVector(\n getUnitVector(this.createSideVector(this.A, this.T)),\n -this.strokeProjectionMagnitude,\n );\n const projectedA = this.A.add(strokePointingOut);\n projections.push(\n projectedA.add(orthogonalProjection),\n projectedA.subtract(orthogonalProjection),\n );\n }\n\n return projections.map((p) => this.applySkew(p));\n }\n\n protected projectPoints() {\n switch (this.options.strokeLineCap) {\n case 'round':\n return this.projectRound();\n case 'square':\n return this.projectSquare();\n default:\n return this.projectButt();\n }\n }\n\n public project(): TProjection[] {\n return this.projectPoints().map((point) => ({\n originPoint: this.A,\n projectedPoint: point,\n }));\n }\n}\n"],"mappings":"oRAmBA,IAAa,EAAb,cAA8C,CAAA,CAU5C,YAAY,EAAO,EAAO,EAAA,CACxB,MAAM,EAAA,CACN,KAAK,EAAI,IAAI,EAAM,EAAA,CACnB,KAAK,EAAI,IAAI,EAAM,EAAA,CAGrB,yBACE,EACA,EACA,EAAoB,KAAK,0BAAA,CAEzB,IAAM,EAAS,KAAK,iBAAiB,EAAM,EAAA,CAC3C,OAAO,KAAK,gBAAgB,EAAqB,EAAA,CAAS,EAAA,CAS5D,aAAA,CACE,MAAO,CACL,KAAK,oBAAoB,KAAK,EAAG,KAAK,EAAG,KAAK,0BAAA,CAC9C,KAAK,oBAAoB,KAAK,EAAG,KAAK,EAAA,CAAI,KAAK,0BAAA,CAAA,CAUnD,cAAA,CACE,IAAM,EAAuB,EAAA,CAE7B,GAAA,CAAK,KAAK,UAAA,EAAc,KAAK,EAAE,GAAG,KAAK,EAAA,CAAI,CAKzC,IAAM,EAAa,IAAI,EAAM,EAAG,EAAA,CAC7B,eAAe,KAAK,0BAAA,CACpB,SAAS,KAAK,oBAAA,CACjB,EAAY,KACV,KAAK,UAAU,KAAK,EAAE,IAAI,EAAA,CAAA,CAC1B,KAAK,UAAU,KAAK,EAAE,SAAS,EAAA,CAAA,CAAA,MAGjC,EAAY,KAAA,GACP,IAAI,EACL,KAAK,EACL,KAAK,EACL,KAAK,EACL,KAAK,QAAA,CACL,cAAA,CAAA,CAIN,OAAO,EAST,eAAA,CACE,IAAM,EAAuB,EAAA,CAE7B,GAAI,KAAK,EAAE,GAAG,KAAK,EAAA,CAAI,CAKrB,IAAM,EAAa,IAAI,EAAM,EAAG,EAAA,CAC7B,eAAe,KAAK,0BAAA,CACpB,SAAS,KAAK,oBAAA,CACjB,EAAY,KAAK,KAAK,EAAE,IAAI,EAAA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