Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add d3.pathRound. #12

Merged
merged 9 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ Now code you write once can be used with both Canvas (for performance) and SVG (

## Installing

If you use npm, `npm install d3-path`. You can also download the [latest release on GitHub](https://github.com/d3/d3-path/releases/latest). In modern browsers, you can import d3-path from Skypack:
If you use npm, `npm install d3-path`. You can also download the [latest release on GitHub](https://github.com/d3/d3-path/releases/latest). In modern browsers, you can import d3-path from jsDelivr:

```html
<script type="module">

import {path} from "https://cdn.skypack.dev/d3-path@3";
import {path} from "https://cdn.jsdelivr.net/npm/d3-path@3/+esm";

const p = path();
p.moveTo(1, 2);
Expand Down Expand Up @@ -88,3 +88,7 @@ Creates a new subpath containing just the four points ⟨*x*, *y*⟩, ⟨*x* + *
<a name="path_toString" href="#path_toString">#</a> <i>path</i>.<b>toString</b>()

Returns the string representation of this *path* according to SVG’s [path data specification](http://www.w3.org/TR/SVG/paths.html#PathData).

<a name="pathFixed" href="#pathFixed">#</a> d3.<b>pathFixed</b>(*digits* = 3) · [Source](https://github.com/d3/d3-path/blob/master/src/path.js), [Examples](https://observablehq.com/@d3/d3-path)

Like [d3.path](#path), except limits the digits after the decimal to the specified number of *digits*.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {default as path} from "./path.js";
export {Path, path, pathFixed} from "./path.js";
125 changes: 75 additions & 50 deletions src/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,67 @@ const pi = Math.PI,
epsilon = 1e-6,
tauEpsilon = tau - epsilon;

function Path() {
this._x0 = this._y0 = // start of current subpath
this._x1 = this._y1 = null; // end of current subpath
this._ = "";
function append(strings) {
let i = 0;
for (const j = strings.length - 1; i < j; ++i) {
this._ += strings[i] + arguments[i + 1];
}
this._ += strings[i];
}

function path() {
return new Path;
function appendFixed(digits) {
(digits = +digits).toFixed(digits); // validate digits
return function(strings) {
let i = 0;
for (const j = strings.length - 1; i < j; ++i) {
this._ += strings[i] + +(arguments[i + 1]).toFixed(digits);
}
this._ += strings[i];
};
}

Path.prototype = path.prototype = {
constructor: Path,
moveTo: function(x, y) {
this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y);
},
closePath: function() {
export class Path {
constructor(digits) {
this._x0 = this._y0 = // start of current subpath
this._x1 = this._y1 = null; // end of current subpath
this._ = "";
this._append = digits == null ? append : appendFixed(digits);
}
moveTo(x, y) {
this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}`;
}
closePath() {
if (this._x1 !== null) {
this._x1 = this._x0, this._y1 = this._y0;
this._ += "Z";
this._append`Z`;
}
},
lineTo: function(x, y) {
this._ += "L" + (this._x1 = +x) + "," + (this._y1 = +y);
},
quadraticCurveTo: function(x1, y1, x, y) {
this._ += "Q" + (+x1) + "," + (+y1) + "," + (this._x1 = +x) + "," + (this._y1 = +y);
},
bezierCurveTo: function(x1, y1, x2, y2, x, y) {
this._ += "C" + (+x1) + "," + (+y1) + "," + (+x2) + "," + (+y2) + "," + (this._x1 = +x) + "," + (this._y1 = +y);
},
arcTo: function(x1, y1, x2, y2, r) {
}
lineTo(x, y) {
this._append`L${this._x1 = +x},${this._y1 = +y}`;
}
quadraticCurveTo(x1, y1, x, y) {
this._append`Q${+x1},${+y1},${this._x1 = +x},${this._y1 = +y}`;
}
bezierCurveTo(x1, y1, x2, y2, x, y) {
this._append`C${+x1},${+y1},${+x2},${+y2},${this._x1 = +x},${this._y1 = +y}`;
}
arcTo(x1, y1, x2, y2, r) {
x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
var x0 = this._x1,

// Is the radius negative? Error.
if (r < 0) throw new Error(`negative radius: ${r}`);

let x0 = this._x1,
y0 = this._y1,
x21 = x2 - x1,
y21 = y2 - y1,
x01 = x0 - x1,
y01 = y0 - y1,
l01_2 = x01 * x01 + y01 * y01;

// Is the radius negative? Error.
if (r < 0) throw new Error("negative radius: " + r);

// Is this path empty? Move to (x1,y1).
if (this._x1 === null) {
this._ += "M" + (this._x1 = x1) + "," + (this._y1 = y1);
this._append`M${this._x1 = x1},${this._y1 = y1}`;
}

// Or, is (x1,y1) coincident with (x0,y0)? Do nothing.
Expand All @@ -58,12 +73,12 @@ Path.prototype = path.prototype = {
// Equivalently, is (x1,y1) coincident with (x2,y2)?
// Or, is the radius zero? Line to (x1,y1).
else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
this._ += "L" + (this._x1 = x1) + "," + (this._y1 = y1);
this._append`L${this._x1 = x1},${this._y1 = y1}`;
}

// Otherwise, draw an arc!
else {
var x20 = x2 - x0,
let x20 = x2 - x0,
y20 = y2 - y0,
l21_2 = x21 * x21 + y21 * y21,
l20_2 = x20 * x20 + y20 * y20,
Expand All @@ -75,32 +90,33 @@ Path.prototype = path.prototype = {

// If the start tangent is not coincident with (x0,y0), line to.
if (Math.abs(t01 - 1) > epsilon) {
this._ += "L" + (x1 + t01 * x01) + "," + (y1 + t01 * y01);
this._append`L${x1 + t01 * x01},${y1 + t01 * y01}`;
}

this._ += "A" + r + "," + r + ",0,0," + (+(y01 * x20 > x01 * y20)) + "," + (this._x1 = x1 + t21 * x21) + "," + (this._y1 = y1 + t21 * y21);
this._append`A${r},${r},0,0,${+(y01 * x20 > x01 * y20)},${this._x1 = x1 + t21 * x21},${this._y1 = y1 + t21 * y21}`;
}
},
arc: function(x, y, r, a0, a1, ccw) {
}
arc(x, y, r, a0, a1, ccw) {
x = +x, y = +y, r = +r, ccw = !!ccw;
var dx = r * Math.cos(a0),

// Is the radius negative? Error.
if (r < 0) throw new Error(`negative radius: ${r}`);

let dx = r * Math.cos(a0),
dy = r * Math.sin(a0),
x0 = x + dx,
y0 = y + dy,
cw = 1 ^ ccw,
da = ccw ? a0 - a1 : a1 - a0;

// Is the radius negative? Error.
if (r < 0) throw new Error("negative radius: " + r);

// Is this path empty? Move to (x0,y0).
if (this._x1 === null) {
this._ += "M" + x0 + "," + y0;
this._append`M${x0},${y0}`;
}

// Or, is (x0,y0) not coincident with the previous point? Line to (x0,y0).
else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
this._ += "L" + x0 + "," + y0;
this._append`L${x0},${y0}`;
}

// Is this arc empty? We’re done.
Expand All @@ -111,20 +127,29 @@ Path.prototype = path.prototype = {

// Is this a complete circle? Draw two arcs to complete the circle.
if (da > tauEpsilon) {
this._ += "A" + r + "," + r + ",0,1," + cw + "," + (x - dx) + "," + (y - dy) + "A" + r + "," + r + ",0,1," + cw + "," + (this._x1 = x0) + "," + (this._y1 = y0);
this._append`A${r},${r},0,1,${cw},${x - dx},${y - dy}A${r},${r},0,1,${cw},${this._x1 = x0},${this._y1 = y0}`;
}

// Is this arc non-empty? Draw an arc!
else if (da > epsilon) {
this._ += "A" + r + "," + r + ",0," + (+(da >= pi)) + "," + cw + "," + (this._x1 = x + r * Math.cos(a1)) + "," + (this._y1 = y + r * Math.sin(a1));
this._append`A${r},${r},0,${+(da >= pi)},${cw},${this._x1 = x + r * Math.cos(a1)},${this._y1 = y + r * Math.sin(a1)}`;
}
},
rect: function(x, y, w, h) {
this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y) + "h" + (+w) + "v" + (+h) + "h" + (-w) + "Z";
},
toString: function() {
}
rect(x, y, w, h) {
this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}h${w = +w}v${+h}h${-w}Z`;
}
toString() {
return this._;
}
};
}

export default path;
export function path() {
return new Path;
}

// Allow instanceof d3.path
path.prototype = Path.prototype;

export function pathFixed(digits = 3) {
return new Path(+digits);
}
83 changes: 83 additions & 0 deletions test/pathFixed-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import assert from "assert";
import {path, pathFixed} from "../src/index.js";

it("pathFixed() defaults to three digits of precision", () => {
const p = pathFixed();
p.moveTo(Math.PI, Math.E);
assert.strictEqual(p + "", "M3.142,2.718");
});

it("pathFixed(null) is equivalent to pathFixed(0)", () => {
const p = pathFixed(null);
p.moveTo(Math.PI, Math.E);
assert.strictEqual(p + "", "M3,3");
});

it("pathFixed(NaN) is equivalent to pathFixed(0)", () => {
const p = pathFixed(NaN); // arguably this should throw, but number.toFixed doesn’t
p.moveTo(Math.PI, Math.E);
assert.strictEqual(p + "", "M3,3");
});

it("pathFixed(digits) validates the specified digits", () => {
assert.throws(() => pathFixed(-1), /digits argument must be between 0 and 100/);
assert.throws(() => pathFixed(200), /digits argument must be between 0 and 100/);
});

it("pathFixed.moveTo(x, y) limits the precision", () => {
const p = pathFixed(1);
p.moveTo(123.456, 789.012);
assert.strictEqual(p + "", "M123.5,789");
});

it("pathFixed.lineTo(x, y) limits the precision", () => {
const p = pathFixed(1);
p.moveTo(0, 0);
p.lineTo(123.456, 789.012);
assert.strictEqual(p + "", "M0,0L123.5,789");
});

it("pathFixed.arc(x, y, r, a0, a1, ccw) limits the precision", () => {
const p0 = path(), p = pathFixed(1);
p0.arc(10.0001, 10.0001, 123.456, 0, Math.PI+0.0001);
p.arc(10.0001, 10.0001, 123.456, 0, Math.PI+0.0001);
assert.strictEqual(p + "", precision(p0 + "", 1));
p0.arc(10.0001, 10.0001, 123.456, 0, Math.PI-0.0001);
p.arc(10.0001, 10.0001, 123.456, 0, Math.PI-0.0001);
assert.strictEqual(p + "", precision(p0 + "", 1));
p0.arc(10.0001, 10.0001, 123.456, 0, Math.PI / 2, true);
p.arc(10.0001, 10.0001, 123.456, 0, Math.PI / 2, true);
assert.strictEqual(p + "", precision(p0 + "", 1));
});

it("pathFixed.arcTo(x1, y1, x2, y2, r) limits the precision", () => {
const p0 = path(), p = pathFixed(1);
p0.arcTo(10.0001, 10.0001, 123.456, 456.789, 12345.6789);
p.arcTo(10.0001, 10.0001, 123.456, 456.789, 12345.6789);
assert.strictEqual(p + "", precision(p0 + "", 1));
});

it("pathFixed.quadraticCurveTo(x1, y1, x, y) limits the precision", () => {
const p0 = path(), p = pathFixed(1);
p0.quadraticCurveTo(10.0001, 10.0001, 123.456, 456.789);
p.quadraticCurveTo(10.0001, 10.0001, 123.456, 456.789);
assert.strictEqual(p + "", precision(p0 + "", 1));
});

it("pathFixed.bezierCurveTo(x1, y1, x2, y2, x, y) limits the precision", () => {
const p0 = path(), p = pathFixed(1);
p0.bezierCurveTo(10.0001, 10.0001, 123.456, 456.789, 0.007, 0.006);
p.bezierCurveTo(10.0001, 10.0001, 123.456, 456.789, 0.007, 0.006);
assert.strictEqual(p + "", precision(p0 + "", 1));
});

it("pathFixed.rect(x, y, w, h) limits the precision", () => {
const p0 = path(), p = pathFixed(1);
p0.rect(10.0001, 10.0001, 123.456, 456.789);
p.rect(10.0001, 10.0001, 123.456, 456.789);
assert.strictEqual(p + "", precision(p0 + "", 1));
});

function precision(str, precision) {
return str.replace(/\d+\.\d+/g, s => +parseFloat(s).toFixed(precision));
}