summaryrefslogtreecommitdiffstats
path: root/src/line.js
blob: 0712f1eb77eca1d89a485038e940532bddd97e5f (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
SVG.Line = function() {
  this.constructor.call(this, SVG.create('line'))
}

// Inherit from SVG.Shape
SVG.Line.prototype = new SVG.Shape()

// Add required methods
SVG.extend(SVG.Line, {
  // Move line
  move: function(x, y) {
    var bbox = this.bbox()
    
    return this.attr({
      x1: this.attr('x1') - bbox.x + x
    , y1: this.attr('y1') - bbox.y + y
    , x2: this.attr('x2') - bbox.x + x
    , y2: this.attr('y2') - bbox.y + y
    })
  }
  // Move element by its center
, center: function(x, y) {
    var bbox = this.bbox()
    
    return this.move(x - bbox.width / 2, y - bbox.height / 2)
  }
  // Set line size by width and height
, size: function(width, height) {
    var bbox = this.bbox()
    
    return this
      .attr(this.attr('x1') < this.attr('x2') ? 'x2' : 'x1', bbox.x + width)
      .attr(this.attr('y1') < this.attr('y2') ? 'y2' : 'y1', bbox.y + height)
  }
})

// Extend all container modules
SVG.extend(SVG.Container, {
  line: function(x1, y1, x2, y2) {
    return this.put(new SVG.Line().attr({
      x1: x1
    , y1: y1
    , x2: x2
    , y2: y2
    }))
  }
})