summaryrefslogtreecommitdiffstats
path: root/src/shape.js
blob: 8295f1c482d7bee125d04be624bfcbc352b011b1 (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
SVG.Shape = function Shape(element) {
  this.drag = new SVG.Draggable(this);
  this.constructor.call(this, element);
};

// inherit from SVG.Element
SVG.Shape.prototype = new SVG.Element();

// set fill color and opacity
SVG.Shape.prototype.fill = function(fill) {
  this._formatColor(fill);
  
  if (fill.color != null)
    this.setAttribute('fill', fill.color);
  
  if (fill.opacity != null)
    this.setAttribute('fill-opacity', fill.opacity);
  
  return this;
};

// set stroke color and opacity
SVG.Shape.prototype.stroke = function(stroke) {
  this._formatColor(stroke);
  
  if (stroke.color != null)
    this.setAttribute('stroke', stroke.color);
  
  if (stroke.width != null)
    this.setAttribute('stroke-width', stroke.width);
  
  if (stroke.opacity != null)
    this.setAttribute('stroke-opacity', stroke.opacity);
  
  if (this.attributes['fill-opacity'] == null)
    this.fill({ opacity: 0 });
  
  return this;
};

// ensure correct color string
SVG.Shape.prototype._formatColor = function(obj) {
  if (typeof obj.color === 'number') {
    obj.color = '' + obj.color.toString(16);
    while (obj.color.length < 6)
      obj.color = '0' + obj.color;
    
    return obj.color = '#' + obj.color;
  }
};