Skip to content

Commit 59400f6

Browse files
committed
fix: implement dual-path rendering for arcs and add path-safety moveTos
1 parent 7d9298c commit 59400f6

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

src/core/p5.Renderer2D.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,10 @@ class Renderer2D extends Renderer {
287287
this.clipPath.closePath();
288288
} else {
289289
if (this.states.fillColor) {
290-
this.drawingContext.fill(visitor.path);
290+
this.drawingContext.fill(visitor.fillPath || visitor.path);
291291
}
292292
if (this.states.strokeColor) {
293-
this.drawingContext.stroke(visitor.path);
293+
this.drawingContext.stroke(visitor.strokePath || visitor.path);
294294
}
295295
}
296296
}

src/shape/custom_shapes.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,14 @@ class Quad extends ShapePrimitive {
466466
}
467467
}
468468

469+
/**
470+
* TODO: Future enhancement — align with arcVertex proposal (#6459)
471+
* Currently stores start/stop angles and mode (OPEN/CHORD/PIE).
472+
* For full SVG compatibility and arcs inside beginShape/endShape,
473+
* we may want to add an arc-to-vertex variant that matches the
474+
* arcVertex() API discussed in #6459.
475+
*/
476+
469477
class ArcPrimitive extends ShapePrimitive {
470478
#x;
471479
#y;
@@ -1153,6 +1161,8 @@ class PrimitiveVisitor {
11531161
// requires testing
11541162
class PrimitiveToPath2DConverter extends PrimitiveVisitor {
11551163
path = new Path2D();
1164+
strokePath = null;
1165+
fillPath = null;
11561166
strokeWeight;
11571167

11581168
constructor({ strokeWeight }) {
@@ -1276,27 +1286,52 @@ class PrimitiveToPath2DConverter extends PrimitiveVisitor {
12761286
const centerY = arc.y + arc.h / 2;
12771287
const radiusX = arc.w / 2;
12781288
const radiusY = arc.h / 2;
1289+
const startX = centerX + radiusX * Math.cos(arc.start);
1290+
const startY = centerY + radiusY * Math.sin(arc.start);
12791291

1280-
this.path.ellipse(
1281-
centerX, centerY, radiusX, radiusY, 0, arc.start, arc.stop
1292+
const delta = arc.stop - arc.start;
1293+
const isFullCircle = Math.abs(delta % (2 * Math.PI)) < 0.00001 &&
1294+
Math.abs(delta) > 0.00001;
1295+
1296+
const createPieSlice = ! (
1297+
arc.mode === constants.CHORD ||
1298+
arc.mode === constants.OPEN ||
1299+
isFullCircle
12821300
);
12831301

1284-
if (arc.mode === constants.OPEN) {
1285-
// OPEN: leave path open — arc stroke/fill is just the curve
1286-
} else if (arc.mode === constants.CHORD) {
1302+
if (!this.fillPath) this.fillPath = new Path2D(this.path);
1303+
if (!this.strokePath) this.strokePath = new Path2D(this.path);
12871304

1288-
this.path.closePath();
1289-
} else {
1290-
this.path.lineTo(centerX, centerY);
1291-
this.path.closePath();
1305+
this.fillPath.moveTo(startX, startY);
1306+
this.fillPath.ellipse(centerX, centerY, radiusX, radiusY,
1307+
0, arc.start, arc.stop);
1308+
if (createPieSlice) {
1309+
this.fillPath.lineTo(centerX, centerY);
1310+
}
1311+
this.fillPath.closePath();
1312+
1313+
this.strokePath.moveTo(startX, startY);
1314+
this.strokePath.ellipse(centerX, centerY, radiusX, radiusY,
1315+
0, arc.start, arc.stop);
1316+
if (arc.mode === constants.PIE && createPieSlice) {
1317+
this.strokePath.lineTo(centerX, centerY);
12921318
}
1319+
if (arc.mode === constants.PIE || arc.mode === constants.CHORD) {
1320+
this.strokePath.closePath();
1321+
}
1322+
1323+
// Still maintain base path just in case
1324+
this.path.moveTo(startX, startY);
1325+
this.path.ellipse(centerX, centerY, radiusX, radiusY,
1326+
0, arc.start, arc.stop);
12931327
}
12941328
visitEllipsePrimitive(ellipse) {
12951329
const centerX = ellipse.x + ellipse.w / 2;
12961330
const centerY = ellipse.y + ellipse.h / 2;
12971331
const radiusX = ellipse.w / 2;
12981332
const radiusY = ellipse.h / 2;
12991333

1334+
this.path.moveTo(centerX + radiusX, centerY);
13001335
this.path.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
13011336
}
13021337
visitQuadStrip(quadStrip) {

0 commit comments

Comments
 (0)