aboutsummaryrefslogtreecommitdiffstats
path: root/src/element.js
blob: 3dcf785b3ec9266ca95b045cf3e712f8e10e2a16 (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
SVG.Element = function Element(svgElement) {
  this.svgElement = svgElement;
  this.attributes = {};
};

//-D // inherit from SVG.Object
//-D SVG.Element.prototype = new SVG.Object();

// move element to given x and y values
SVG.Element.prototype.move = function(x, y) {
  this.setAttribute('x', x);
  this.setAttribute('y', y);
  
  return this;
};

// set element opacity
SVG.Element.prototype.opacity = function(o) {
  return this.setAttribute('opacity', Math.max(0, Math.min(1, o)));
};

// set element size to given width and height
SVG.Element.prototype.size = function(w, h) {
  this.setAttribute('width', w);
  this.setAttribute('height', h);
  
  return this;
};

// clip element using another element
SVG.Element.prototype.clip = function(block) {
  var p = this.parentSVG().defs().clipPath();
  block(p);
  
  return this.clipTo(p);
};

// distribute clipping path to svg element
SVG.Element.prototype.clipTo = function(p) {
  return this.setAttribute('clip-path', 'url(#' + p.id + ')');
};

// remove element
SVG.Element.prototype.destroy = function() {
  return this.parent != null ? this.parent.remove(this) : void 0;
};

// get parent document
SVG.Element.prototype.parentDoc = function() {
  return this._findParent(SVG.Document);
};

// get parent svg wrapper
SVG.Element.prototype.parentSVG = function() {
  return this.parentDoc();
};

// set svg element attribute
SVG.Element.prototype.setAttribute = function(a, v, ns) {
  this.attributes[a] = v;
  
  if (ns != null)
    this.svgElement.setAttributeNS(ns, a, v);
  else
    this.svgElement.setAttribute(a, v);
  
  return this;
};

// set svg element attribute
SVG.Element.prototype.attr = function(v) {
  if (typeof v == 'object')
    for (var k in v)
      this.setAttribute(k, v[k]);
  else if (arguments.length == 2)
    this.setAttribute(arguments[0], arguments[1]);
  
  return this;
};

// get bounding box
SVG.Element.prototype.getBBox = function() {
  return this.svgElement.getBBox();
};

// private: find svg parent
SVG.Element.prototype._findParent = function(pt) {
  var e = this;
  
  while (e != null && !(e instanceof pt))
    e = e.parent;
  
  return e;
};