summaryrefslogtreecommitdiffstats
path: root/src/mask.js
blob: a9b457eb61bd4f31d983963794cd3abd526c0803 (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
SVG.Mask = function Mask() {
  this.constructor.call(this, SVG.create('mask'));
  
  // set unique id
  this.id = 'svgjs_' + (SVG.did++);
  this.attr('id', this.id);
};

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

// include the container object
SVG.extend(SVG.Mask, SVG.Container);

// add clipping functionality to element
SVG.extend(SVG.Element, {
  
  // mask element using another element
  mask: function(b) {
    var m = this.parent.defs().mask();
    b(m);

    return this.maskTo(m);
  },

  // distribute mask to svg element
  maskTo: function(m) {
    return this.attr('mask', 'url(#' + m.id + ')');
  }
  
});

// add def-specific functions
SVG.extend(SVG.Defs, {
  
  // create clippath
  mask: function() {
    return this.put(new SVG.Mask());
  }
  
});