summaryrefslogtreecommitdiffstats
path: root/src/number.js
blob: 92ff796b705dcc443756df0adfbfbd4eccaf29e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Module for unit convertions
SVG.Number = function(value) {

  /* 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

  } else if (typeof value === 'string') {
    var match = value.match(SVG.regex.unit)

    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
    }
  }

}

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
  }
  // 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
  }
  // 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

    /* generate new morphed number */
    return new SVG.Number(this.destination)
        .minus(this)
        .times(pos)
        .plus(this)
  }

})