mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-04-07 07:51:00 +00:00
Simplify point parsing logic
The SVG spec basically allows delimiting numbers and pairs of numbers with either whitespace or commas, so convert all commas to whitespace, split on whitespace and remove the `asPairs` logic
This commit is contained in:
parent
a1cde528dc
commit
bbd1dd2d99
1 changed files with 9 additions and 28 deletions
|
|
@ -713,38 +713,19 @@
|
|||
// points attribute is required and must not be empty
|
||||
if (!points) return null;
|
||||
|
||||
points = points.trim();
|
||||
var asPairs = points.indexOf(',') > -1;
|
||||
// replace commas with whitespace and remove bookending whitespace
|
||||
points = points.replace(/,/g, ' ').trim();
|
||||
|
||||
// remove possible whitespace around commas
|
||||
if (asPairs) {
|
||||
points = points.replace(/\s*,\s*/g, ',')
|
||||
}
|
||||
|
||||
points = points.split(/\s+/);
|
||||
var parsedPoints = [ ], i, len;
|
||||
|
||||
// points could look like "10,20 30,40" or "10 20 30 40"
|
||||
if (asPairs) {
|
||||
i = 0;
|
||||
len = points.length;
|
||||
for (; i < len; i++) {
|
||||
var pair = points[i].split(',');
|
||||
parsedPoints.push({
|
||||
x: parseFloat(pair[0]),
|
||||
y: parseFloat(pair[1])
|
||||
});
|
||||
}
|
||||
}
|
||||
else {
|
||||
i = 0;
|
||||
len = points.length;
|
||||
for (; i < len; i+=2) {
|
||||
parsedPoints.push({
|
||||
x: parseFloat(points[i]),
|
||||
y: parseFloat(points[i + 1])
|
||||
});
|
||||
}
|
||||
i = 0;
|
||||
len = points.length;
|
||||
for (; i < len; i+=2) {
|
||||
parsedPoints.push({
|
||||
x: parseFloat(points[i]),
|
||||
y: parseFloat(points[i + 1])
|
||||
});
|
||||
}
|
||||
|
||||
// odd number of points is an error
|
||||
|
|
|
|||
Loading…
Reference in a new issue