Skip to content

Commit e54c95a

Browse files
committed
Fix arc calculations
Add TODOs for unimplemented operations
1 parent fb50783 commit e54c95a

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

plugins/context2d.js

Lines changed: 29 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,48 @@
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+
}
15071514

1508-
// Compute the sequence of arc curves, up to PI/2 at a time.
15091515
// Total arc angle is less than 2PI.
1516+
var totalAngle = Math.abs(endAngleN - startAngleN);
1517+
if (anticlockwise) {
1518+
if (startAngle < endAngle) {
1519+
totalAngle = twoPI - totalAngle;
1520+
}
1521+
}
1522+
else {
1523+
if (startAngle > endAngle) {
1524+
totalAngle = twoPI - totalAngle;
1525+
}
1526+
}
1527+
//TODO case when angles are equal. Do we draw circle? Or NOP?
1528+
1529+
// Compute the sequence of arc curves, up to PI/2 at a time.
15101530
var curves = [];
1511-
var piOverTwo = Math.PI / 2.0;
1512-
// var sgn = (startAngle < endAngle) ? +1 : -1; // clockwise or counterclockwise
15131531
var sgn = anticlockwise ? -1 : +1;
15141532

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);
1533+
var a1 = startAngleN;
1534+
for (; totalAngle > EPSILON;) {
1535+
var remain = sgn * Math.min(totalAngle, piOverTwo);
1536+
var a2 = a1 + remain;
15181537
curves.push(this.createSmallArc(radius, a1, a2));
15191538
totalAngle -= Math.abs(a2 - a1);
15201539
a1 = a2;
@@ -1523,6 +1542,7 @@
15231542
return curves;
15241543
};
15251544

1545+
15261546
c2d.internal.getCurrentPage = function () {
15271547
return this.pdf.internal.pages[this.pdf.internal.getCurrentPageInfo().pageNumber];
15281548
};

0 commit comments

Comments
 (0)