Skip to content

Commit

Permalink
cleanup Svg.pathToPoints
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Mar 1, 2015
1 parent 7822ead commit 8bf3278
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions src/geometry/Svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ var Svg = {};
* Converts an SVG path into a `Matter.Vertices` object for the given `Matter.Body`.
* @method pathToPoints
* @param {string} path
* @param {Number} [sampleLength=15]
* @return {Vector} points
*/
Svg.pathToPoints = function(path) {
var i, il, total, point, distance, segment, segments,
Svg.pathToPoints = function(path, sampleLength) {
var i, il, total, point, segment, segments,
segmentsQueue, lastSegment,
lastPoint, segmentIndex, points = [],
length = 0, x = 0, y = 0;

sampleLength = sampleLength || 15;

// prepare helpers functions
var addPoint = function(px, py, pathSegType) {
// all odd-numbered path types are relative, except PATHSEG_CLOSEPATH (1)
Expand Down Expand Up @@ -68,6 +71,9 @@ var Svg = {};
case 'M':
case 'L':
case 'T':
case 'C':
case 'S':
case 'Q':
x = segment.x;
y = segment.y;
break;
Expand All @@ -77,16 +83,6 @@ var Svg = {};
case 'V':
y = segment.y;
break;
case 'C':
x = segment.x;
y = segment.y;
break;
case 'S':
case 'Q':
x = segment.x;
y = segment.y;
break;

}

// add point
Expand All @@ -96,26 +92,9 @@ var Svg = {};
// ensure path is absolute
_svgPathToAbsolute(path);

// parse sample value
sample = '1%';

// get total length
total = path.getTotalLength();

// calculate sample distance
if (typeof sample === 'string') {
if (sample.indexOf('%') > -1) {
// sample distance in %
distance = total * (parseFloat(sample) / 100);
} else if (sample.indexOf('px') > -1) {
// fixed sample distance in px
distance = parseFloat(sample);
}
} else {
// specific number of samples
distance = total / sample;
}

// Put all path segments in a queue
segments = [];
for (i = 0; i < path.pathSegList.numberOfItems; i += 1)
Expand All @@ -140,6 +119,7 @@ var Svg = {};
}

// add points in between when curving
// TODO: adaptive sampling
switch (segment.pathSegTypeAsLetter.toUpperCase()) {

case 'C':
Expand All @@ -154,7 +134,7 @@ var Svg = {};
}

// increment by sample value
length += distance;
length += sampleLength;
}

// add remaining segments we didn't pass while sampling
Expand Down

0 comments on commit 8bf3278

Please sign in to comment.