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`.
|
* Converts an SVG path into a `Matter.Vertices` object for the given `Matter.Body`.
|
||||||
* @method pathToPoints
|
* @method pathToPoints
|
||||||
* @param {string} path
|
* @param {string} path
|
||||||
|
* @param {Number} [sampleLength=15]
|
||||||
* @return {Vector} points
|
* @return {Vector} points
|
||||||
*/
|
*/
|
||||||
Svg.pathToPoints = function(path) {
|
Svg.pathToPoints = function(path, sampleLength) {
|
||||||
var i, il, total, point, distance, segment, segments,
|
var i, il, total, point, segment, segments,
|
||||||
segmentsQueue, lastSegment,
|
segmentsQueue, lastSegment,
|
||||||
lastPoint, segmentIndex, points = [],
|
lastPoint, segmentIndex, points = [],
|
||||||
length = 0, x = 0, y = 0;
|
length = 0, x = 0, y = 0;
|
||||||
|
|
||||||
|
sampleLength = sampleLength || 15;
|
||||||
|
|
||||||
// prepare helpers functions
|
// prepare helpers functions
|
||||||
var addPoint = function(px, py, pathSegType) {
|
var addPoint = function(px, py, pathSegType) {
|
||||||
// all odd-numbered path types are relative, except PATHSEG_CLOSEPATH (1)
|
// all odd-numbered path types are relative, except PATHSEG_CLOSEPATH (1)
|
||||||
|
@ -68,6 +71,9 @@ var Svg = {};
|
||||||
case 'M':
|
case 'M':
|
||||||
case 'L':
|
case 'L':
|
||||||
case 'T':
|
case 'T':
|
||||||
|
case 'C':
|
||||||
|
case 'S':
|
||||||
|
case 'Q':
|
||||||
x = segment.x;
|
x = segment.x;
|
||||||
y = segment.y;
|
y = segment.y;
|
||||||
break;
|
break;
|
||||||
|
@ -77,16 +83,6 @@ var Svg = {};
|
||||||
case 'V':
|
case 'V':
|
||||||
y = segment.y;
|
y = segment.y;
|
||||||
break;
|
break;
|
||||||
case 'C':
|
|
||||||
x = segment.x;
|
|
||||||
y = segment.y;
|
|
||||||
break;
|
|
||||||
case 'S':
|
|
||||||
case 'Q':
|
|
||||||
x = segment.x;
|
|
||||||
y = segment.y;
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add point
|
// add point
|
||||||
|
@ -96,26 +92,9 @@ var Svg = {};
|
||||||
// ensure path is absolute
|
// ensure path is absolute
|
||||||
_svgPathToAbsolute(path);
|
_svgPathToAbsolute(path);
|
||||||
|
|
||||||
// parse sample value
|
|
||||||
sample = '1%';
|
|
||||||
|
|
||||||
// get total length
|
// get total length
|
||||||
total = path.getTotalLength();
|
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
|
// Put all path segments in a queue
|
||||||
segments = [];
|
segments = [];
|
||||||
for (i = 0; i < path.pathSegList.numberOfItems; i += 1)
|
for (i = 0; i < path.pathSegList.numberOfItems; i += 1)
|
||||||
|
@ -140,6 +119,7 @@ var Svg = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// add points in between when curving
|
// add points in between when curving
|
||||||
|
// TODO: adaptive sampling
|
||||||
switch (segment.pathSegTypeAsLetter.toUpperCase()) {
|
switch (segment.pathSegTypeAsLetter.toUpperCase()) {
|
||||||
|
|
||||||
case 'C':
|
case 'C':
|
||||||
|
@ -154,7 +134,7 @@ var Svg = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// increment by sample value
|
// increment by sample value
|
||||||
length += distance;
|
length += sampleLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add remaining segments we didn't pass while sampling
|
// add remaining segments we didn't pass while sampling
|
||||||
|
|
Loading…
Reference in a new issue