From 17035c7480e12c816ed13e4dcd75b3fac9802798 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Mon, 24 Apr 2017 02:13:18 +0200 Subject: [PATCH] reduce angle calculation error for cosin of 90 and 270 degrees (#3872) * reduce angle calculation error * reduce angle calculation error --- src/canvas.class.js | 23 ++++++++++++----------- src/mixins/object_geometry.mixin.js | 4 ++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/canvas.class.js b/src/canvas.class.js index a8179c4d5b7..5adaa13f51f 100644 --- a/src/canvas.class.js +++ b/src/canvas.class.js @@ -976,13 +976,6 @@ angle = radiansToDegrees(curAngle - lastAngle + t.theta), hasRoated = true; - // normalize angle to positive value - if (angle < 0) { - angle = 360 + angle; - } - - angle %= 360; - if (t.target.snapAngle > 0) { var snapAngle = t.target.snapAngle, snapThreshold = t.target.snapThreshold || snapAngle, @@ -995,13 +988,21 @@ else if (Math.abs(angle - rightAngleLocked) < snapThreshold) { angle = rightAngleLocked; } + } - if (t.target.angle === angle) { - hasRoated = false; - } + // normalize angle to positive value + if (angle < 0) { + angle = 360 + angle; + } + angle %= 360; + + if (t.target.angle === angle) { + hasRoated = false; + } + else { + t.target.angle = angle; } - t.target.angle = angle; return hasRoated; }, diff --git a/src/mixins/object_geometry.mixin.js b/src/mixins/object_geometry.mixin.js index 152d8d7b5d8..52ebd87d2aa 100644 --- a/src/mixins/object_geometry.mixin.js +++ b/src/mixins/object_geometry.mixin.js @@ -450,6 +450,10 @@ _calcRotateMatrix: function() { if (this.angle) { var theta = degreesToRadians(this.angle), cos = Math.cos(theta), sin = Math.sin(theta); + // trying to keep rounding error small, ugly but it works. + if (cos === 6.123233995736766e-17 || cos === -1.8369701987210297e-16) { + cos = 0; + } return [cos, sin, -sin, cos, 0, 0]; } return fabric.iMatrix.concat();