1 line
5.7 KiB
Plaintext
1 line
5.7 KiB
Plaintext
|
|
{"version":3,"file":"collect-point.mjs","names":[],"sources":["../../../extensions/aligning_guidelines/util/collect-point.ts"],"sourcesContent":["import type { FabricObject, Point } from 'fabric';\nimport type { AligningGuidelines } from '..';\nimport type { LineProps } from '../typedefs';\nimport { getDistanceList } from './basic';\n\ntype CollectPointProps = {\n target: FabricObject;\n /** Operation points of the target element: top-left, bottom-left, top-right, bottom-right */\n point: Point;\n /** Position using diagonal points when resizing/scaling. */\n diagonalPoint: Point;\n /** Set of points to consider for alignment: [tl, tr, br, bl, center] */\n list: Point[];\n /** Change the zoom or change the size, determine by whether e.transform.action starts with the string \"scale\" */\n isScale: boolean;\n /** Whether to change uniformly is determined by canvas.uniformScaling and canvas.uniScaleKey. */\n isUniform: boolean;\n /** When holding the centerKey (default is altKey), the shape will scale based on the center point, with the reference point being the center. */\n isCenter: boolean;\n /** tl、tr、br、bl、mt、mr、mb、ml */\n corner: string;\n};\n\nexport function collectVerticalPoint(\n this: AligningGuidelines,\n props: CollectPointProps,\n): LineProps[] {\n const {\n target,\n isScale,\n isUniform,\n corner,\n point,\n diagonalPoint,\n list,\n isCenter,\n } = props;\n const { dis, arr } = getDistanceList(point, list, 'x');\n const margin = this.margin / this.canvas.getZoom();\n if (dis > margin) return [];\n let v = arr[arr.length - 1].x - point.x;\n // tl bl ml\n // If modifying on the left side, the size decreases; conversely, it increases.\n const dirX = corner.includes('l') ? -1 : 1;\n v *= dirX;\n\n const { width, height, scaleX, scaleY } = target;\n // Because when modifying through the center point, isUniform is always false, so skew does not need to be considered.\n const dStrokeWidth = target.strokeUniform ? 0 : target.strokeWidth;\n const scaleWidth = scaleX * width + dStrokeWidth;\n const sx = (v + scaleWidth) / scaleWidth;\n // When v equals -scaleWidth, sx equals 0.\n if (sx == 0) return [];\n if (isScale) {\n target.set('scaleX', scaleX * sx);\n if (isUniform) target.set('scaleY', scaleY * sx);\n } else {\n target.set('width', width * sx);\n if (isUniform) target.set('height', height * sx);\n }\n if (isCenter) {\n target.setRelativeXY(diagonalPoint, 'center', 'center');\n } else {\n const originArr = this.contraryOriginMap;\n target.setRelativeXY(diagonalPoint, ...originArr[corner]);\n }\n target.setCoords();\n return arr.map((target) => ({ origin: point, target }));\n}\n\nexport function collectHorizontalPoint(\n this: AligningGuidelines,\n props: CollectPointProps,\n): LineProps[] {\n const {\n target,\n isScale,\n isUniform,\n corner,\n point,\n diagonalPoint,\n list,\n isCenter,\n } = props;\n const { dis, arr } = getDistanceList(point, list, 'y');\n const margin = this.margin / this.canvas.getZoom();\n if (dis > margin) return [];\n let v = arr[arr.length - 1].y - point.y;\n // tl mt tr\n // If modifying on the top side, the size decreases; conversely, it increases.\n const dirY = corner.includes('t') ? -1 : 1;\n v *= dirY;\n\n const { width, height, scaleX, scaleY } = target;\n // Because when modifying through the center point, isUniform is always false, so skew does not need to be considered.\n const dStrokeWidth = target.strokeUniform ? 0 : target.strokeWidth;\n const scaleHeight = scaleY * height + dStrokeWidth;\n const sy = (v + scaleHeight) / scaleHeight;\n // When v equals -scaleHeight, sy equals 0.\n if (sy == 0) return [];\n if (isScale) {\n target.set('scaleY', scaleY * sy);\n if (isUniform) target.set('scaleX', scaleX * sy);\n } else {\n target.set('height', height * sy);\n if (isUniform) target.set('width', width * sy);\n }\n if (isCenter) {\n target.setRelativeXY(diagonalPoint, 'center', 'center');\n } else {\n const ori
|