From 1144404e4bea677c293e9bad273ce273aa1cd885 Mon Sep 17 00:00:00 2001 From: Ulrich-Matthias Schäfer Date: Mon, 20 Feb 2017 15:29:14 +0100 Subject: Added clone method to SVG.Array/PointArray/PathArray (#590) --- src/array.js | 24 ++++++++++++++---------- src/helpers.js | 12 ++++++++++++ src/patharray.js | 3 ++- src/pointarray.js | 4 ++-- 4 files changed, 30 insertions(+), 13 deletions(-) (limited to 'src') 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() } - }) -- cgit v1.2.3