diff --git a/src/io/mouse.js b/src/io/mouse.js index 90752158d..1c7bb81e5 100644 --- a/src/io/mouse.js +++ b/src/io/mouse.js @@ -59,19 +59,19 @@ class Mouse { postData (data) { if (data.x) { this._clientX = data.x; - this._scratchX = MathUtil.clamp( + this._scratchX = Math.round(MathUtil.clamp( 480 * ((data.x / data.canvasWidth) - 0.5), -240, 240 - ); + )); } if (data.y) { this._clientY = data.y; - this._scratchY = MathUtil.clamp( + this._scratchY = Math.round(MathUtil.clamp( -360 * ((data.y / data.canvasHeight) - 0.5), -180, 180 - ); + )); } if (typeof data.isDown !== 'undefined') { const previousDownState = this._isDown; @@ -119,7 +119,7 @@ class Mouse { /** * Get the X position of the mouse in scratch coordinates. - * @return {number} Clamped X position of the mouse cursor. + * @return {number} Clamped and integer rounded X position of the mouse cursor. */ getScratchX () { return this._scratchX; @@ -127,7 +127,7 @@ class Mouse { /** * Get the Y position of the mouse in scratch coordinates. - * @return {number} Clamped Y position of the mouse cursor. + * @return {number} Clamped and integer rounded Y position of the mouse cursor. */ getScratchY () { return this._scratchY; diff --git a/test/unit/io_mouse.js b/test/unit/io_mouse.js index 736394eaf..c1fa21935 100644 --- a/test/unit/io_mouse.js +++ b/test/unit/io_mouse.js @@ -40,14 +40,14 @@ test('mouseDown', t => { const m = new Mouse(rt); m.postData({ - x: 10, - y: 400, + x: 9.9, + y: 400.1, isDown: true, canvasWidth: 480, canvasHeight: 360 }); - t.strictEquals(m.getClientX(), 10); - t.strictEquals(m.getClientY(), 400); + t.strictEquals(m.getClientX(), 9.9); + t.strictEquals(m.getClientY(), 400.1); t.strictEquals(m.getScratchX(), -230); t.strictEquals(m.getScratchY(), -180); t.strictEquals(m.getIsDown(), true);