aboutsummaryrefslogtreecommitdiffstats
path: root/src/rbox.js
blob: 95cc1e271b61d409fbb6c7cf3cedf1ee477156b8 (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
// Get the rectangular box of a given element
SVG.RBox = function(element) {
  var e, zoom
    , box = {}

  /* initialize zero box */
  this.x      = 0
  this.y      = 0
  this.width  = 0
  this.height = 0
  
  if (element) {
    e = element.doc().parent
    zoom = element.doc().viewbox().zoom
    
    /* actual, native bounding box */
    box = element.node.getBoundingClientRect()
    
    /* get screen offset */
    this.x = box.left
    this.y = box.top
    
    /* subtract parent offset */
    this.x -= e.offsetLeft
    this.y -= e.offsetTop
    
    while (e = e.offsetParent) {
      this.x -= e.offsetLeft
      this.y -= e.offsetTop
    }
    
    /* calculate cumulative zoom from svg documents */
    e = element
    while (e = e.parent) {
      if (e.type == 'svg' && e.viewbox) {
        zoom *= e.viewbox().zoom
        this.x -= e.x() || 0
        this.y -= e.y() || 0
      }
    }
  }
  
  /* recalculate viewbox distortion */
  this.x /= zoom
  this.y /= zoom
  this.width  = box.width  /= zoom
  this.height = box.height /= zoom
  
  /* add the center */
  this.cx = this.x + this.width  / 2
  this.cy = this.y + this.height / 2
  
}

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

    /* 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 the center */
    b.cx = b.x + b.width / 2
    b.cy = b.y + b.height / 2

    return b
  }

})