summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authordotnetCarpenter <jon.ronnenberg@gmail.com>2016-10-11 19:30:28 -0200
committerdotnetCarpenter <jon.ronnenberg@gmail.com>2016-10-12 12:53:24 -0200
commiteed96bcf59c00584527ac87902ba48669cb8cb6e (patch)
treea500e8e983ef9f85b8a9b4fee645f99668996d8c /src
parent92fb920e110645bdc6e56a1b349662a6b48a981c (diff)
downloadsvg.js-eed96bcf59c00584527ac87902ba48669cb8cb6e.tar.gz
svg.js-eed96bcf59c00584527ac87902ba48669cb8cb6e.zip
fixes #525 by accepting/parsing coordianate strings where x and y is seperated by comma and or by space - we count the shape coordinates until (and including) the maximum even number
Diffstat (limited to 'src')
-rw-r--r--src/pointarray.js40
1 files changed, 26 insertions, 14 deletions
diff --git a/src/pointarray.js b/src/pointarray.js
index d5c632c..b935cb5 100644
--- a/src/pointarray.js
+++ b/src/pointarray.js
@@ -9,7 +9,7 @@ SVG.PointArray.prototype = new SVG.Array
SVG.extend(SVG.PointArray, {
// Convert array to string
toString: function() {
- // convert to a poly point string
+ // convert to a poly point string
for (var i = 0, il = this.value.length, array = []; i < il; i++)
array.push(this.value[i].join(','))
@@ -26,10 +26,10 @@ SVG.extend(SVG.PointArray, {
}
// Get morphed array at given position
, at: function(pos) {
- // make sure a destination is defined
+ // make sure a destination is defined
if (!this.destination) return this
- // generate morphed point string
+ // generate morphed point string
for (var i = 0, il = this.value.length, array = []; i < il; i++)
array.push([
this.value[i][0] + (this.destination[i][0] - this.value[i][0]) * pos
@@ -42,29 +42,41 @@ SVG.extend(SVG.PointArray, {
, parse: function(array) {
array = array.valueOf()
- // if already is an array, no need to parse it
+ // if already is an array, no need to parse it
if (Array.isArray(array)) return array
- // split points
- array = this.split(array)
- // parse points
- for (var i = 0, il = array.length, p, points = []; i < il; i++) {
- p = array[i].split(',')
- points.push([parseFloat(p[0]), parseFloat(p[1])])
- }
+ // parse points
+ var points = array.split(/\s|,/)
+
+ // validate points - https://svgwg.org/svg2-draft/shapes.html#DataTypePoints
+ // Odd number of coordinates is an error. In such cases, drop the last odd coordinate.
+ if (points.length % 2 !== 0) points.pop()
+
+ // parse points as floats
+ points = points.map(function(x) { return parseFloat(x) })
+
+ // wrap points in two-tuples
+ points = points.map(wrapPoints()).filter(function(x) { return x })
return points
+
+ function wrapPoints() {
+ var a
+ return function(b, i) {
+ return i % 2 === 0 ? (a = b, null) : [a,b]
+ }
+ }
}
// Move point string
, move: function(x, y) {
var box = this.bbox()
- // get relative offset
+ // get relative offset
x -= box.x
y -= box.y
- // move every point
+ // move every point
if (!isNaN(x) && !isNaN(y))
for (var i = this.value.length - 1; i >= 0; i--)
this.value[i] = [this.value[i][0] + x, this.value[i][1] + y]
@@ -75,7 +87,7 @@ SVG.extend(SVG.PointArray, {
, size: function(width, height) {
var i, box = this.bbox()
- // recalculate position of all points according to new size
+ // recalculate position of all points according to new size
for (i = this.value.length - 1; i >= 0; i--) {
this.value[i][0] = ((this.value[i][0] - box.x) * width) / box.width + box.x
this.value[i][1] = ((this.value[i][1] - box.y) * height) / box.height + box.y