summaryrefslogtreecommitdiffstats
path: root/src/sugar.js
blob: 10a871664cddf69d04ca3dd2bf49babd43d1332a (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Add shape-specific functions
SVG.extend(SVG.Shape, {
  
  // set fill color and opacity
  fill: function(f) {
    if (f.color != null)
      this.attr('fill', f.color);

    if (f.opacity != null)
      this.attr('fill-opacity', f.opacity);

    return this;
  },

  // set stroke color and opacity
  stroke: function(s) {
    if (s.color)
      this.attr('stroke', s.color);
    
    var a = ('width opacity linecap linejoin miterlimit dasharray dashoffset').split(' ');
    
    for (var i = a.length - 1; i >= 0; i--)
      if (s[a[i]] != null)
        this.attr('stroke-' + a[i], s[a[i]]);
    
    return this;
  }
  
});

// Add element-specific functions
SVG.extend(SVG.Element, {
  
  // rotation
  rotate: function(d, x, y) {
    var b = this.bbox();
    
    return this.transform({
      rotation: d || 0,
      cx:       x == null ? b.cx : x,
      cy:       y == null ? b.cx : y
    });
  },
  
  // skew
  skew: function(x, y) {
    return this.transform({
      skewX: x || 0,
      skewY: y || 0
    });
  }
  
});

// Add group-specific functions
SVG.extend(SVG.G, {
  
  // move using translate
  move: function(x, y) {
    return this.transform({ x: x, y: y });
  }
  
});

// Add text-specific functions
SVG.extend(SVG.Text, {
  
  // set font 
  font: function(o) {
    var k, a = {};
    
    for (k in o)
      k == 'leading' ?
        a[k] = o[k] :
      k == 'anchor' ?
        a['text-anchor'] = o[k] :
      this._s.indexOf(k) > -1 ?
        a['font-'+ k] = o[k] :
        void 0;
    
    return this.attr(a).text(this.content);
  }
  
});