summaryrefslogtreecommitdiffstats
path: root/src/viewbox.js
blob: 71e0a0424577f9bd2afdf919e71fde666febbf0b (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
SVG.ViewBox = function(element) {
  var x, y, width, height
    , box  = element.bbox()
    , view = (element.attr('viewBox') || '').match(/[\d\.]+/g)
  
  /* clone attributes */
  this.x      = box.x
  this.y      = box.y
  this.width  = element.node.offsetWidth
  this.height = element.node.offsetHeight
  this.zoom   = 1
  
  if (view) {
    /* get width and height from viewbox */
    x      = parseFloat(view[0])
    y      = parseFloat(view[1])
    width  = parseFloat(view[2]) - x
    height = parseFloat(view[3]) - y
    
    /* calculate zoom accoring to viewbox */
    this.zoom = ((this.width / this.height) > (width / height)) ?
      this.height / height :
      this.width  / width

    /* calculate real pixel dimensions on parent SVG.Doc element */
    if (element instanceof SVG.Doc) {
      this.x      = x
      this.y      = y
      this.width  = width
      this.height = height
    }
  }
}