summaryrefslogtreecommitdiffstats
path: root/src/bbox.js
blob: c68bec58f71e1875a93b7f252c05da3cb5b3ab2b (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
 
SVG.BBox = function(element) {
  var box

  /* initialize zero box */
  this.x      = 0
  this.y      = 0
  this.width  = 0
  this.height = 0
  
  /* get values if element is given */
  if (element) {
    try {
      /* actual, native bounding box */
      box = element.node.getBBox()
    } catch(e) {
      /* fallback for some browsers */
      box = {
        x:      element.node.clientLeft
      , y:      element.node.clientTop
      , width:  element.node.clientWidth
      , height: element.node.clientHeight
      }
    }
    
    /* include translations on x an y */
    this.x = box.x + element.trans.x
    this.y = box.y + element.trans.y
    
    /* plain width and height */
    this.width  = box.width  * element.trans.scaleX
    this.height = box.height * element.trans.scaleY
  }

  /* add center, right and bottom */
  boxProperties(this)
  
}

//
SVG.extend(SVG.BBox, {
  // merge bounding box with another, return a new instance
  merge: function(box) {
    var b = new SVG.BBox()

    /* merge box */
    b.x      = Math.min(this.x, box.x)
    b.y      = Math.min(this.y, box.y)
    b.width  = Math.max(this.x + this.width,  box.x + box.width)  - b.x
    b.height = Math.max(this.y + this.height, box.y + box.height) - b.y

    /* add center, right and bottom */
    boxProperties(b)

    return b
  }

})