mirror of
https://github.com/liabru/matter-js.git
synced 2025-01-12 16:08:50 -05:00
cleanup Svg.pathToPoints
This commit is contained in:
parent
7822ead3bf
commit
8bf3278304
1 changed files with 10 additions and 30 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue