Skip to content

Commit f2ec07a

Browse files
authored
Merge pull request #1037 from Flamenco/hotfix_arc_calculation
Fix arc calculations
2 parents fb50783 + 2576533 commit f2ec07a

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

plugins/context2d.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@
649649
x = this._wrapX(x);
650650
y = this._wrapY(y);
651651

652+
//TODO angles and radius need to be transformed
652653
var xpt = this._matrix_map_point(this.ctx._transform, [x, y]);
653654
x = xpt[0];
654655
y = xpt[1];
@@ -1491,30 +1492,51 @@
14911492
*/
14921493

14931494
c2d.internal.createArc = function (radius, startAngle, endAngle, anticlockwise) {
1494-
14951495
var EPSILON = 0.00001; // Roughly 1/1000th of a degree, see below
1496-
1497-
// normalize startAngle, endAngle to [-2PI, 2PI]
14981496
var twoPI = Math.PI * 2;
1497+
var piOverTwo = Math.PI / 2.0;
1498+
1499+
// normalize startAngle, endAngle to [0, 2PI]
14991500
var startAngleN = startAngle;
15001501
if (startAngleN < twoPI || startAngleN > twoPI) {
15011502
startAngleN = startAngleN % twoPI;
15021503
}
1504+
if (startAngleN < 0) {
1505+
startAngleN = twoPI + startAngleN;
1506+
}
15031507
var endAngleN = endAngle;
15041508
if (endAngleN < twoPI || endAngleN > twoPI) {
15051509
endAngleN = endAngleN % twoPI;
15061510
}
1511+
if (endAngleN < 0) {
1512+
endAngleN = twoPI + endAngleN;
1513+
}
1514+
1515+
// Total arc angle is less than or equal to 2PI.
1516+
var totalAngle = Math.abs(endAngleN - startAngleN);
1517+
if (totalAngle < twoPI) {
1518+
if (totalAngle < twoPI) {
1519+
if (anticlockwise) {
1520+
if (startAngle < endAngle) {
1521+
totalAngle = twoPI - totalAngle;
1522+
}
1523+
}
1524+
else {
1525+
if (startAngle > endAngle) {
1526+
totalAngle = twoPI - totalAngle;
1527+
}
1528+
}
1529+
}
1530+
}
15071531

15081532
// Compute the sequence of arc curves, up to PI/2 at a time.
1509-
// Total arc angle is less than 2PI.
15101533
var curves = [];
1511-
var piOverTwo = Math.PI / 2.0;
1512-
// var sgn = (startAngle < endAngle) ? +1 : -1; // clockwise or counterclockwise
15131534
var sgn = anticlockwise ? -1 : +1;
15141535

1515-
var a1 = startAngle;
1516-
for (var totalAngle = Math.min(twoPI, Math.abs(endAngleN - startAngleN)); totalAngle > EPSILON;) {
1517-
var a2 = a1 + sgn * Math.min(totalAngle, piOverTwo);
1536+
var a1 = startAngleN;
1537+
for (; totalAngle > EPSILON;) {
1538+
var remain = sgn * Math.min(totalAngle, piOverTwo);
1539+
var a2 = a1 + remain;
15181540
curves.push(this.createSmallArc(radius, a1, a2));
15191541
totalAngle -= Math.abs(a2 - a1);
15201542
a1 = a2;
@@ -1523,6 +1545,7 @@
15231545
return curves;
15241546
};
15251547

1548+
15261549
c2d.internal.getCurrentPage = function () {
15271550
return this.pdf.internal.pages[this.pdf.internal.getCurrentPageInfo().pageNumber];
15281551
};

0 commit comments

Comments
 (0)