Debian Codegraph @

dojox/gfx/svg_mask.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare",
3 "dojo/_base/lang",
4 "./_base",
5 "./shape",
6 "./svg"
7 ], function(declare, lang, gfx, gfxShape, svg){
8
9 /*=====
10 return {
11 // summary:
12 // An svg-specific module that adds SVG mask support to the gfx api.
13 // You may require this module when your application specifically targets the SVG renderer.
14 }
15 =====*/
16
17 lang.extend(svg.Shape, {
18 mask: null,
19 setMask: function(/*dojox.gfx.svg.Mask*/mask){
20 // summary:
21 // Sets a mask object (SVG)
22 // mask:
23 // The mask object
24
25 var rawNode = this.rawNode;
26 if(mask){
27 rawNode.setAttribute("mask", "url(#"+mask.shape.id+")");
28 this.mask = mask;
29 }else{
30 rawNode.removeAttribute("mask");
31 this.mask = null;
32 }
33
34 return this;
35 },
36 getMask: function(){
37 // summary:
38 // Returns the current mask object or null
39 return this.mask;
40 }
41 });
42
43 var Mask = svg.Mask = declare("dojox.gfx.svg.Mask", svg.Shape, {
44 // summary:
45 // An SVG mask object
46 // description:
47 // This object represents an SVG mask. Much like `dojox/gfx.Group`,
48 // a Mask's geometry is defined by its children.
49
50 constructor: function(){
51 gfxShape.Container._init.call(this);
52 this.shape = Mask.defaultMask;
53 },
54
55 setRawNode: function(rawNode){
56 this.rawNode = rawNode;
57 },
58
59 setShape: function(shape){
60 if(!shape.id){
61 shape = lang.mixin({ id: gfx._base._getUniqueId() }, shape);
62 }
63 this.inherited(arguments, [shape]);
64 }
65 });
66 Mask.nodeType = 'mask';
67 Mask.defaultMask = {
68 // summary:
69 // Defines the default Mask prototype.
70
71 // id: String
72 // The mask identifier. If none is provided, a generated id will be used.
73 id: null,
74
75 // x: Number
76 // The x coordinate of the top-left corner of the mask
77 x: 0,
78
79 // y: Number
80 // The y coordinate of the top-left corner of the mask
81 y: 0,
82
83 // width: Number
84 // The width of the mask. Defaults to 1 which is 100% of the bounding
85 // box width of the object applying the mask.
86 width: 1,
87
88 // height: Number
89 // The height of the mask. Defaults to 1 which is 100% of the bounding
90 // box height of the object applying the mask.
91 height: 1,
92
93 // maskUnits: String
94 // The coordinate system of the mask's `x`, `y`, `width`, and `height` properties.
95 // The default is "objectBoundingBox" where coordinates are fractions of the bounding box
96 // of the shape referencing the mask.
97 maskUnits: "objectBoundingBox",
98
99 // maskContentUnits: String
100 // The coordinate system for the mask's children. The default is "userSpaceOnUse"
101 // (i.e., the coordinate system of the shape referencing the mask).
102 maskContentUnits: "userSpaceOnUse"
103 };
104
105 lang.extend(Mask, svg.Container);
106 lang.extend(Mask, gfxShape.Creator);
107 lang.extend(Mask, svg.Creator);
108
109 var Surface = svg.Surface,
110 surfaceAdd = Surface.prototype.add,
111 surfaceRemove = Surface.prototype.remove;
112 lang.extend(Surface, {
113 createMask: function(mask){
114 // summary:
115 // Creates a mask object
116 // mask: Object
117 // A mask object (see dojox/gfx.svg.Mask.defaultMask)
118 //
119 // example:
120 // Define a mask where content coordinates are fractions of the bounding box
121 // of the object using the mask:
122 // | var mask = surface.createMask({ maskContentUnits: "objectBoundingBox" });
123 // | mask.createRect({ width: 1, height: 1 });
124 // | mask.setFill({
125 // | type: 'linear',
126 // | x2: 1,
127 // | y2: 0,
128 // | colors: [
129 // | { offset: 0, color: '#111' },
130 // | { offset: 1, color: '#ddd' }
131 // | ]
132 // | });
133 //
134 // example:
135 // A mask with dimensions in user coordinates of element referring to mask
136 // | var mask = {
137 // | maskUnits: 'userSpaceOnUse'
138 // | };
139 return this.createObject(Mask, mask); // dojox.gfx.svg.Mask
140 },
141 add: function(shape){
142 if(shape instanceof Mask){
143 this.defNode.appendChild(shape.rawNode);
144 shape.parent = this;
145 }else{
146 surfaceAdd.apply(this, arguments);
147 }
148 return this;
149 },
150 remove: function(shape, silently){
151 if(shape instanceof Mask && this.defNode == shape.rawNode.parentNode){
152 this.defNode.removeChild(shape.rawNode);
153 shape.parent = null;
154 }else{
155 surfaceRemove.apply(this, arguments);
156 }
157 return this;
158 }
159 });
160 });