From 3cdd94e46548214f9bf335a49d93867526d5fc4c Mon Sep 17 00:00:00 2001 From: jonasengelmann <40031476+jonasengelmann@users.noreply.github.com> Date: Wed, 29 Jun 2022 19:47:26 +0200 Subject: [PATCH] constrained rotation to shortest path --- src/viewport.js | 11 ++++++++++- test/modules/viewport.js | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/viewport.js b/src/viewport.js index 203fe07f..9a058968 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -917,7 +917,16 @@ $.Viewport.prototype = { if (immediately) { this.degreesSpring.resetTo(degrees); } else { - this.degreesSpring.springTo(degrees); + var from = $.euclideanModulo(this.degreesSpring.current.value, 360); + this.degreesSpring.resetTo(from); + var to = $.euclideanModulo(degrees, 360); + var diff = to - from; + if (diff > 180) { + to -= 360; + } else if (diff < -180) { + to += 360; + } + this.degreesSpring.springTo(to); } this._setContentBounds( diff --git a/test/modules/viewport.js b/test/modules/viewport.js index 59597060..804af1e9 100644 --- a/test/modules/viewport.js +++ b/test/modules/viewport.js @@ -1075,7 +1075,7 @@ viewport.setRotation(400); assert.strictEqual(viewport.getRotation(true), 0, 'current rotation is not changed'); - assert.strictEqual(viewport.getRotation(false), 400, 'target rotation is set correctly'); + assert.strictEqual(viewport.getRotation(false), 40, 'target rotation is set correctly'); viewport.setRotation(200, true); assert.strictEqual(viewport.getRotation(true), 200, 'current rotation is set correctly');