summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2017-02-20 15:29:14 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2017-02-20 15:29:14 +0100
commit1144404e4bea677c293e9bad273ce273aa1cd885 (patch)
treeec66756c26c653194619a055dffa0234811b55fe /src
parentdfc07aa5be10a2b3749c3aae548d3652f0b780d5 (diff)
downloadsvg.js-1144404e4bea677c293e9bad273ce273aa1cd885.tar.gz
svg.js-1144404e4bea677c293e9bad273ce273aa1cd885.zip
Added clone method to SVG.Array/PointArray/PathArray (#590)
Diffstat (limited to 'src')
-rw-r--r--src/array.js24
-rw-r--r--src/helpers.js12
-rw-r--r--src/patharray.js3
-rw-r--r--src/pointarray.js4
4 files changed, 30 insertions, 13 deletions
diff --git a/src/array.js b/src/array.js
index 8f47774..4589835 100644
--- a/src/array.js
+++ b/src/array.js
@@ -2,11 +2,11 @@
SVG.Array = function(array, fallback) {
array = (array || []).valueOf()
- // if array is empty and fallback is provided, use fallback
+ // if array is empty and fallback is provided, use fallback
if (array.length == 0 && fallback)
array = fallback.valueOf()
- // parse array
+ // parse array
this.value = this.parse(array)
}
@@ -15,7 +15,7 @@ SVG.extend(SVG.Array, {
morph: function(array) {
this.destination = this.parse(array)
- // normalize length of arrays
+ // normalize length of arrays
if (this.value.length != this.destination.length) {
var lastValue = this.value[this.value.length - 1]
, lastDestination = this.destination[this.destination.length - 1]
@@ -25,25 +25,25 @@ SVG.extend(SVG.Array, {
while(this.value.length < this.destination.length)
this.value.push(lastValue)
}
-
+
return this
}
// Clean up any duplicate points
, settle: function() {
- // find all unique values
+ // find all unique values
for (var i = 0, il = this.value.length, seen = []; i < il; i++)
if (seen.indexOf(this.value[i]) == -1)
seen.push(this.value[i])
- // set new value
+ // set new value
return this.value = seen
}
// 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 array
+ // generate morphed array
for (var i = 0, il = this.value.length, array = []; i < il; i++)
array.push(this.value[i] + (this.destination[i] - this.value[i]) * pos)
@@ -61,7 +61,7 @@ SVG.extend(SVG.Array, {
, 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
return this.split(array)
@@ -76,5 +76,9 @@ SVG.extend(SVG.Array, {
return this
}
-
+, clone: function() {
+ var clone = new this.constructor()
+ clone.value = array_clone(this.value)
+ return clone
+ }
}) \ No newline at end of file
diff --git a/src/helpers.js b/src/helpers.js
index aa23bbe..2a52a1b 100644
--- a/src/helpers.js
+++ b/src/helpers.js
@@ -1,3 +1,15 @@
+// creates deep clone of array
+function array_clone(arr){
+ var clone = arr.slice(0)
+ for(var i = clone.length; i--;){
+ if(Array.isArray(clone[i])){
+ clone[i] = array_clone(clone[i])
+ }
+ }
+ return clone
+}
+
+// tests if a given element is instance of an object
function is(el, obj){
return el instanceof obj
}
diff --git a/src/patharray.js b/src/patharray.js
index 479c66f..366729e 100644
--- a/src/patharray.js
+++ b/src/patharray.js
@@ -1,10 +1,11 @@
// Path points array
SVG.PathArray = function(array, fallback) {
- this.constructor.call(this, array, fallback || [['M', 0, 0]])
+ SVG.Array.call(this, array, fallback || [['M', 0, 0]])
}
// Inherit from SVG.Array
SVG.PathArray.prototype = new SVG.Array
+SVG.PathArray.prototype.constructor = SVG.PathArray
SVG.extend(SVG.PathArray, {
// Convert array to string
diff --git a/src/pointarray.js b/src/pointarray.js
index 0a4401a..fa87c4b 100644
--- a/src/pointarray.js
+++ b/src/pointarray.js
@@ -1,10 +1,11 @@
// Poly points array
SVG.PointArray = function(array, fallback) {
- this.constructor.call(this, array, fallback || [[0,0]])
+ SVG.Array.call(this, array, fallback || [[0,0]])
}
// Inherit from SVG.Array
SVG.PointArray.prototype = new SVG.Array
+SVG.PointArray.prototype.constructor = SVG.PointArray
SVG.extend(SVG.PointArray, {
// Convert array to string
@@ -98,5 +99,4 @@ SVG.extend(SVG.PointArray, {
return SVG.parser.poly.getBBox()
}
-
})