From 627149053cc036b010c3a3ebce7bebc96d5242fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cchrisshank=E2=80=9D?= Date: Sun, 8 Sep 2024 15:32:59 -0700 Subject: [PATCH] fix moving target, make resize handlers parts --- src/canvas/spatial-geometry.ts | 63 +++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/src/canvas/spatial-geometry.ts b/src/canvas/spatial-geometry.ts index ba43ecf..ced4397 100644 --- a/src/canvas/spatial-geometry.ts +++ b/src/canvas/spatial-geometry.ts @@ -52,11 +52,11 @@ styles.replaceSync(` user-select: none; } -:host(:not(:focus-within)) [resize-handler], :host(:not(:focus-within)) [rotation-handler] { +:host(:not(:focus-within)) [part^="resize"], :host(:not(:focus-within)) [part="rotate"] { opacity: 0; } -[resize-handler] { +[part^="resize"] { display: block; position: absolute; box-sizing: border-box; @@ -64,10 +64,10 @@ styles.replaceSync(` background: hsl(210, 20%, 98%); z-index: calc(infinity); /* should the handlers always show? */ - &[resize-handler="top-left"], - &[resize-handler="top-right"], - &[resize-handler="bottom-right"], - &[resize-handler="bottom-left"] { + &[part="resize-nw"], + &[part="resize-ne"], + &[part="resize-se"], + &[part="resize-sw"] { width: 13px; aspect-ratio: 1; transform: translate(-50%, -50%); @@ -75,36 +75,36 @@ styles.replaceSync(` border-radius: 2px; } - &[resize-handler="top-left"] { + &[part="resize-nw"] { top: 0; left: 0; } - &[resize-handler="top-right"] { + &[part="resize-ne"] { top: 0; left: 100%; } - &[resize-handler="bottom-right"] { + &[part="resize-se"] { top: 100%; left: 100%; } - &[resize-handler="bottom-left"] { + &[part="resize-sw"] { top: 100%; left: 0; } - &[resize-handler="top-left"], &[resize-handler="bottom-right"] { + &[part="resize-nw"], &[part="resize-se"] { cursor: var(--fc-nwse-resize, nwse-resize) } - &[resize-handler="top-right"], &[resize-handler="bottom-left"] { + &[part="resize-ne"], &[part="resize-sw"] { cursor: var(--fc-nesw-resize, nesw-resize) } } -[rotation-handler] { +[part="rotate"] { display: block; position: absolute; box-sizing: border-box; @@ -121,7 +121,7 @@ styles.replaceSync(` cursor: url("data:image/svg+xml,") 16 16, pointer; } -[rotation-handler]::before { +[part="rotate"]::before { box-sizing: border-box; display: block; position: absolute; @@ -157,11 +157,11 @@ export class SpatialGeometry extends HTMLElement { // I can see it becoming important at scale shadowRoot.innerHTML = `
- - - - - + + + + +
`; } @@ -258,7 +258,12 @@ export class SpatialGeometry extends HTMLElement { case 'pointerdown': { if (event.button !== 0 || event.ctrlKey) return; - const target = event.composedPath()[0] as HTMLElement; + let target = event.composedPath()[0] as HTMLElement; + + // if a resize handler isn't interacted with then we should move the element. + if (!target.hasAttribute('part')) { + target = this; + } target.addEventListener('pointermove', this); target.setPointerCapture(event.pointerId); @@ -277,31 +282,33 @@ export class SpatialGeometry extends HTMLElement { return; } - const resizeDirection = target.getAttribute('resize-handler'); + const part = target.getAttribute('part'); - if (resizeDirection !== null) { + if (part === null) return; + + if (part.includes('resize')) { // This triggers a move and resize event :( - if (resizeDirection.includes('top')) { + if (part.includes('-n')) { this.y += event.movementY; this.height -= event.movementY; } - if (resizeDirection.includes('right')) { + if (part.endsWith('e')) { this.width += event.movementX; } - if (resizeDirection.includes('bottom')) { + if (part.includes('-s')) { this.height += event.movementY; } - if (resizeDirection.includes('left')) { + if (part.endsWith('w')) { this.x += event.movementX; this.width -= event.movementX; } return; } - if (target.hasAttribute('rotation-handler')) { + if (part === 'rotate') { const centerX = (this.#x + this.#width) / 2; const centerY = (this.#y + this.#height) / 2; var newAngle = @@ -310,7 +317,7 @@ export class SpatialGeometry extends HTMLElement { console.log(newAngle); this.rotate = newAngle; - // When a rotation handler is + // When a rotate handler is // newAngle = (Math.atan2(centerY - mouseY, centerX - mouseX) * 180) / Math.PI - currentAngle; return; }