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


// Prepend correct color prefix
var _colorPrefix = function(type, attr) {
  return attr == 'color' ? type : type + '-' + attr;
};

/* Add sugar for fill and stroke */
['fill', 'stroke'].forEach(function(method) {
  
  // Set fill color and opacity
  SVG.Shape.prototype[method] = function(o) {
    var index;
    
    if (typeof o == 'string')
      this.attr(method, o);
    
    else
      /* set all attributes from _fillAttr and _strokeAttr list */
      for (index = SVG['_' + method].length - 1; index >= 0; index--)
        if (o[SVG['_' + method][index]] != null)
          this.attr(_colorPrefix(method, SVG['_' + method][index]), o[SVG['_' + method][index]]);
    
    return this;
  };
  
});

[SVG.Element, SVG.FX].forEach(function(module) {
  if (module) {
    SVG.extend(module, {
      // Rotation
      rotate: function(angle) {
        return this.transform({
          rotation: angle || 0
        });
      },
      // Skew
      skew: function(x, y) {
        return this.transform({
          skewX: x || 0,
          skewY: y || 0
        });
      },
      // Scale
      scale: function(x, y) {
        return this.transform({
          scaleX: x || 0,
          scaleY: y || 0
        });
      }

    });
  }
});


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

  });
}

if (SVG.Text) {
  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);
    }

  });
}