diff options
author | wout <wout@impinc.co.uk> | 2014-08-28 18:44:14 +0200 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2014-08-28 18:44:14 +0200 |
commit | 8789391f036ffded3ac919d62ee652e7cae15c5c (patch) | |
tree | cbb130ca8206e1b66057a5652a775d16e3aa3c36 /src/number.js | |
parent | 7b8e6f1d5afe2472cd1245f9bb73b6d59238e541 (diff) | |
download | svg.js-8789391f036ffded3ac919d62ee652e7cae15c5c.tar.gz svg.js-8789391f036ffded3ac919d62ee652e7cae15c5c.zip |
Fixed instantiation issue in SVG.Number
Diffstat (limited to 'src/number.js')
-rwxr-xr-x | src/number.js | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/src/number.js b/src/number.js index 22915b9..ace511c 100755 --- a/src/number.js +++ b/src/number.js @@ -1,31 +1,31 @@ // Module for unit convertions SVG.Number = SVG.invent({ // Initialize - create: function(value) { - // Initialize defaults + create: function(value, unit) { + // initialize defaults this.value = 0 - this.unit = '' + this.unit = unit || '' - // Parse value + // parse value if (typeof value === 'number') { - // Ensure a valid numeric value + // ensure a valid numeric value this.value = isNaN(value) ? 0 : !isFinite(value) ? (value < 0 ? -3.4e+38 : +3.4e+38) : value } else if (typeof value === 'string') { - var match = value.match(SVG.regex.unit) + unit = value.match(SVG.regex.unit) - if (match) { - // Make value numeric - this.value = parseFloat(match[1]) + if (unit) { + // make value numeric + this.value = parseFloat(unit[1]) - // Normalize - if (match[2] == '%') + // normalize + if (unit[2] == '%') this.value /= 100 - else if (match[2] == 's') + else if (unit[2] == 's') this.value *= 1000 - // Store unit - this.unit = match[2] + // store unit + this.unit = unit[2] } } else { @@ -54,9 +54,7 @@ SVG.Number = SVG.invent({ } // Add number , plus: function(number) { - this.value = this + new SVG.Number(number) - - return this + return new SVG.Number(this + new SVG.Number(number), this.unit) } // Subtract number , minus: function(number) { @@ -64,15 +62,11 @@ SVG.Number = SVG.invent({ } // Multiply number , times: function(number) { - this.value = this * new SVG.Number(number) - - return this + return new SVG.Number(this * new SVG.Number(number), this.unit) } // Divide number , divide: function(number) { - this.value = this / new SVG.Number(number) - - return this + return new SVG.Number(this / new SVG.Number(number), this.unit) } // Convert to different unit , to: function(unit) { |