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

    /* calculate real pixel dimensions on parent SVG.Doc element */
    if (element instanceof SVG.Doc) {
      this.x      = 0
      this.y      = 0
      this.width  = element.node.offsetWidth
      this.height = element.node.offsetHeight
    }
    
    /* calculate zoom accoring to viewbox */
    this.zoom = (this.width / this.height > width / height) ?
      this.height / height :
      this.width  / width
    
  } else {
    this.zoom = 1
  }
  
}