polyline-normals
Advanced tools
Comparing version 1.0.2 to 2.0.0
14
index.js
@@ -8,8 +8,11 @@ var util = require('polyline-miter-util') | ||
module.exports = function(points) { | ||
var total = points.length | ||
module.exports = function(points, closed) { | ||
var curNormal = null | ||
var out = [] | ||
if (closed) { | ||
points = points.slice() | ||
points.push(points[0]) | ||
} | ||
var total = points.length | ||
for (var i=1; i<total; i++) { | ||
@@ -43,5 +46,3 @@ var last = points[i-1] | ||
//if the polyline is a closed loop, clean up the last normal | ||
if (points.length > 2 | ||
&& points[0][0] === points[total-1][0] | ||
&& points[0][1] === points[total-1][1]) { | ||
if (points.length > 2 && closed) { | ||
var last2 = points[total-2] | ||
@@ -60,2 +61,3 @@ var cur2 = points[0] | ||
out[total-1][1] = miterLen2 | ||
out.pop() | ||
} | ||
@@ -62,0 +64,0 @@ |
{ | ||
"name": "polyline-normals", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "gets miter normals for a 2D polyline", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,7 +12,7 @@ # polyline-normals | ||
//a triangle, closed | ||
var path = [ [0, 122], [0, 190], [90, 190], [0, 122] ] | ||
//a triangle | ||
var path = [ [0, 122], [0, 190], [90, 190] ] | ||
//get the normals | ||
var normals = getNormals(path) | ||
//get the normals, flag as closed loop | ||
var normals = getNormals(path, true) | ||
@@ -34,3 +34,3 @@ //now draw our thick line in 2D/3D/etc | ||
#### `normals(path)` | ||
#### `normals(path[, closed])` | ||
@@ -46,4 +46,6 @@ For the given path, produces a new array of the same length with normal information for each point. The data contains a normal, `[nx, ny]` and the length of the miter (default to 1.0 where no join occurs). | ||
If `closed` is true, it assumes a segment will be drawn from the last point to the first point, and adjusts those normals accordingly. | ||
## License | ||
MIT, see [LICENSE.md](http://github.com/mattdesl/polyline-normals/blob/master/LICENSE.md) for details. |
@@ -11,8 +11,17 @@ require('canvas-testbed')(render, { once: true }) | ||
paths.push( [ [40, 40], [80, 30], [80, 60], [125, 33], [115, 100], [50, 120], [70, 150] ] ) | ||
paths.push( arc(130, 120, 25, 0, Math.PI*2, false) ) | ||
paths.push( { path: circle(130, 120, 25), closed: true } ) | ||
paths.push( curve([40, 40], [70, 100], [120, 20], [200, 40], 5) ) | ||
paths.push( [ [0, 122], [0, 190], [90, 190], [0, 122] ] ) | ||
paths.push( [ [50, 50], [100, 50], [100, 100], [50, 100], [50, 50] ] ) | ||
paths.push( { path: [ [0, 122], [0, 190], [90, 190] ], closed: true } ) | ||
paths.push( { path: [ [50, 50], [100, 50], [100, 100], [50, 100] ], closed: true } ) | ||
paths.push( [ [30, -60], [80, 10] ] ) | ||
function circle(x, y, radius) { | ||
//in this case arc-to closes itself by making the | ||
//last point equal to the first. we want to fix this | ||
//to pass in a more typical polyline and get the right normals | ||
var c = arc(x, y, radius, 0, Math.PI*2, false) | ||
c.pop() | ||
return c | ||
} | ||
function render(ctx, width, height) { | ||
@@ -23,3 +32,6 @@ ctx.clearRect(0,0,width,height) | ||
ctx.save() | ||
paths.forEach(function(path, i) { | ||
paths.forEach(function(data, i) { | ||
var path = Array.isArray(data) ? data : data.path | ||
var closed = typeof data === 'object' && data.closed | ||
var cols = 3 | ||
@@ -29,3 +41,3 @@ var x = i % cols, | ||
ctx.translate(x * 50, y * 50) | ||
draw(ctx, path) | ||
draw(ctx, path, closed) | ||
}) | ||
@@ -35,3 +47,3 @@ ctx.restore() | ||
function draw(ctx, path) { | ||
function draw(ctx, path, closed) { | ||
var thick = 25, | ||
@@ -46,3 +58,9 @@ halfThick = thick / 2 | ||
//get the normals of the path | ||
var normals = getNormals(path) | ||
var normals = getNormals(path, closed) | ||
//for drawing the join, we can just add the first point | ||
if (closed) { | ||
path.push(path[0]) | ||
normals.push(normals[0]) | ||
} | ||
@@ -49,0 +67,0 @@ //draw our expanded vertices for each point in the path |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
49
8218
6
134
1