From 37a532236bfd32a61d1777353e507077af617621 Mon Sep 17 00:00:00 2001 From: unsettledgames <47360416+unsettledgames@users.noreply.github.com> Date: Thu, 17 Sep 2020 16:11:00 +0200 Subject: [PATCH] Fixed brush preview snapping --- js/_getCursorPosition.js | 2 +- js/_mouseEvents.js | 13 ++++++++++--- js/_tools.js | 10 ++++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/js/_getCursorPosition.js b/js/_getCursorPosition.js index 0452e16..4c9cdca 100644 --- a/js/_getCursorPosition.js +++ b/js/_getCursorPosition.js @@ -2,7 +2,7 @@ function getCursorPosition(e) { var x; var y; - + if (e.pageX != undefined && e.pageY != undefined) { x = e.pageX; y = e.pageY; diff --git a/js/_mouseEvents.js b/js/_mouseEvents.js index 6342030..de0a386 100644 --- a/js/_mouseEvents.js +++ b/js/_mouseEvents.js @@ -157,16 +157,23 @@ window.addEventListener("mouseup", function (mouseEvent) { }, false); // TODO: Make it snap to the pixel grid -function setPreviewPosition(preview, cursor, size){ +function setPreviewPosition(preview, size){ + let toAdd = 0; + + // This prevents the brush to be placed in the middle of pixels + if (size % 2 == 0) { + toAdd = 0.5; + } + preview.style.left = ( currentLayer.canvas.offsetLeft + Math.floor(cursor[0]/zoom) * zoom - - Math.floor(size / 2) * zoom + - Math.floor(size / 2) * zoom + toAdd ) + 'px'; preview.style.top = ( currentLayer.canvas.offsetTop + Math.floor(cursor[1]/zoom) * zoom - - Math.floor(size / 2) * zoom + - Math.floor(size / 2) * zoom + toAdd ) + 'px'; } diff --git a/js/_tools.js b/js/_tools.js index 335c09a..3c10d70 100644 --- a/js/_tools.js +++ b/js/_tools.js @@ -95,8 +95,14 @@ class Tool { } moveBrushPreview(cursorLocation) { - brushPreview.style.left = (Math.ceil(cursorLocation[0] / zoom) * zoom + currentLayer.canvas.offsetLeft - this.currentBrushSize * zoom / 2 - zoom / 2) + 'px'; - brushPreview.style.top = (Math.ceil(cursorLocation[1] / zoom) * zoom + currentLayer.canvas.offsetTop - this.currentBrushSize * zoom / 2 - zoom / 2) + 'px'; + let toSub = 0; + // Prevents the brush to be put in the middle of pixels + if (this.currentBrushSize % 2 == 0) { + toSub = 0.5; + } + + brushPreview.style.left = (Math.ceil(cursorLocation[0] / zoom) * zoom + currentLayer.canvas.offsetLeft - this.currentBrushSize * zoom / 2 - zoom / 2 - toSub * zoom) + 'px'; + brushPreview.style.top = (Math.ceil(cursorLocation[1] / zoom) * zoom + currentLayer.canvas.offsetTop - this.currentBrushSize * zoom / 2 - zoom / 2 - toSub * zoom) + 'px'; } }