From 723a0e4053670ee54950377156f425c617268a65 Mon Sep 17 00:00:00 2001 From: simonbethke Date: Thu, 7 Nov 2024 13:45:42 +0100 Subject: [PATCH] Allow to change the brush-size with the mouse-wheel --- src/tools/brush-selection.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/tools/brush-selection.ts b/src/tools/brush-selection.ts index 71c5ad9..95b2c3e 100644 --- a/src/tools/brush-selection.ts +++ b/src/tools/brush-selection.ts @@ -24,6 +24,7 @@ class BrushSelection { const prev = { x: 0, y: 0 }; let dragId: number | undefined; + let wheelModeBrushSize = false; const update = (e: PointerEvent) => { const x = e.offsetX; @@ -104,12 +105,37 @@ class BrushSelection { } }; + const wheel = (e: WheelEvent) => { + if(wheelModeBrushSize){ + if(e.deltaY > 0) + radius = Math.max(1, radius * 1.05); + else + radius = Math.max(1, radius / 1.05); + circle.setAttribute('r', radius.toString()); + e.preventDefault(); + e.stopPropagation(); + } + }; + + const keydown = (e: KeyboardEvent) => { + if (e.shiftKey) { + wheelModeBrushSize = true; + } + }; + + const keyup = (e: KeyboardEvent) => { + wheelModeBrushSize = false; + }; + this.activate = () => { svg.style.display = 'inline'; parent.style.display = 'block'; parent.addEventListener('pointerdown', pointerdown); parent.addEventListener('pointermove', pointermove); parent.addEventListener('pointerup', pointerup); + parent.addEventListener('wheel', wheel); + document.addEventListener('keydown', keydown); + document.addEventListener('keyup', keyup); }; this.deactivate = () => { @@ -122,6 +148,10 @@ class BrushSelection { parent.removeEventListener('pointerdown', pointerdown); parent.removeEventListener('pointermove', pointermove); parent.removeEventListener('pointerup', pointerup); + parent.removeEventListener('wheel', wheel); + document.removeEventListener('keydown', keydown); + document.removeEventListener('keyup', keyup); + wheelModeBrushSize = false; }; events.on('tool.brushSelection.smaller', () => {