Unlock custom brushes, natural fill effects and intuitive hatching in p5.js
Return plots from arc() and endShape()
acamposuribe75b6ff612280
diff --git a/src/core/primitives.js b/src/core/primitives.jsindex a89b4ff..98aa200 100644--- a/src/core/primitives.js+++ b/src/core/primitives.js@@ -101,12 +101,13 @@ export function circle(x, y, radius, r = false) {* @param {number} radius - Radius.* @param {number} start - Start angle in the current runtime angle units.* @param {number} end - End angle in the current runtime angle units.+ * @returns {Plot|null} The drawn Plot, or null when the sweep is zero.*/export function arc(x, y, radius, start, end) {const startDeg = toDegreesSigned(start);const endDeg = toDegreesSigned(end);const sweepDeg = ((endDeg - startDeg) % 360 + 360) % 360;- if (sweepDeg === 0) return;+ if (sweepDeg === 0) return null;const p = new Plot("curve");const segmentCount = Math.max(1, Math.ceil(sweepDeg / 90));@@ -121,6 +122,7 @@ export function arc(x, y, radius, start, end) {const startX = x + radius * cos(startDeg);const startY = y - radius * sin(startDeg);p.draw(startX, startY, 1);+ return p;}// Variables for managing paths and strokes@@ -178,6 +180,7 @@ export function vertex(x, y, pressure = 1) {/*** Ends the current path and renders all subpaths.+ * @returns {Plot} The rendered Plot for the completed shape.*/export function endShape(close = false) {if (!_current) {@@ -194,9 +197,9 @@ export function endShape(close = false) {_current.vertex(..._current.vert[0]);_current.isClosed = true;}- _current.show();+ const plot = _current.show();_current = false;- return _current;+ return plot;}let _strokeArray, _strokeOrigin;diff --git a/test/unit/unit.test.js b/test/unit/unit.test.jsindex a887221..ddb54db 100644--- a/test/unit/unit.test.js+++ b/test/unit/unit.test.js@@ -63,6 +63,7 @@ vi.mock("../../src/core/plot.js", () => ({this.addSegment = vi.fn();this.endPlot = vi.fn();this.draw = vi.fn();+ this.show = vi.fn();plotInstances.push(this);}},@@ -82,7 +83,7 @@ import {weightedRand,toDegreesSigned,} from "../../src/core/utils.js";-import { arc } from "../../src/core/primitives.js";+import { arc, beginShape, vertex, endShape } from "../../src/core/primitives.js";import { Position, addField, field as activateField, noField } from "../../src/core/flowfield.js";import { getHatchLines, hatch } from "../../src/hatch/hatch.js";@@ -462,3 +463,44 @@ describe("addField()", () => {expect(pos.angle()).toBeCloseTo(-90, 3);});});++// ---- arc() return value ----+describe("arc() return value", () => {+ it("returns the Plot instance that was drawn (non-zero sweep)", () => {+ const result = arc(0, 0, 50, 0, Math.PI);+ expect(result).toBe(plotInstances[0]);+ expect(plotInstances[0].draw).toHaveBeenCalled();+ });++ it("returns null and constructs no Plot when sweep is zero", () => {+ // same start and end angle → sweepDeg === 0+ const result = arc(0, 0, 50, 10, 10);+ expect(result).toBeNull();+ expect(plotInstances).toHaveLength(0);+ });+});++// ---- endShape() return value ----+describe("endShape() return value", () => {+ it("returns a Plot instance (not false) after a valid shape", () => {+ beginShape(0);+ vertex(0, 0);+ vertex(10, 0);+ vertex(10, 10);+ const result = endShape();+ // The Plot mock is pushed into plotInstances by _createSpline inside endShape+ expect(result).toBeTruthy();+ expect(result).not.toBe(false);+ // result is one of the Plot instances constructed during the call+ expect(plotInstances).toContain(result);+ });++ it("throws on a second endShape() call (the _current=false guard is preserved)", () => {+ beginShape(0);+ vertex(0, 0);+ vertex(10, 0);+ vertex(10, 10);+ endShape();+ expect(() => endShape()).toThrow();+ });+});