From 9a57a09cd35f24dd12985e19c2fd633e92c01835 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 25 Aug 2023 13:47:03 +0400 Subject: [PATCH] Up CanvasView --- src/canvas/index.ts | 4 ++++ src/canvas/view/CanvasView.ts | 34 ++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/canvas/index.ts b/src/canvas/index.ts index 1c2db7218..d044215ad 100644 --- a/src/canvas/index.ts +++ b/src/canvas/index.ts @@ -728,6 +728,10 @@ export default class CanvasModule extends Module { ['model', 'droppable'].forEach(i => (this[i] = {})); } + getRectToScreen(boxRect: Parameters[0]) { + return this.canvasView?.getRectToScreen(boxRect); + } + addSpot(props: Omit & { id?: string }, opts: AddOptions = {}) { const spotProps = props as T; const spots = this.getSpots(spotProps); diff --git a/src/canvas/view/CanvasView.ts b/src/canvas/view/CanvasView.ts index 19d9e026a..78fbf1a51 100644 --- a/src/canvas/view/CanvasView.ts +++ b/src/canvas/view/CanvasView.ts @@ -352,6 +352,21 @@ export default class CanvasView extends ModuleView { }; } + getRectToScreen(boxRect: Partial): BoxRect { + const zoom = this.module.getZoomDecimal(); + const coords = this.module.getCoords(); + const vwDelta = this.getViewportDelta(); + const x = (boxRect.x ?? 0) * zoom + coords.x + vwDelta.x || 0; + const y = (boxRect.y ?? 0) * zoom + coords.y + vwDelta.y || 0; + + return { + x, + y, + width: (boxRect.width ?? 0) * zoom, + height: (boxRect.height ?? 0) * zoom, + }; + } + getElBoxRect(el: HTMLElement, opts: GetBoxRectOptions = {}): BoxRect { const { module } = this; const { width, height, left, top } = getElRect(el); @@ -364,27 +379,14 @@ export default class CanvasView extends ModuleView { const docScroll = getDocumentScroll(); const xWithFrame = left + frameX + (canvasEl.scrollLeft + docScroll.x) * zoomMlt; const yWithFrame = top + frameY + (canvasEl.scrollTop + docScroll.y) * zoomMlt; - - if (opts.toScreen) { - const zoom = module.getZoomDecimal(); - const vwDelta = this.getViewportDelta(); - const x = xWithFrame * zoom + vwDelta.x || 0; - const y = yWithFrame * zoom + vwDelta.y || 0; - - return { - x, - y, - width: width * zoom, - height: height * zoom, - }; - } - - return { + const boxRect = { x: xWithFrame, y: yWithFrame, width, height, }; + + return opts.toScreen ? this.getRectToScreen(boxRect) : boxRect; } getViewportRect(opts: ToWorldOption = {}): BoxRect {