summaryrefslogtreecommitdiffstats
path: root/src/sugar.js
blob: 222a8b889846447de4407d2c3d4f8973f7e364ca (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// define list of available attributes for stroke and fill
var _strokeAttr = ['width', 'opacity', 'linecap', 'linejoin', 'miterlimit', 'dasharray', 'dashoffset'],
    _fillAttr   = ['opacity', 'rule'];


// Add shape-specific functions
SVG.extend(SVG.Shape, {
  
  // set fill color and opacity
  fill: function(f) {
    var i;
    
    // set fill color if not null
    if (f.color != null)
      this.attr('fill', f.color);
    
    // set all attributes from _fillAttr list with prependes 'fill-' if not null
    for (i = _fillAttr.length - 1; i >= 0; i--)
      if (f[_fillAttr[i]] != null)
        this.attr('fill-' + _fillAttr[i], f[_fillAttr[i]]);
    
    return this;
  },

  // set stroke color and opacity
  stroke: function(s) {
    var i;
    
    // set stroke color if not null
    if (s.color)
      this.attr('stroke', s.color);
    
    // set all attributes from _strokeAttr list with prependes 'stroke-' if not null
    for (i = _strokeAttr.length - 1; i >= 0; i--)
      if (s[_strokeAttr[i]] != null)
        this.attr('stroke-' + _strokeAttr[i], s[_strokeAttr[i]]);
    
    return this;
  }
  
});


// Add element-specific functions
SVG.extend(SVG.Element, {
  
  // rotation
  rotate: function(d) {
    return this.transform({
      rotation: d || 0
    });
  },
  
  // 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] :
      _styleAttr.indexOf(k) > -1 ?
        a['font-'+ k] = o[k] :
        void 0;
    
    return this.attr(a).text(this.content);
  }
  
});

// add methods to SVG.FX
if (SVG.FX) {
  // add sugar for fill and stroke
  ['fill', 'stroke'].forEach(function(m) {
    SVG.FX.prototype[m] = function(o) {
      var a, k;

      for (k in o) {
        a = k == 'color' ? m : m + '-' + k;
        this.attrs[a] = {
          from: this.target.attrs[a],
          to:   o[k]
        };
      };

      return this;
    };
  });
  
  SVG.extend(SVG.FX, {

    // rotation
    rotate: function(d) {
      return this.transform({
        rotation: d || 0
      });
    },

    // skew
    skew: function(x, y) {
      return this.transform({
        skewX: x || 0,
        skewY: y || 0
      });
    }

  });
}