summaryrefslogtreecommitdiffstats
path: root/src/number.js
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2013-06-23 14:59:46 +0100
committerwout <wout@impinc.co.uk>2013-06-23 14:59:46 +0100
commita72b8a7afb609b9a76c494d9123d78e5c40962bf (patch)
treefdaacf9d56a3cc9e935ab1a5dcb4ad0458f929e4 /src/number.js
parent5e52bd4f9f309b20bc848d5d45e5e620b17c1ad7 (diff)
downloadsvg.js-a72b8a7afb609b9a76c494d9123d78e5c40962bf.tar.gz
svg.js-a72b8a7afb609b9a76c494d9123d78e5c40962bf.zip
Added SVG.Number, reworked gradient system
Diffstat (limited to 'src/number.js')
-rw-r--r--src/number.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/number.js b/src/number.js
new file mode 100644
index 0000000..28e260e
--- /dev/null
+++ b/src/number.js
@@ -0,0 +1,75 @@
+// Module for unit convertions
+SVG.Number = function(value) {
+
+ /* initialize defaults */
+ this.value = 0
+ this.unit = ''
+
+ /* parse value */
+ switch(typeof value) {
+ case 'number':
+ this.value = value
+ break
+ case 'string':
+ var match = value.match(SVG.regex.unit)
+
+ /* make valu numeric */
+ this.value = parseFloat(match[1])
+
+ /* normalize percent value */
+ if (match[2] == '%')
+ this.value /= 100
+
+ /* store unit */
+ this.unit = match[2]
+
+ break
+ default:
+ if (value instanceof SVG.Number) {
+ this.value = value.value
+ this.unit = value.unit
+ }
+ break
+ }
+}
+
+SVG.extend(SVG.Number, {
+ // Stringalize
+ toString: function() {
+ return (this.unit == '%' ? ~~(this.value * 100) : this.value) + this.unit
+ }
+, // Convert to primitive
+ valueOf: function() {
+ return this.value
+ }
+ // Convert to different unit
+, to: function(unit) {
+ if (typeof unit === 'string')
+ this.unit = unit
+
+ return this
+ }
+ // 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
+ }
+ // Divide number
+, divide: function(number) {
+ this.value = this / new SVG.Number(number)
+
+ return this
+ }
+
+}) \ No newline at end of file