summaryrefslogtreecommitdiffstats
path: root/src/sugar.js
blob: 7301fda177d302d541e11bede7f6b35f9d27ac78 (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
// Define list of available attributes for stroke and fill
var _strokeAttr = ['width', 'opacity', 'linecap', 'linejoin', 'miterlimit', 'dasharray', 'dashoffset'],
    _fillAttr   = ['opacity', 'rule'];

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

SVG.extend(SVG.Element, {
  // Rotation
  rotate: function(angle) {
    return this.transform({
      rotation: angle || 0
    });
  },
  // Skew
  skew: function(x, y) {
    return this.transform({
      skewX: x || 0,
      skewY: y || 0
    });
  }
  
});

SVG.extend(SVG.G, {
  // Move using translate
  move: function(x, y) {
    return this.transform({
      x: x,
      y: y
    });
  }
  
});

SVG.extend(SVG.Text, {
  // Set font 
  font: function(o) {
    var key, attr = {};
    
    for (key in o)
      key == 'leading' ?
        attr[key] = o[key] :
      key == 'anchor' ?
        attr['text-anchor'] = o[key] :
      _styleAttr.indexOf(key) > -1 ?
        attr['font-'+ key] = o[key] :
        void 0;
    
    return this.attr(attr).text(this.content);
  }
  
});


if (SVG.FX) {
  /* Add sugar for fill and stroke */
  ['fill', 'stroke'].forEach(function(method) {
    SVG.FX.prototype[method] = function(o) {
      var attr, key;

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

      return this;
    };
  });
  
  SVG.extend(SVG.FX, {
    // Rotation
    rotate: function(angle) {
      return this.transform({
        rotation: angle || 0
      });
    },

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

  });
}