Skip to content

AI-native design editor. Open-source Figma alternative.

Highlight objects on hover (Figma-style)

Danila Poyarkovb10d42729601
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.ts
index 2f8c20d31..bfb06a94d 100644
--- a/src/composables/use-canvas-input.ts
+++ b/src/composables/use-canvas-input.ts
@@ -215,6 +215,7 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
}
function onMouseDown(e: MouseEvent) {
+ store.setHoveredNode(null)
const { sx, sy, cx, cy } = getCoords(e)
const tool = store.state.activeTool
@@ -423,9 +424,9 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
store.requestRender()
}
- // Cursor on hover
+ // Cursor + hover highlight
if (!drag.value && store.state.activeTool === 'SELECT') {
- const { sx, sy } = getCoords(e)
+ const { sx, sy, cx, cy } = getCoords(e)
let cursor: string | null = null
// Rotation handle cursor
@@ -477,6 +478,9 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
}
}
cursorOverride.value = cursor
+
+ const hit = store.graph.hitTest(cx, cy)
+ store.setHoveredNode(hit && !store.state.selectedIds.has(hit.id) ? hit.id : null)
}
if (!drag.value) return
@@ -914,7 +918,10 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
useEventListener(canvasRef, 'mousedown', onMouseDown)
useEventListener(canvasRef, 'mousemove', onMouseMove)
useEventListener(canvasRef, 'mouseup', onMouseUp)
- useEventListener(canvasRef, 'mouseleave', onMouseUp)
+ useEventListener(canvasRef, 'mouseleave', () => {
+ onMouseUp()
+ store.setHoveredNode(null)
+ })
useEventListener(canvasRef, 'wheel', onWheel, { passive: false })
return {
diff --git a/src/composables/use-canvas.ts b/src/composables/use-canvas.ts
index 028d1dd57..7a95b16d5 100644
--- a/src/composables/use-canvas.ts
+++ b/src/composables/use-canvas.ts
@@ -58,6 +58,7 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Edito
renderer.showRulers = !new URLSearchParams(window.location.search).has('no-rulers')
renderer.pageColor = store.state.pageColor
renderer.render(store.graph, store.state.selectedIds, {
+ hoveredNodeId: store.state.hoveredNodeId,
editingTextId: store.state.editingTextId,
marquee: store.state.marquee,
snapGuides: store.state.snapGuides,
diff --git a/src/engine/renderer.ts b/src/engine/renderer.ts
index 422c9da3d..9389dbb15 100644
--- a/src/engine/renderer.ts
+++ b/src/engine/renderer.ts
@@ -28,6 +28,7 @@ import type {
} from 'canvaskit-wasm'
export interface RenderOverlays {
+ hoveredNodeId?: string | null
editingTextId?: string | null
marquee?: { x: number; y: number; width: number; height: number } | null
snapGuides?: SnapGuide[]
@@ -157,6 +158,7 @@ export class SkiaRenderer {
canvas.save()
canvas.scale(this.dpr, this.dpr)
+ this.drawHoverHighlight(canvas, graph, overlays.hoveredNodeId)
this.drawSelection(canvas, graph, selectedIds, overlays)
this.drawSnapGuides(canvas, overlays.snapGuides)
this.drawMarquee(canvas, overlays.marquee)
@@ -168,6 +170,31 @@ export class SkiaRenderer {
this.surface.flush()
}
+ // --- Hover highlight ---
+
+ private drawHoverHighlight(
+ canvas: Canvas,
+ graph: SceneGraph,
+ hoveredNodeId?: string | null
+ ): void {
+ if (!hoveredNodeId) return
+ const node = graph.getNode(hoveredNodeId)
+ if (!node) return
+
+ const abs = graph.getAbsolutePosition(hoveredNodeId)
+ const cx = (abs.x + node.width / 2) * this.zoom + this.panX
+ const cy = (abs.y + node.height / 2) * this.zoom + this.panY
+ const hw = (node.width / 2) * this.zoom
+ const hh = (node.height / 2) * this.zoom
+
+ canvas.save()
+ if (node.rotation !== 0) {
+ canvas.rotate(node.rotation, cx, cy)
+ }
+ canvas.drawRect(this.ck.LTRBRect(cx - hw, cy - hh, cx + hw, cy + hh), this.selectionPaint)
+ canvas.restore()
+ }
+
// --- Selection UI ---
private drawSelection(
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index 2c86ecc3f..fbffc3a06 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -79,6 +79,7 @@ export function createEditorStore() {
length: number
direction: 'HORIZONTAL' | 'VERTICAL'
} | null,
+ hoveredNodeId: null as string | null,
editingTextId: null as string | null,
penState: null as {
vertices: VectorVertex[]
@@ -154,6 +155,12 @@ export function createEditorStore() {
requestRender()
}
+ function setHoveredNode(id: string | null) {
+ if (state.hoveredNodeId === id) return
+ state.hoveredNodeId = id
+ requestRender()
+ }
+
function setDropTarget(id: string | null) {
state.dropTargetId = id
requestRender()
@@ -1037,6 +1044,7 @@ export function createEditorStore() {
setMarquee,
setSnapGuides,
setRotationPreview,
+ setHoveredNode,
setDropTarget,
setLayoutInsertIndicator,
reorderInAutoLayout,