From 8bf3278304b8ee53b6d69c44a778bc5ae7539141 Mon Sep 17 00:00:00 2001 From: liabru Date: Sun, 1 Mar 2015 17:26:46 +0000 Subject: [PATCH] cleanup Svg.pathToPoints --- src/geometry/Svg.js | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/src/geometry/Svg.js b/src/geometry/Svg.js index 434ec49a..281d8b82 100644 --- a/src/geometry/Svg.js +++ b/src/geometry/Svg.js @@ -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) @@ -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; @@ -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 @@ -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) @@ -140,6 +119,7 @@ var Svg = {}; } // add points in between when curving + // TODO: adaptive sampling switch (segment.pathSegTypeAsLetter.toUpperCase()) { case 'C': @@ -154,7 +134,7 @@ var Svg = {}; } // increment by sample value - length += distance; + length += sampleLength; } // add remaining segments we didn't pass while sampling