UNPKG

@antv/x6

Version:

JavaScript diagramming library that uses SVG and HTML for rendering

98 lines 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoordManager = void 0; const geometry_1 = require("../geometry"); const common_1 = require("../common"); const base_1 = require("./base"); class CoordManager extends base_1.Base { getClientMatrix() { return common_1.Dom.createSVGMatrix(this.view.stage.getScreenCTM()); } /** * Returns coordinates of the graph viewport, relative to the window. */ getClientOffset() { // see: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect const rect = this.view.svg.getBoundingClientRect(); return new geometry_1.Point(rect.left, rect.top); } /** * Returns coordinates of the graph viewport, relative to the document. */ getPageOffset() { // see: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect return this.getClientOffset().translate(window.scrollX, window.scrollY); } snapToGrid(x, y) { const p = typeof x === 'number' ? this.clientToLocalPoint(x, y) : this.clientToLocalPoint(x.x, x.y); return p.snapToGrid(this.graph.getGridSize()); } localToGraphPoint(x, y) { const localPoint = geometry_1.Point.create(x, y); return common_1.Util.transformPoint(localPoint, this.graph.matrix()); } localToClientPoint(x, y) { const localPoint = geometry_1.Point.create(x, y); return common_1.Util.transformPoint(localPoint, this.getClientMatrix()); } localToPagePoint(x, y) { const p = typeof x === 'number' ? this.localToGraphPoint(x, y) : this.localToGraphPoint(x); return p.translate(this.getPageOffset()); } localToGraphRect(x, y, width, height) { const localRect = geometry_1.Rectangle.create(x, y, width, height); return common_1.Util.transformRectangle(localRect, this.graph.matrix()); } localToClientRect(x, y, width, height) { const localRect = geometry_1.Rectangle.create(x, y, width, height); return common_1.Util.transformRectangle(localRect, this.getClientMatrix()); } localToPageRect(x, y, width, height) { const rect = typeof x === 'number' ? this.localToGraphRect(x, y, width, height) : this.localToGraphRect(x); return rect.translate(this.getPageOffset()); } graphToLocalPoint(x, y) { const graphPoint = geometry_1.Point.create(x, y); return common_1.Util.transformPoint(graphPoint, this.graph.matrix().inverse()); } clientToLocalPoint(x, y) { const clientPoint = geometry_1.Point.create(x, y); return common_1.Util.transformPoint(clientPoint, this.getClientMatrix().inverse()); } clientToGraphPoint(x, y) { const clientPoint = geometry_1.Point.create(x, y); return common_1.Util.transformPoint(clientPoint, this.graph.matrix().multiply(this.getClientMatrix().inverse())); } pageToLocalPoint(x, y) { const pagePoint = geometry_1.Point.create(x, y); const graphPoint = pagePoint.diff(this.getPageOffset()); return this.graphToLocalPoint(graphPoint); } graphToLocalRect(x, y, width, height) { const graphRect = geometry_1.Rectangle.create(x, y, width, height); return common_1.Util.transformRectangle(graphRect, this.graph.matrix().inverse()); } clientToLocalRect(x, y, width, height) { const clientRect = geometry_1.Rectangle.create(x, y, width, height); return common_1.Util.transformRectangle(clientRect, this.getClientMatrix().inverse()); } clientToGraphRect(x, y, width, height) { const clientRect = geometry_1.Rectangle.create(x, y, width, height); return common_1.Util.transformRectangle(clientRect, this.graph.matrix().multiply(this.getClientMatrix().inverse())); } pageToLocalRect(x, y, width, height) { const graphRect = geometry_1.Rectangle.create(x, y, width, height); const pageOffset = this.getPageOffset(); graphRect.x -= pageOffset.x; graphRect.y -= pageOffset.y; return this.graphToLocalRect(graphRect); } } exports.CoordManager = CoordManager; //# sourceMappingURL=coord.js.map