Comparing version 5.0.1 to 5.1.0
@@ -406,17 +406,9 @@ // math-inlining. | ||
makeline: function (p1, p2) { | ||
const x1 = p1.x, | ||
y1 = p1.y, | ||
x2 = p2.x, | ||
y2 = p2.y, | ||
dx = (x2 - x1) / 3, | ||
dy = (y2 - y1) / 3; | ||
return new Bezier( | ||
x1, | ||
y1, | ||
x1 + dx, | ||
y1 + dy, | ||
x1 + 2 * dx, | ||
y1 + 2 * dy, | ||
x2, | ||
y2 | ||
p1.x, | ||
p1.y, | ||
(p1.x + p2.x) / 2, | ||
(p1.y + p2.y) / 2, | ||
p2.x, | ||
p2.y | ||
); | ||
@@ -1058,4 +1050,6 @@ }, | ||
// is this curve, practically speaking, a straight line? | ||
const aligned = utils.align(points, { p1: points[0], p2: points[order] }); | ||
this._linear = !aligned.some((p) => abs$1(p.y) > 0.0001); | ||
const baselength = utils.dist(points[0], points[order]); | ||
this._linear = aligned.reduce((t, p) => t + abs$1(p.y), 0) < baselength / 50; | ||
@@ -1603,2 +1597,18 @@ this._lut = []; | ||
translate(v, d1, d2) { | ||
d2 = typeof d2 === "number" ? d2 : d1; | ||
// TODO: make this take curves with control points outside | ||
// of the start-end interval into account | ||
const o = this.order; | ||
let d = this.points.map((_, i) => (1 - i / o) * d1 + (i / o) * d2); | ||
return new Bezier( | ||
this.points.map((p, i) => ({ | ||
x: p.x + v.x * d[i], | ||
y: p.y + v.y * d[i], | ||
})) | ||
); | ||
} | ||
scale(d) { | ||
@@ -1614,8 +1624,18 @@ const order = this.order; | ||
// TODO: add special handling for degenerate (=linear) curves. | ||
// TODO: add special handling for non-linear degenerate curves. | ||
const clockwise = this.clockwise; | ||
const points = this.points; | ||
if (this._linear) { | ||
return this.translate( | ||
this.normal(0), | ||
distanceFn ? distanceFn(0) : d, | ||
distanceFn ? distanceFn(1) : d | ||
); | ||
} | ||
const r1 = distanceFn ? distanceFn(0) : d; | ||
const r2 = distanceFn ? distanceFn(1) : d; | ||
const v = [this.offset(0, 10), this.offset(1, 10)]; | ||
const points = this.points; | ||
const np = []; | ||
@@ -1627,5 +1647,5 @@ const o = utils.lli4(v[0], v[0].c, v[1], v[1].c); | ||
} | ||
// move all points by distance 'd' wrt the origin 'o' | ||
// move end points by fixed distance along normal. | ||
// move all points by distance 'd' wrt the origin 'o', | ||
// and move end points by fixed distance along normal. | ||
[0, 1].forEach(function (t) { | ||
@@ -1673,3 +1693,34 @@ const p = (np[t * order] = utils.copy(points[t * order])); | ||
outline(d1, d2, d3, d4) { | ||
d2 = typeof d2 === "undefined" ? d1 : d2; | ||
d2 = d2 === undefined ? d1 : d2; | ||
if (this._linear) { | ||
// TODO: find the actual extrema, because they might | ||
// be before the start, or past the end. | ||
const n = this.normal(0); | ||
const start = this.points[0]; | ||
const end = this.points[this.points.length - 1]; | ||
let s, mid, e; | ||
if (d3 === undefined) { | ||
d3 = d1; | ||
d4 = d2; | ||
} | ||
s = { x: start.x + n.x * d1, y: start.y + n.y * d1 }; | ||
e = { x: end.x + n.x * d3, y: end.y + n.y * d3 }; | ||
mid = { x: (s.x + e.x) / 2, y: (s.y + e.y) / 2 }; | ||
const fline = [s, mid, e]; | ||
s = { x: start.x - n.x * d2, y: start.y - n.y * d2 }; | ||
e = { x: end.x - n.x * d4, y: end.y - n.y * d4 }; | ||
mid = { x: (s.x + e.x) / 2, y: (s.y + e.y) / 2 }; | ||
const bline = [e, mid, s]; | ||
const ls = utils.makeline(bline[2], fline[0]); | ||
const le = utils.makeline(fline[2], bline[0]); | ||
const segments = [ls, new Bezier(fline), le, new Bezier(bline)]; | ||
return new PolyBezier(segments); | ||
} | ||
const reduced = this.reduce(), | ||
@@ -1676,0 +1727,0 @@ len = reduced.length, |
{ | ||
"name": "bezier-js", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"author": "Pomax", | ||
@@ -31,4 +31,5 @@ "description": "A javascript library for working with Bezier curves", | ||
"test": "npm start", | ||
"start": "run-s lint:* mocha build:*", | ||
"start": "run-s lint:* mocha build", | ||
"-----": "------------------------------------------", | ||
"build": "run-s build:*", | ||
"build:docs": "npm run rollup -- ./docs/js/bezier.js", | ||
@@ -35,0 +36,0 @@ "build:dist": "npm run rollup -- ./dist/bezier.js", |
@@ -85,7 +85,8 @@ /** | ||
// is this curve, practically speaking, a straight line? | ||
const aligned = utils.align(points, { p1: points[0], p2: points[order] }); | ||
this._linear = !aligned.some((p) => abs(p.y) > 0.0001); | ||
const baselength = utils.dist(points[0], points[order]); | ||
this._linear = aligned.reduce((t, p) => t + abs(p.y), 0) < baselength / 50; | ||
this._lut = []; | ||
this._t1 = 0; | ||
@@ -630,2 +631,18 @@ this._t2 = 1; | ||
translate(v, d1, d2) { | ||
d2 = typeof d2 === "number" ? d2 : d1; | ||
// TODO: make this take curves with control points outside | ||
// of the start-end interval into account | ||
const o = this.order; | ||
let d = this.points.map((_, i) => (1 - i / o) * d1 + (i / o) * d2); | ||
return new Bezier( | ||
this.points.map((p, i) => ({ | ||
x: p.x + v.x * d[i], | ||
y: p.y + v.y * d[i], | ||
})) | ||
); | ||
} | ||
scale(d) { | ||
@@ -641,8 +658,18 @@ const order = this.order; | ||
// TODO: add special handling for degenerate (=linear) curves. | ||
// TODO: add special handling for non-linear degenerate curves. | ||
const clockwise = this.clockwise; | ||
const points = this.points; | ||
if (this._linear) { | ||
return this.translate( | ||
this.normal(0), | ||
distanceFn ? distanceFn(0) : d, | ||
distanceFn ? distanceFn(1) : d | ||
); | ||
} | ||
const r1 = distanceFn ? distanceFn(0) : d; | ||
const r2 = distanceFn ? distanceFn(1) : d; | ||
const v = [this.offset(0, 10), this.offset(1, 10)]; | ||
const points = this.points; | ||
const np = []; | ||
@@ -654,5 +681,5 @@ const o = utils.lli4(v[0], v[0].c, v[1], v[1].c); | ||
} | ||
// move all points by distance 'd' wrt the origin 'o' | ||
// move end points by fixed distance along normal. | ||
// move all points by distance 'd' wrt the origin 'o', | ||
// and move end points by fixed distance along normal. | ||
[0, 1].forEach(function (t) { | ||
@@ -700,3 +727,34 @@ const p = (np[t * order] = utils.copy(points[t * order])); | ||
outline(d1, d2, d3, d4) { | ||
d2 = typeof d2 === "undefined" ? d1 : d2; | ||
d2 = d2 === undefined ? d1 : d2; | ||
if (this._linear) { | ||
// TODO: find the actual extrema, because they might | ||
// be before the start, or past the end. | ||
const n = this.normal(0); | ||
const start = this.points[0]; | ||
const end = this.points[this.points.length - 1]; | ||
let s, mid, e; | ||
if (d3 === undefined) { | ||
d3 = d1; | ||
d4 = d2; | ||
} | ||
s = { x: start.x + n.x * d1, y: start.y + n.y * d1 }; | ||
e = { x: end.x + n.x * d3, y: end.y + n.y * d3 }; | ||
mid = { x: (s.x + e.x) / 2, y: (s.y + e.y) / 2 }; | ||
const fline = [s, mid, e]; | ||
s = { x: start.x - n.x * d2, y: start.y - n.y * d2 }; | ||
e = { x: end.x - n.x * d4, y: end.y - n.y * d4 }; | ||
mid = { x: (s.x + e.x) / 2, y: (s.y + e.y) / 2 }; | ||
const bline = [e, mid, s]; | ||
const ls = utils.makeline(bline[2], fline[0]); | ||
const le = utils.makeline(fline[2], bline[0]); | ||
const segments = [ls, new Bezier(fline), le, new Bezier(bline)]; | ||
return new PolyBezier(segments); | ||
} | ||
const reduced = this.reduce(), | ||
@@ -759,4 +817,3 @@ len = reduced.length, | ||
le = utils.makeline(fe, be), | ||
segments = [ls].concat(fcurves).concat([le]).concat(bcurves), | ||
slen = segments.length; | ||
segments = [ls].concat(fcurves).concat([le]).concat(bcurves); | ||
@@ -763,0 +820,0 @@ return new PolyBezier(segments); |
@@ -408,17 +408,9 @@ import { Bezier } from "./bezier.js"; | ||
makeline: function (p1, p2) { | ||
const x1 = p1.x, | ||
y1 = p1.y, | ||
x2 = p2.x, | ||
y2 = p2.y, | ||
dx = (x2 - x1) / 3, | ||
dy = (y2 - y1) / 3; | ||
return new Bezier( | ||
x1, | ||
y1, | ||
x1 + dx, | ||
y1 + dy, | ||
x1 + 2 * dx, | ||
y1 + 2 * dy, | ||
x2, | ||
y2 | ||
p1.x, | ||
p1.y, | ||
(p1.x + p2.x) / 2, | ||
(p1.y + p2.y) / 2, | ||
p2.x, | ||
p2.y | ||
); | ||
@@ -425,0 +417,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155942
5217