UNPKG

fabric

Version:

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

1 lines 3.36 kB
{"version":3,"file":"StrokeProjectionsBase.min.mjs","names":[],"sources":["../../../../../src/util/misc/projectStroke/StrokeProjectionsBase.ts"],"sourcesContent":["import type { XY } from '../../../Point';\nimport { Point } from '../../../Point';\nimport { degreesToRadians } from '../radiansDegreesConversion';\nimport { createVector } from '../vectors';\nimport type { TProjectStrokeOnPointsOptions, TProjection } from './types';\n\n/**\n * @see https://github.com/fabricjs/fabric.js/pull/8344\n * @todo consider removing skewing from points before calculating stroke projection,\n * see https://github.com/fabricjs/fabric.js/commit/494a10ee2f8c2278ae9a55b20bf50cf6ee25b064#commitcomment-94751537\n */\nexport abstract class StrokeProjectionsBase {\n declare options: TProjectStrokeOnPointsOptions;\n declare scale: Point;\n declare strokeUniformScalar: Point;\n declare strokeProjectionMagnitude: number;\n\n constructor(options: TProjectStrokeOnPointsOptions) {\n this.options = options;\n this.strokeProjectionMagnitude = this.options.strokeWidth / 2;\n this.scale = new Point(this.options.scaleX, this.options.scaleY);\n this.strokeUniformScalar = this.options.strokeUniform\n ? new Point(1 / this.options.scaleX, 1 / this.options.scaleY)\n : new Point(1, 1);\n }\n\n /**\n * When the stroke is uniform, scaling affects the arrangement of points. So we must take it into account.\n */\n protected createSideVector(from: XY, to: XY) {\n const v = createVector(from, to);\n return this.options.strokeUniform ? v.multiply(this.scale) : v;\n }\n\n protected abstract calcOrthogonalProjection(\n from: Point,\n to: Point,\n magnitude?: number,\n ): Point;\n\n protected projectOrthogonally(from: Point, to: Point, magnitude?: number) {\n return this.applySkew(\n from.add(this.calcOrthogonalProjection(from, to, magnitude)),\n );\n }\n\n protected isSkewed() {\n return this.options.skewX !== 0 || this.options.skewY !== 0;\n }\n\n protected applySkew(point: Point) {\n const p = new Point(point);\n // skewY must be applied before skewX as this distortion affects skewX calculation\n p.y += p.x * Math.tan(degreesToRadians(this.options.skewY));\n p.x += p.y * Math.tan(degreesToRadians(this.options.skewX));\n return p;\n }\n\n protected scaleUnitVector(unitVector: Point, scalar: number) {\n return unitVector.multiply(this.strokeUniformScalar).scalarMultiply(scalar);\n }\n\n protected abstract projectPoints(): Point[];\n\n public abstract project(): TProjection[];\n}\n"],"mappings":"wKAWA,IAAsB,EAAtB,KAAA,CAME,YAAY,EAAA,CACV,KAAK,QAAU,EACf,KAAK,0BAA4B,KAAK,QAAQ,YAAc,EAC5D,KAAK,MAAQ,IAAI,EAAM,KAAK,QAAQ,OAAQ,KAAK,QAAQ,OAAA,CACzD,KAAK,oBAAsB,KAAK,QAAQ,cACpC,IAAI,EAAM,EAAI,KAAK,QAAQ,OAAQ,EAAI,KAAK,QAAQ,OAAA,CACpD,IAAI,EAAM,EAAG,EAAA,CAMnB,iBAA2B,EAAU,EAAA,CACnC,IAAM,EAAI,EAAa,EAAM,EAAA,CAC7B,OAAO,KAAK,QAAQ,cAAgB,EAAE,SAAS,KAAK,MAAA,CAAS,EAS/D,oBAA8B,EAAa,EAAW,EAAA,CACpD,OAAO,KAAK,UACV,EAAK,IAAI,KAAK,yBAAyB,EAAM,EAAI,EAAA,CAAA,CAAA,CAIrD,UAAA,CACE,OAAO,KAAK,QAAQ,QAAU,GAAK,KAAK,QAAQ,QAAU,EAG5D,UAAoB,EAAA,CAClB,IAAM,EAAI,IAAI,EAAM,EAAA,CAIpB,MAFA,GAAE,GAAK,EAAE,EAAI,KAAK,IAAI,EAAiB,KAAK,QAAQ,MAAA,CAAA,CACpD,EAAE,GAAK,EAAE,EAAI,KAAK,IAAI,EAAiB,KAAK,QAAQ,MAAA,CAAA,CAC7C,EAGT,gBAA0B,EAAmB,EAAA,CAC3C,OAAO,EAAW,SAAS,KAAK,oBAAA,CAAqB,eAAe,EAAA,GAAA,OAAA,KAAA"}