point-at-length
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -0,0 +0,0 @@ var point = require('../'); |
var point = require('../'); | ||
var pts = point(process.argv.slice(2).join(' ')); | ||
console.log(pts.length()); |
@@ -0,0 +0,0 @@ var point = require('../'); |
86
index.js
@@ -11,3 +11,4 @@ var parse = require('parse-svg-path'); | ||
this._path = abs(this._path); | ||
this._path = zToL(this._path); | ||
this._path = zvhToL(this._path); | ||
this._path = longhand(this._path); | ||
} | ||
@@ -26,6 +27,5 @@ | ||
var prev = [ 0, 0, 0 ]; | ||
var p0 = [ 0, 0 ]; | ||
var len = 0; | ||
var fudge = 1.045; | ||
if (typeof pos === 'number') pos *= fudge; | ||
for (var i = 0; i < this._path.length; i++) { | ||
@@ -41,6 +41,6 @@ var p = this._path[i]; | ||
else if (p[0] === 'C') { | ||
prev[0] = cur[0]; | ||
prev[1] = cur[1]; | ||
prev[0] = p0[0] = cur[0]; | ||
prev[1] = p0[1] = cur[1]; | ||
prev[2] = len; | ||
var n = 100; | ||
@@ -52,9 +52,9 @@ for (var j = 0; j <= n; j++) { | ||
len += dist(cur[0], cur[1], x, y); | ||
cur[0] = x; | ||
cur[1] = y; | ||
if (typeof pos === 'number' && len >= pos) { | ||
var dv = (len - pos) / (len - prev[2]); | ||
var npos = [ | ||
@@ -72,6 +72,6 @@ cur[0] * (1 - dv) + prev[0] * dv, | ||
else if (p[0] === 'Q') { | ||
prev[0] = cur[0]; | ||
prev[1] = cur[1]; | ||
prev[0] = p0[0] = cur[0]; | ||
prev[1] = p0[1] = cur[1]; | ||
prev[2] = len; | ||
var n = 100; | ||
@@ -83,9 +83,9 @@ for (var j = 0; j <= n; j++) { | ||
len += dist(cur[0], cur[1], x, y); | ||
cur[0] = x; | ||
cur[1] = y; | ||
if (typeof pos === 'number' && len >= pos) { | ||
var dv = (len - pos) / (len - prev[2]); | ||
var npos = [ | ||
@@ -124,6 +124,6 @@ cur[0] * (1 - dv) + prev[0] * dv, | ||
} | ||
return { length: len / fudge, pos: cur }; | ||
return { length: len, pos: cur }; | ||
function xof_C (p, t) { | ||
return Math.pow((1-t), 3) * cur[0] | ||
return Math.pow((1-t), 3) * p0[0] | ||
+ 3 * Math.pow((1-t), 2) * t * p[1] | ||
@@ -135,3 +135,3 @@ + 3 * (1-t) * Math.pow(t, 2) * p[3] | ||
function yof_C (p, t) { | ||
return Math.pow((1-t), 3) * cur[1] | ||
return Math.pow((1-t), 3) * p0[1] | ||
+ 3 * Math.pow((1-t), 2) * t * p[2] | ||
@@ -144,3 +144,3 @@ + 3 * (1-t) * Math.pow(t, 2) * p[4] | ||
function xof_Q (p, t) { | ||
return Math.pow((1-t), 2) * cur[0] | ||
return Math.pow((1-t), 2) * p0[0] | ||
+ 2 * (1-t) * t * p[1] | ||
@@ -151,3 +151,3 @@ + Math.pow(t, 2) * p[3] | ||
function yof_Q (p, t) { | ||
return Math.pow((1-t), 2) * cur[1] | ||
return Math.pow((1-t), 2) * p0[1] | ||
+ 2 * (1-t) * t * p[2] | ||
@@ -165,6 +165,34 @@ + Math.pow(t, 2) * p[4] | ||
// Convert 'Z' segments to 'L' segments | ||
function zToL(path){ | ||
// Expand shorthand curve commands to full versions; mutates the path in place for efficiency | ||
// Requires commands have already been converted to absolute versions | ||
function longhand(path){ | ||
var prev,x1=0,y1=0; | ||
var conversion = { S:{to:'C',x:3}, T:{to:'Q',x:1} }; | ||
for(var i=0, len=path.length; i<len; i++){ | ||
var cmd = path[i]; | ||
var convert = conversion[cmd[0]]; | ||
if (convert) { | ||
cmd[0] = convert.to; | ||
if (prev) { | ||
if (prev[0] === convert.to) { | ||
x1 = 2*prev[convert.x+2]-prev[convert.x ]; | ||
y1 = 2*prev[convert.x+3]-prev[convert.x+1]; | ||
} else { | ||
x1 = prev[prev.length-2]; | ||
y1 = prev[prev.length-1]; | ||
} | ||
} | ||
cmd.splice(1,0,x1,y1); | ||
} | ||
prev=cmd; | ||
} | ||
return path; | ||
} | ||
// Convert 'Z', 'V' and 'H' segments to 'L' segments | ||
function zvhToL(path){ | ||
var ret = []; | ||
var startPoint = ['L',0,0]; | ||
var last_point; | ||
@@ -181,3 +209,11 @@ for(var i=0, len=path.length; i<len; i++){ | ||
break; | ||
default: | ||
case 'H': | ||
last_point = ret[ret.length - 1] || ['L',0,0]; | ||
ret.push( ['L', pt[1], last_point[last_point.length - 1]] ); | ||
break; | ||
case 'V': | ||
last_point = ret[ret.length - 1] || ['L',0,0]; | ||
ret.push( ['L', last_point[last_point.length - 2], pt[1]] ); | ||
break; | ||
default: | ||
ret.push(pt); | ||
@@ -184,0 +220,0 @@ } |
{ | ||
"name": "point-at-length", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "compute a point from an svg path string", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,2 +14,3 @@ var test = require('tape'); | ||
+ ' M 363.38912,100.18734 l 7.78212,25.23786 l 15.68721,-12.87912 L 363.38912,100.18734 z' | ||
+ ' v 25 h 75' | ||
; | ||
@@ -71,3 +72,8 @@ var expected = [ | ||
[379.63006591796875,108.73966217041016], | ||
[363.3891296386719,100.18733978271484] | ||
[363.3891296386719,100.18733978271484], | ||
[363.3891296386719,121.84354470328532], | ||
[380.0453247032853,125.18733978271484], | ||
[400.0453247032853,125.18733978271484], | ||
[420.0453247032853,125.18733978271484], | ||
[438.3891296386719,125.18733978271484] | ||
]; | ||
@@ -74,0 +80,0 @@ |
@@ -14,2 +14,3 @@ var test = require('tape'); | ||
+ ' M 363.38912,100.18734 l 7.78212,25.23786 l 15.68721,-12.87912 L 363.38912,100.18734 z' | ||
+ ' v 25 h 75' | ||
; | ||
@@ -21,3 +22,3 @@ | ||
var len = pt.length(); | ||
var ref = 1078.3551025390625; | ||
var ref = 1178.3551025390625; | ||
t.ok(cmp(len, ref, 0.005), len + ' ~~ ' + ref + ' ±0.5%'); | ||
@@ -24,0 +25,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
16761
329
1