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

curveBumpX, curveBumpY #174

Merged
merged 4 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,18 @@ Produces a closed cubic [basis spline](https://en.wikipedia.org/wiki/B-spline) u

Produces a cubic [basis spline](https://en.wikipedia.org/wiki/B-spline) using the specified control points. Unlike [basis](#basis), the first and last points are not repeated, and thus the curve typically does not intersect these points.

<a name="curveBumpX" href="#curveBumpX">#</a> d3.<b>curveBumpX</b>(<i>context</i>) · [Source](https://github.com/d3/d3-shape/blob/master/src/curve/bump.js)

<img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/bumpX.png" width="888" height="240" alt="bumpX">

Produces a Bézier curve between each pair of points, with horizontal tangents at each point.

<a name="curveBumpY" href="#curveBumpY">#</a> d3.<b>curveBumpY</b>(<i>context</i>) · [Source](https://github.com/d3/d3-shape/blob/master/src/curve/bump.js)

<img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/bumpY.png" width="888" height="240" alt="bumpY">

Produces a Bézier curve between each pair of points, with vertical tangents at each point.

<a name="curveBundle" href="#curveBundle">#</a> d3.<b>curveBundle</b>(<i>context</i>) · [Source](https://github.com/d3/d3-shape/blob/master/src/curve/bundle.js)

<img src="https://raw.githubusercontent.com/d3/d3-shape/master/img/bundle.png" width="888" height="240" alt="bundle">
Expand Down
Binary file added img/bumpX.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/bumpY.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions src/curve/bump.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Bump {
constructor(context, x) {
this._context = context;
this._x = x;
}
areaStart() {
this._line = 0;
}
areaEnd() {
this._line = NaN;
}
lineStart() {
this._point = 0;
}
lineEnd() {
if (this._line || (this._line !== 0 && this._point === 1)) this._context.closePath();
this._line = 1 - this._line;
}
point(x, y) {
x = +x, y = +y;
switch (this._point) {
case 0: {
this._point = 1;
if (this._line) this._context.lineTo(x, y);
else this._context.moveTo(x, y);
break;
}
case 1: this._point = 2; // proceed
default: {
if (this._x) this._context.bezierCurveTo(this._x0 = (this._x0 + x) / 2, this._y0, this._x0, y, x, y);
else this._context.bezierCurveTo(this._x0, this._y0 = (this._y0 + y) / 2, x, this._y0, x, y);
break;
}
}
this._x0 = x, this._y0 = y;
}
}

export function bumpX(context) {
return new Bump(context, true);
}

export function bumpY(context) {
return new Bump(context, false);
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export {default as symbolWye} from "./symbol/wye.js";
export {default as curveBasisClosed} from "./curve/basisClosed.js";
export {default as curveBasisOpen} from "./curve/basisOpen.js";
export {default as curveBasis} from "./curve/basis.js";
export {bumpX as curveBumpX, bumpY as curveBumpY} from "./curve/bump.js";
export {default as curveBundle} from "./curve/bundle.js";
export {default as curveCardinalClosed} from "./curve/cardinalClosed.js";
export {default as curveCardinalOpen} from "./curve/cardinalOpen.js";
Expand Down
22 changes: 22 additions & 0 deletions test/curve/bumpX-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var tape = require("tape"),
shape = require("../../");

require("../pathEqual");

tape("line.curve(curveBumpX)(data) generates the expected path", function(test) {
var l = shape.line().curve(shape.curveBumpX);
test.equal(l([]), null);
test.pathEqual(l([[0, 1]]), "M0,1Z");
test.pathEqual(l([[0, 1], [1, 3]]), "M0,1C0.500000,1,0.500000,3,1,3");
test.pathEqual(l([[0, 1], [1, 3], [2, 1]]), "M0,1C0.500000,1,0.500000,3,1,3C1.500000,3,1.500000,1,2,1");
test.end();
});

tape("area.curve(curveBumpX)(data) generates the expected path", function(test) {
var a = shape.area().curve(shape.curveBumpX);
test.equal(a([]), null);
test.pathEqual(a([[0, 1]]), "M0,1L0,0Z");
test.pathEqual(a([[0, 1], [1, 3]]), "M0,1C0.500000,1,0.500000,3,1,3L1,0C0.500000,0,0.500000,0,0,0Z");
test.pathEqual(a([[0, 1], [1, 3], [2, 1]]), "M0,1C0.500000,1,0.500000,3,1,3C1.500000,3,1.500000,1,2,1L2,0C1.500000,0,1.500000,0,1,0C0.500000,0,0.500000,0,0,0Z");
test.end();
});
22 changes: 22 additions & 0 deletions test/curve/bumpY-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var tape = require("tape"),
shape = require("../../");

require("../pathEqual");

tape("line.curve(curveBumpY)(data) generates the expected path", function(test) {
var l = shape.line().curve(shape.curveBumpY);
test.equal(l([]), null);
test.pathEqual(l([[0, 1]]), "M0,1Z");
test.pathEqual(l([[0, 1], [1, 3]]), "M0,1C0,2,1,2,1,3");
test.pathEqual(l([[0, 1], [1, 3], [2, 1]]), "M0,1C0,2,1,2,1,3C1,2,2,2,2,1");
test.end();
});

tape("area.curve(curveBumpY)(data) generates the expected path", function(test) {
var a = shape.area().curve(shape.curveBumpY);
test.equal(a([]), null);
test.pathEqual(a([[0, 1]]), "M0,1L0,0Z");
test.pathEqual(a([[0, 1], [1, 3]]), "M0,1C0,2,1,2,1,3L1,0C1,0,0,0,0,0Z");
test.pathEqual(a([[0, 1], [1, 3], [2, 1]]), "M0,1C0,2,1,2,1,3C1,2,2,2,2,1L2,0C2,0,1,0,1,0C1,0,0,0,0,0Z");
test.end();
});