import Konva from 'konva'; import { Render } from '../index'; import { GraphDraw, LinkDraw, RulerDraw, PreviewDraw, RefLineDraw, BgDraw } from '../draws'; export class PositionTool { private render: Render; constructor(render: Render) { this.render = render; } // 自适应大小 positionFit() { const { pageWidth, pageHeight } = this.render.getPageSettings(); const children = [ ...this.render.layer.getChildren(), ...this.render.layerCover.find('.link-line') ]; let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity; const stageState = this.render.getStageState(); for (const node of children) { if (node instanceof Konva.Group) { const { x, y, width, height } = ((rect) => ({ x: this.render.toStageValue(rect.x - stageState.x), y: this.render.toStageValue(rect.y - stageState.y), width: this.render.toStageValue(rect.width), height: this.render.toStageValue(rect.height) }))(node.getClientRect()); if (x < minX) minX = x; if (x + width > maxX) maxX = x + width; if (y < minY) minY = y; if (y + height > maxY) maxY = y + height; } else if (node instanceof Konva.Line && node.name() === 'link-line') { // 连线占用空间 const points = node.points(); for (let i = 0; i < points.length; i += 2) { const [px, py] = [points[i], points[i + 1]]; if (px < minX) minX = px - 1; if (px > maxX) maxX = px + 1; if (py < minY) minY = py - 1; if (py > maxY) maxY = py + 1; } } } // 把画布区域也纳入计算范围(画布从 (0,0) 到 (pageWidth, pageHeight)) if (0 < minX) minX = 0; if (pageWidth > maxX) maxX = pageWidth; if (0 < minY) minY = 0; if (pageHeight > maxY) maxY = pageHeight; // 没有素材时,以画布自身为准 if (minX === Infinity) minX = 0; if (maxX === -Infinity) maxX = pageWidth; if (minY === Infinity) minY = 0; if (maxY === -Infinity) maxY = pageHeight; const contentSize = { width: maxX - minX, height: maxY - minY }; const contentRate = contentSize.width / contentSize.height; const viewSize = { width: this.render.stage.width() - (this.render.config.readonly ? 0 : this.render.rulerSize), height: this.render.stage.height() - (this.render.config.readonly ? 0 : this.render.rulerSize) }; const viewRate = viewSize.width / viewSize.height; let scale = 1; // 设置padding,避免边缘紧贴 const padding = 20; const effectiveWidth = viewSize.width - padding * 2; const effectiveHeight = viewSize.height - padding * 2; if (viewRate > contentRate) { // 视口比内容更"扁",以高度为基准 scale = effectiveHeight / contentSize.height; } else { // 视口比内容更"长",以宽度为基准 scale = effectiveWidth / contentSize.width; } scale = Math.max(0.2, scale); scale = Math.floor(scale * 100) / 100; // 设置缩放比例最大为1(暂时隐藏) // if (contentSize.width * scale <= viewSize.width && contentSize.height * scale <= viewSize.height) { // scale = Math.min(1, scale); // } this.render.stage.setAttrs({ scale: { x: scale, y: scale }, position: { x: (this.render.config.readonly ? 0 : this.render.rulerSize) - minX * scale + (viewSize.width - contentSize.width * scale) / 2, y: (this.render.config.readonly ? 0 : this.render.rulerSize) - minY * scale + (viewSize.height - contentSize.height * scale) / 2 } }); this.render.emit('scale-change', scale); // 重绘 this.render.redraw([ BgDraw.NAME, GraphDraw.NAME, LinkDraw.NAME, RulerDraw.NAME, RefLineDraw.NAME, PreviewDraw.NAME ]); } // 恢复位置大小 positionZoomReset() { this.render.stage.setAttrs({ scale: { x: 1, y: 1 } }); this.render.emit('scale-change', 1); this.positionReset(); } // 恢复位置 positionReset() { this.render.stage.setAttrs({ x: this.render.rulerSize, y: this.render.rulerSize }); // 重绘 this.render.redraw([ BgDraw.NAME, GraphDraw.NAME, LinkDraw.NAME, RulerDraw.NAME, RefLineDraw.NAME, PreviewDraw.NAME ]); } // 更新中心位置 updateCenter(x = 0, y = 0) { // stage 状态 const stageState = this.render.getStageState(); // 提取节点 const nodes = this.render.layer.getChildren((node) => !this.render.ignore(node)); // 计算节点占用的区域(计算起点即可) let minX = 0; let minY = 0; for (const node of nodes) { const x = node.x(); const y = node.y(); if (x < minX) { minX = x; } if (y < minY) { minY = y; } } // 居中画布 this.render.stage.setAttrs({ x: stageState.width / 2 - this.render.toBoardValue(minX) - this.render.toBoardValue(x) + this.render.rulerSize, y: stageState.height / 2 - this.render.toBoardValue(minY) - this.render.toBoardValue(y) + this.render.rulerSize }); // 重绘 this.render.redraw([ BgDraw.NAME, GraphDraw.NAME, LinkDraw.NAME, RulerDraw.NAME, RefLineDraw.NAME, PreviewDraw.NAME ]); } }