fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 5.85 kB
Source Map (JSON)
{"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 originArr = this.contraryOriginMap;\n target.setRelativeXY(diagonalPoint, ...originArr[corner]);\n }\n target.setCoords();\n return arr.map((target) => ({ origin: point, target }));\n}\n"],"mappings":";;AAuBA,SAAgB,qBAEd,OACa;CACb,MAAM,EACJ,QACA,SACA,WACA,QACA,OACA,eACA,MACA,aACE;CACJ,MAAM,EAAE,KAAK,QAAQ,gBAAgB,OAAO,MAAM,IAAI;AAEtD,KAAI,MADW,KAAK,SAAS,KAAK,OAAO,SAAS,CAChC,QAAO,EAAE;CAC3B,IA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