diff options
author | wout <wout@impinc.co.uk> | 2014-07-25 17:47:03 +0200 |
---|---|---|
committer | wout <wout@impinc.co.uk> | 2014-07-25 17:47:03 +0200 |
commit | 9842187d84e71e736ada6a2e9444bab592945600 (patch) | |
tree | 3b2e67ca99fd881d0d6a8e2ab6358c4ef077a74a /src/number.js | |
parent | 94077ff77815f622f6ad0daebf26160ec33e0271 (diff) | |
download | svg.js-9842187d84e71e736ada6a2e9444bab592945600.tar.gz svg.js-9842187d84e71e736ada6a2e9444bab592945600.zip |
Various small fixes
Diffstat (limited to 'src/number.js')
-rwxr-xr-x | src/number.js | 170 |
1 files changed, 86 insertions, 84 deletions
diff --git a/src/number.js b/src/number.js index 92ff796..22915b9 100755 --- a/src/number.js +++ b/src/number.js @@ -1,101 +1,103 @@ // Module for unit convertions -SVG.Number = function(value) { +SVG.Number = SVG.invent({ + // Initialize + create: function(value) { + // Initialize defaults + this.value = 0 + this.unit = '' - /* initialize defaults */ - this.value = 0 - this.unit = '' + // Parse value + if (typeof value === 'number') { + // Ensure a valid numeric value + this.value = isNaN(value) ? 0 : !isFinite(value) ? (value < 0 ? -3.4e+38 : +3.4e+38) : value - /* parse value */ - if (typeof value === 'number') { - /* 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) - } else if (typeof value === 'string') { - var match = value.match(SVG.regex.unit) + if (match) { + // Make value numeric + this.value = parseFloat(match[1]) + + // Normalize + if (match[2] == '%') + this.value /= 100 + else if (match[2] == 's') + this.value *= 1000 + + // Store unit + this.unit = match[2] + } - if (match) { - /* make value numeric */ - this.value = parseFloat(match[1]) - - /* normalize percent value */ - if (match[2] == '%') - this.value /= 100 - else if (match[2] == 's') - this.value *= 1000 - - /* store unit */ - this.unit = match[2] + } else { + if (value instanceof SVG.Number) { + this.value = value.value + this.unit = value.unit + } } - } else { - if (value instanceof SVG.Number) { - this.value = value.value - this.unit = value.unit - } } + // Add methods +, extend: { + // Stringalize + toString: function() { + return ( + this.unit == '%' ? + ~~(this.value * 1e8) / 1e6: + this.unit == 's' ? + this.value / 1e3 : + this.value + ) + this.unit + } + , // Convert to primitive + valueOf: function() { + return this.value + } + // Add number + , plus: function(number) { + this.value = this + new SVG.Number(number) -} - -SVG.extend(SVG.Number, { - // Stringalize - toString: function() { - return ( - this.unit == '%' ? - ~~(this.value * 1e8) / 1e6: - this.unit == 's' ? - this.value / 1e3 : - this.value - ) + this.unit - } -, // Convert to primitive - valueOf: function() { - return this.value - } - // Add number -, plus: function(number) { - this.value = this + new SVG.Number(number) + return this + } + // Subtract number + , minus: function(number) { + return this.plus(-new SVG.Number(number)) + } + // Multiply number + , times: function(number) { + this.value = this * new SVG.Number(number) - return this - } - // Subtract number -, minus: function(number) { - return this.plus(-new SVG.Number(number)) - } - // Multiply number -, times: function(number) { - this.value = this * new SVG.Number(number) + return this + } + // Divide number + , divide: function(number) { + this.value = this / new SVG.Number(number) - return this - } - // Divide number -, divide: function(number) { - this.value = this / new SVG.Number(number) + return this + } + // Convert to different unit + , to: function(unit) { + if (typeof unit === 'string') + this.unit = unit - return this - } - // Convert to different unit -, to: function(unit) { - if (typeof unit === 'string') - this.unit = unit + return this + } + // Make number morphable + , morph: function(number) { + this.destination = new SVG.Number(number) - return this - } - // Make number morphable -, morph: function(number) { - this.destination = new SVG.Number(number) + return this + } + // Get morphed number at given position + , at: function(pos) { + // Make sure a destination is defined + if (!this.destination) return this - return this - } - // Get morphed number at given position -, at: function(pos) { - /* make sure a destination is defined */ - if (!this.destination) return this + // Generate new morphed number + return new SVG.Number(this.destination) + .minus(this) + .times(pos) + .plus(this) + } - /* generate new morphed number */ - return new SVG.Number(this.destination) - .minus(this) - .times(pos) - .plus(this) } - })
\ No newline at end of file |