Skip to content

Commit 1461e37

Browse files
authored
perf: avoid calling getBoundingClientRect too often in useSelectionToolboxPosition (#5976)
┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-5976-perf-avoid-calling-getBoundingClientRect-too-often-in-useSelectionToolboxPosition-2866d73d365081639755d8087e80377c) by [Unito](https://www.unito.io)
1 parent 874ef3b commit 1461e37

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/composables/canvas/useSelectionToolboxPosition.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRafFn } from '@vueuse/core'
1+
import { useElementBounding, useRafFn } from '@vueuse/core'
22
import { computed, onUnmounted, ref, watch } from 'vue'
33
import type { Ref } from 'vue'
44

@@ -57,6 +57,11 @@ export function useSelectionToolboxPosition(
5757

5858
const visible = ref(false)
5959

60+
// Use VueUse to reactively track canvas bounding rect
61+
const { left: canvasLeft, top: canvasTop } = useElementBounding(
62+
lgCanvas.canvas
63+
)
64+
6065
/**
6166
* Update position based on selection
6267
*/
@@ -114,11 +119,11 @@ export function useSelectionToolboxPosition(
114119
if (!visible.value) return
115120

116121
const { scale, offset } = lgCanvas.ds
117-
const canvasRect = lgCanvas.canvas.getBoundingClientRect()
118122

119123
const screenX =
120-
(worldPosition.value.x + offset[0]) * scale + canvasRect.left
121-
const screenY = (worldPosition.value.y + offset[1]) * scale + canvasRect.top
124+
(worldPosition.value.x + offset[0]) * scale + canvasLeft.value
125+
const screenY =
126+
(worldPosition.value.y + offset[1]) * scale + canvasTop.value
122127

123128
// Update CSS custom properties directly for best performance
124129
if (toolboxRef.value) {

0 commit comments

Comments
 (0)