point-at-length
Advanced tools
Comparing version 1.0.1 to 1.0.2
41
index.js
@@ -9,3 +9,5 @@ var parse = require('parse-svg-path'); | ||
if (!(this instanceof Points)) return new Points(path); | ||
this._path = abs(isarray(path) ? path : parse(path)); | ||
this._path = isarray(path) ? path : parse(path); | ||
this._path = abs(this._path); | ||
this._path = zToL(this._path); | ||
} | ||
@@ -96,13 +98,18 @@ | ||
else if (p[0] === 'L') { | ||
len += dist(cur[0], cur[1], p[1], p[2]); | ||
prev[0] = cur[0]; | ||
prev[1] = cur[1]; | ||
prev[2] = len; | ||
len += dist(cur[0], cur[1], p[1], p[2]); | ||
cur[0] = p[1]; | ||
cur[1] = p[2]; | ||
if (typeof pos === 'number' && len >= pos) { | ||
var dv = (len - pos) / (len - prev[2]); | ||
var npos = [ | ||
cur[0] * (1 - dv) + p[0] * dv, | ||
cur[1] * (1 - dv) + p[1] * dv | ||
cur[0] * (1 - dv) + prev[0] * dv, | ||
cur[1] * (1 - dv) + prev[1] * dv | ||
]; | ||
return { length: len, pos: npos }; | ||
} | ||
cur[0] = p[1]; | ||
cur[1] = p[2]; | ||
prev[0] = cur[0]; | ||
@@ -149,1 +156,23 @@ prev[1] = cur[1]; | ||
} | ||
// Convert 'Z' segments to 'L' segments | ||
function zToL(path){ | ||
var ret = []; | ||
var startPoint = ['L',0,0]; | ||
for(var i=0, len=path.length; i<len; i++){ | ||
var pt = path[i]; | ||
switch(pt[0]){ | ||
case 'M': | ||
startPoint = ['L', pt[1], pt[2]]; | ||
ret.push(pt); | ||
break; | ||
case 'Z': | ||
ret.push(startPoint); | ||
break; | ||
default: | ||
ret.push(pt); | ||
} | ||
} | ||
return ret; | ||
} |
{ | ||
"name": "point-at-length", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "compute a point from an svg path string", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
107
test/at.js
@@ -13,55 +13,60 @@ var test = require('tape'); | ||
+ ' 24.32036,-88.78668 z' | ||
+ ' M 363.38912,100.18734 l 7.78212,25.23786 l 15.68721,-12.87912 L 363.38912,100.18734 z' | ||
; | ||
var expected = [ | ||
[ 340.0802001953125, 61.38650894165039 ], | ||
[ 321.8157653808594, 69.20254516601562 ], | ||
[ 307.4777526855469, 83.02464294433594 ], | ||
[ 297.37322998046875, 100.23261260986328 ], | ||
[ 290.9107971191406, 119.13245391845703 ], | ||
[ 287.5101318359375, 138.82359313964844 ], | ||
[ 286.7633056640625, 158.79637145996094 ], | ||
[ 288.40179443359375, 178.7179718017578 ], | ||
[ 292.2458801269531, 198.3347625732422 ], | ||
[ 298.2345886230469, 217.40565490722656 ], | ||
[ 306.42352294921875, 235.6382598876953 ], | ||
[ 316.92340087890625, 252.6409149169922 ], | ||
[ 329.89788818359375, 267.8296203613281 ], | ||
[ 345.5393371582031, 280.2276611328125 ], | ||
[ 363.8115234375, 288.1679992675781 ], | ||
[ 383.6481018066406, 289.346923828125 ], | ||
[ 402.3357238769531, 282.6163024902344 ], | ||
[ 417.3066711425781, 269.49432373046875 ], | ||
[ 427.99755859375, 252.64942932128906 ], | ||
[ 434.95172119140625, 233.9269561767578 ], | ||
[ 438.7707214355469, 214.31356811523438 ], | ||
[ 439.8885498046875, 194.35853576660156 ], | ||
[ 438.5883483886719, 174.4122314453125 ], | ||
[ 435.0616455078125, 154.73593139648438 ], | ||
[ 429.3913879394531, 135.56776428222656 ], | ||
[ 421.5321044921875, 117.1902084350586 ], | ||
[ 411.380126953125, 99.97671508789062 ], | ||
[ 398.779541015625, 84.47447967529297 ], | ||
[ 383.53558349609375, 71.58562469482422 ], | ||
[ 365.6178283691406, 62.85908889770508 ], | ||
[ 345.87298583984375, 60.56299591064453 ], | ||
[ 366.84722900390625, 71.46846008300781 ], | ||
[ 382.8489990234375, 83.2032470703125 ], | ||
[ 394.5492858886719, 99.37156677246094 ], | ||
[ 403.0741271972656, 117.44123840332031 ], | ||
[ 408.9737548828125, 136.5365447998047 ], | ||
[ 412.45123291015625, 156.21900939941406 ], | ||
[ 413.35809326171875, 176.18238830566406 ], | ||
[ 411.1669921875, 196.03546142578125 ], | ||
[ 404.64031982421875, 214.86825561523438 ], | ||
[ 391.0517883300781, 229.10057067871094 ], | ||
[ 371.6767883300781, 229.2152862548828 ], | ||
[ 355.7667541503906, 217.34901428222656 ], | ||
[ 344.1390686035156, 201.1275177001953 ], | ||
[ 335.670654296875, 183.03115844726562 ], | ||
[ 329.8182678222656, 163.9211883544922 ], | ||
[ 326.3846435546875, 144.23098754882812 ], | ||
[ 325.52410888671875, 124.26554870605469 ], | ||
[ 327.773681640625, 104.41944122314453 ], | ||
[ 334.3923645019531, 85.62057495117188 ], | ||
[ 348.13775634765625, 71.55425262451172 ] | ||
[340.0802001953125,61.38650894165039], | ||
[321.815673828125,69.2024917602539], | ||
[307.4776611328125,83.02471160888672], | ||
[297.3731994628906,100.23271942138672], | ||
[290.91046142578125,119.13249969482422], | ||
[287.50921630859375,138.82366943359375], | ||
[286.7622985839844,158.796630859375], | ||
[288.4009704589844,178.71836853027344], | ||
[292.24517822265625,198.3352813720703], | ||
[298.23358154296875,217.4064178466797], | ||
[306.42340087890625,235.6387939453125], | ||
[316.9225769042969,252.64205932617188], | ||
[329.898193359375,267.8299560546875], | ||
[345.53973388671875,280.2279052734375], | ||
[363.81201171875,288.1681823730469], | ||
[383.6486511230469,289.34698486328125], | ||
[402.33636474609375,282.61614990234375], | ||
[417.3070983886719,269.49383544921875], | ||
[427.9978332519531,252.6488800048828], | ||
[434.951904296875,233.9263458251953], | ||
[438.77215576171875,214.3130340576172], | ||
[439.88922119140625,194.35777282714844], | ||
[438.58905029296875,174.41134643554688], | ||
[435.06219482421875,154.7349853515625], | ||
[429.3924865722656,135.56646728515625], | ||
[421.5318908691406,117.1893310546875], | ||
[411.380126953125,99.9755630493164], | ||
[398.7788391113281,84.4737777709961], | ||
[383.5347900390625,71.5850830078125], | ||
[365.6168518066406,62.85874557495117], | ||
[345.8718566894531,60.56281280517578], | ||
[366.848388671875,71.46869659423828], | ||
[382.8503723144531,83.20372009277344], | ||
[394.5501708984375,99.37262725830078], | ||
[403.0749206542969,117.44243621826172], | ||
[408.9747009277344,136.5377655029297], | ||
[412.4517517089844,156.22030639648438], | ||
[413.3585205078125,176.18382263183594], | ||
[411.1668701171875,196.03689575195312], | ||
[404.6397705078125,214.86961364746094], | ||
[391.05059814453125,229.1015625], | ||
[371.67529296875,229.21475219726562], | ||
[355.7654113769531,217.34808349609375], | ||
[344.1375732421875,201.12646484375], | ||
[335.6697692871094,183.0297088623047], | ||
[329.81781005859375,163.91954040527344], | ||
[326.3842468261719,144.229248046875], | ||
[325.5235900878906,124.26376342773438], | ||
[327.773193359375,104.41747283935547], | ||
[334.3932800292969,85.61891174316406], | ||
[348.1394958496094,71.55331420898438], | ||
[367.772705078125,114.40347290039062], | ||
[377.7147216796875,120.05304718017578], | ||
[379.63006591796875,108.73966217041016], | ||
[363.3891296386719,100.18733978271484] | ||
]; | ||
@@ -68,0 +73,0 @@ |
@@ -13,2 +13,3 @@ var test = require('tape'); | ||
+ ' 24.32036,-88.78668 z' | ||
+ ' M 363.38912,100.18734 l 7.78212,25.23786 l 15.68721,-12.87912 L 363.38912,100.18734 z' | ||
; | ||
@@ -20,3 +21,3 @@ | ||
var len = pt.length(); | ||
var ref = 1005.1253662109375; | ||
var ref = 1078.3551025390625; | ||
t.ok(cmp(len, ref, 0.005), len + ' ~~ ' + ref + ' ±0.5%'); | ||
@@ -23,0 +24,0 @@ }); |
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
14834
288