Skip to content

Commit

Permalink
reduce angle calculation error for cosin of 90 and 270 degrees (#3872)
Browse files Browse the repository at this point in the history
* reduce angle calculation error

* reduce angle calculation error
  • Loading branch information
asturur committed Apr 24, 2017
1 parent 8814225 commit 51a24b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
},

Expand Down
4 changes: 4 additions & 0 deletions src/mixins/object_geometry.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 51a24b4

Please sign in to comment.