UNPKG

fabric

Version:

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

1 lines 3.07 kB
{"version":3,"file":"collect-line.mjs","names":[],"sources":["../../../extensions/aligning_guidelines/util/collect-line.ts"],"sourcesContent":["import type { FabricObject, Point, TOriginX, TOriginY } from 'fabric';\nimport type { AligningGuidelines } from '..';\nimport type { LineProps } from '../typedefs';\nimport { getDistanceList } from './basic';\n\nexport function collectLine(\n this: AligningGuidelines,\n target: FabricObject,\n points: Point[],\n) {\n const list = target.getCoords();\n list.push(target.getCenterPoint());\n const margin = this.margin / this.canvas.getZoom();\n const opts = { target, list, points, margin };\n const vLines = collectPoints({ ...opts, type: 'x' });\n const hLines = collectPoints({ ...opts, type: 'y' });\n\n return { vLines, hLines };\n}\n\ntype CollectItemLineProps = {\n target: FabricObject;\n list: Point[];\n points: Point[];\n margin: number;\n type: 'x' | 'y';\n};\nconst originArr: [TOriginX, TOriginY][] = [\n ['left', 'top'],\n ['right', 'top'],\n ['right', 'bottom'],\n ['left', 'bottom'],\n ['center', 'center'],\n];\nfunction collectPoints(props: CollectItemLineProps) {\n const { target, list, points, margin, type } = props;\n const res: LineProps[] = [];\n const arr: ReturnType<typeof getDistanceList>[] = [];\n let min = Infinity;\n for (const item of list) {\n const o = getDistanceList(item, points, type);\n arr.push(o);\n if (min > o.dis) min = o.dis;\n }\n if (min > margin) return res;\n let b = false;\n for (let i = 0; i < list.length; i++) {\n if (arr[i].dis != min) continue;\n for (const item of arr[i].arr) {\n res.push({ origin: list[i], target: item });\n }\n\n if (b) continue;\n b = true;\n const d = arr[i].arr[0][type] - list[i][type];\n // It will change the original data, and the next time we collect y, use the modified data.\n list.forEach((item) => {\n item[type] += d;\n });\n target.setXY(list[i], ...originArr[i]);\n target.setCoords();\n }\n\n return res;\n}\n"],"mappings":";;AAKA,SAAgB,YAEd,QACA,QACA;CACA,MAAM,OAAO,OAAO,WAAW;AAC/B,MAAK,KAAK,OAAO,gBAAgB,CAAC;CAEl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