root / trunk / web / dojo / dojox / gfx / shape.js
History | View | Annotate | Download (9.45 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.gfx.shape"]){ |
||
9 | dojo._hasResource["dojox.gfx.shape"]=true; |
||
10 | dojo.provide("dojox.gfx.shape");
|
||
11 | dojo.require("dojox.gfx._base");
|
||
12 | dojo.declare("dojox.gfx.Shape",null,{constructor:function(){ |
||
13 | this.rawNode=null; |
||
14 | this.shape=null; |
||
15 | this.matrix=null; |
||
16 | this.fillStyle=null; |
||
17 | this.strokeStyle=null; |
||
18 | this.bbox=null; |
||
19 | this.parent=null; |
||
20 | this.parentMatrix=null; |
||
21 | },getNode:function(){ |
||
22 | return this.rawNode; |
||
23 | },getShape:function(){ |
||
24 | return this.shape; |
||
25 | },getTransform:function(){ |
||
26 | return this.matrix; |
||
27 | },getFill:function(){ |
||
28 | return this.fillStyle; |
||
29 | },getStroke:function(){ |
||
30 | return this.strokeStyle; |
||
31 | },getParent:function(){ |
||
32 | return this.parent; |
||
33 | },getBoundingBox:function(){ |
||
34 | return this.bbox; |
||
35 | },getTransformedBoundingBox:function(){ |
||
36 | var b=this.getBoundingBox(); |
||
37 | if(!b){
|
||
38 | return null; |
||
39 | } |
||
40 | var m=this._getRealMatrix(); |
||
41 | gm=dojox.gfx.matrix; |
||
42 | return [gm.multiplyPoint(m,b.x,b.y),gm.multiplyPoint(m,b.x+b.width,b.y),gm.multiplyPoint(m,b.x+b.width,b.y+b.height),gm.multiplyPoint(m,b.x,b.y+b.height)];
|
||
43 | },getEventSource:function(){ |
||
44 | return this.rawNode; |
||
45 | },setShape:function(_1){ |
||
46 | this.shape=dojox.gfx.makeParameters(this.shape,_1); |
||
47 | this.bbox=null; |
||
48 | return this; |
||
49 | },setFill:function(_2){ |
||
50 | if(!_2){
|
||
51 | this.fillStyle=null; |
||
52 | return this; |
||
53 | } |
||
54 | var f=null; |
||
55 | if(typeof (_2)=="object"&&"type" in _2){ |
||
56 | switch(_2.type){
|
||
57 | case "linear": |
||
58 | f=dojox.gfx.makeParameters(dojox.gfx.defaultLinearGradient,_2); |
||
59 | break;
|
||
60 | case "radial": |
||
61 | f=dojox.gfx.makeParameters(dojox.gfx.defaultRadialGradient,_2); |
||
62 | break;
|
||
63 | case "pattern": |
||
64 | f=dojox.gfx.makeParameters(dojox.gfx.defaultPattern,_2); |
||
65 | break;
|
||
66 | } |
||
67 | }else{
|
||
68 | f=dojox.gfx.normalizeColor(_2); |
||
69 | } |
||
70 | this.fillStyle=f;
|
||
71 | return this; |
||
72 | },setStroke:function(_3){ |
||
73 | if(!_3){
|
||
74 | this.strokeStyle=null; |
||
75 | return this; |
||
76 | } |
||
77 | if(typeof _3=="string"||dojo.isArray(_3)||_3 instanceof dojo.Color){ |
||
78 | _3={color:_3};
|
||
79 | } |
||
80 | var s=this.strokeStyle=dojox.gfx.makeParameters(dojox.gfx.defaultStroke,_3); |
||
81 | s.color=dojox.gfx.normalizeColor(s.color); |
||
82 | return this; |
||
83 | },setTransform:function(_4){ |
||
84 | this.matrix=dojox.gfx.matrix.clone(_4?dojox.gfx.matrix.normalize(_4):dojox.gfx.matrix.identity);
|
||
85 | return this._applyTransform(); |
||
86 | },_applyTransform:function(){ |
||
87 | return this; |
||
88 | },moveToFront:function(){ |
||
89 | var p=this.getParent(); |
||
90 | if(p){
|
||
91 | p._moveChildToFront(this);
|
||
92 | this._moveToFront();
|
||
93 | } |
||
94 | return this; |
||
95 | },moveToBack:function(){ |
||
96 | var p=this.getParent(); |
||
97 | if(p){
|
||
98 | p._moveChildToBack(this);
|
||
99 | this._moveToBack();
|
||
100 | } |
||
101 | return this; |
||
102 | },_moveToFront:function(){ |
||
103 | },_moveToBack:function(){ |
||
104 | },applyRightTransform:function(_5){ |
||
105 | return _5?this.setTransform([this.matrix,_5]):this; |
||
106 | },applyLeftTransform:function(_6){ |
||
107 | return _6?this.setTransform([_6,this.matrix]):this; |
||
108 | },applyTransform:function(_7){ |
||
109 | return _7?this.setTransform([this.matrix,_7]):this; |
||
110 | },removeShape:function(_8){ |
||
111 | if(this.parent){ |
||
112 | this.parent.remove(this,_8); |
||
113 | } |
||
114 | return this; |
||
115 | },_setParent:function(_9,_a){ |
||
116 | this.parent=_9;
|
||
117 | return this._updateParentMatrix(_a); |
||
118 | },_updateParentMatrix:function(_b){ |
||
119 | this.parentMatrix=_b?dojox.gfx.matrix.clone(_b):null; |
||
120 | return this._applyTransform(); |
||
121 | },_getRealMatrix:function(){ |
||
122 | var m=this.matrix; |
||
123 | var p=this.parent; |
||
124 | while(p){
|
||
125 | if(p.matrix){
|
||
126 | m=dojox.gfx.matrix.multiply(p.matrix,m); |
||
127 | } |
||
128 | p=p.parent; |
||
129 | } |
||
130 | return m;
|
||
131 | }}); |
||
132 | dojox.gfx.shape._eventsProcessing={connect:function(_c,_d,_e){ |
||
133 | return arguments.length>2?dojo.connect(this.getEventSource(),_c,_d,_e):dojo.connect(this.getEventSource(),_c,_d); |
||
134 | },disconnect:function(_f){ |
||
135 | dojo.disconnect(_f); |
||
136 | }}; |
||
137 | dojo.extend(dojox.gfx.Shape,dojox.gfx.shape._eventsProcessing); |
||
138 | dojox.gfx.shape.Container={_init:function(){ |
||
139 | this.children=[];
|
||
140 | },openBatch:function(){ |
||
141 | },closeBatch:function(){ |
||
142 | },add:function(_10){ |
||
143 | var _11=_10.getParent();
|
||
144 | if(_11){
|
||
145 | _11.remove(_10,true);
|
||
146 | } |
||
147 | this.children.push(_10);
|
||
148 | return _10._setParent(this,this._getRealMatrix()); |
||
149 | },remove:function(_12,_13){ |
||
150 | for(var i=0;i<this.children.length;++i){ |
||
151 | if(this.children[i]==_12){ |
||
152 | if(_13){
|
||
153 | }else{
|
||
154 | _12.parent=null;
|
||
155 | _12.parentMatrix=null;
|
||
156 | } |
||
157 | this.children.splice(i,1); |
||
158 | break;
|
||
159 | } |
||
160 | } |
||
161 | return this; |
||
162 | },clear:function(){ |
||
163 | this.children=[];
|
||
164 | return this; |
||
165 | },_moveChildToFront:function(_14){ |
||
166 | for(var i=0;i<this.children.length;++i){ |
||
167 | if(this.children[i]==_14){ |
||
168 | this.children.splice(i,1); |
||
169 | this.children.push(_14);
|
||
170 | break;
|
||
171 | } |
||
172 | } |
||
173 | return this; |
||
174 | },_moveChildToBack:function(_15){ |
||
175 | for(var i=0;i<this.children.length;++i){ |
||
176 | if(this.children[i]==_15){ |
||
177 | this.children.splice(i,1); |
||
178 | this.children.unshift(_15);
|
||
179 | break;
|
||
180 | } |
||
181 | } |
||
182 | return this; |
||
183 | }}; |
||
184 | dojo.declare("dojox.gfx.shape.Surface",null,{constructor:function(){ |
||
185 | this.rawNode=null; |
||
186 | this._parent=null; |
||
187 | this._nodes=[];
|
||
188 | this._events=[];
|
||
189 | },destroy:function(){ |
||
190 | dojo.forEach(this._nodes,dojo.destroy);
|
||
191 | this._nodes=[];
|
||
192 | dojo.forEach(this._events,dojo.disconnect);
|
||
193 | this._events=[];
|
||
194 | this.rawNode=null; |
||
195 | if(dojo.isIE){
|
||
196 | while(this._parent.lastChild){ |
||
197 | dojo.destroy(this._parent.lastChild);
|
||
198 | } |
||
199 | }else{
|
||
200 | this._parent.innerHTML=""; |
||
201 | } |
||
202 | this._parent=null; |
||
203 | },getEventSource:function(){ |
||
204 | return this.rawNode; |
||
205 | },_getRealMatrix:function(){ |
||
206 | return null; |
||
207 | },isLoaded:true,onLoad:function(_16){ |
||
208 | },whenLoaded:function(_17,_18){ |
||
209 | var f=dojo.hitch(_17,_18);
|
||
210 | if(this.isLoaded){ |
||
211 | f(this);
|
||
212 | }else{
|
||
213 | var h=dojo.connect(this,"onLoad",function(_19){ |
||
214 | dojo.disconnect(h); |
||
215 | f(_19); |
||
216 | }); |
||
217 | } |
||
218 | }}); |
||
219 | dojo.extend(dojox.gfx.shape.Surface,dojox.gfx.shape._eventsProcessing); |
||
220 | dojo.declare("dojox.gfx.Point",null,{}); |
||
221 | dojo.declare("dojox.gfx.Rectangle",null,{}); |
||
222 | dojo.declare("dojox.gfx.shape.Rect",dojox.gfx.Shape,{constructor:function(_1a){ |
||
223 | this.shape=dojox.gfx.getDefault("Rect"); |
||
224 | this.rawNode=_1a;
|
||
225 | },getBoundingBox:function(){ |
||
226 | return this.shape; |
||
227 | }}); |
||
228 | dojo.declare("dojox.gfx.shape.Ellipse",dojox.gfx.Shape,{constructor:function(_1b){ |
||
229 | this.shape=dojox.gfx.getDefault("Ellipse"); |
||
230 | this.rawNode=_1b;
|
||
231 | },getBoundingBox:function(){ |
||
232 | if(!this.bbox){ |
||
233 | var _1c=this.shape; |
||
234 | this.bbox={x:_1c.cx-_1c.rx,y:_1c.cy-_1c.ry,width:2*_1c.rx,height:2*_1c.ry}; |
||
235 | } |
||
236 | return this.bbox; |
||
237 | }}); |
||
238 | dojo.declare("dojox.gfx.shape.Circle",dojox.gfx.Shape,{constructor:function(_1d){ |
||
239 | this.shape=dojox.gfx.getDefault("Circle"); |
||
240 | this.rawNode=_1d;
|
||
241 | },getBoundingBox:function(){ |
||
242 | if(!this.bbox){ |
||
243 | var _1e=this.shape; |
||
244 | this.bbox={x:_1e.cx-_1e.r,y:_1e.cy-_1e.r,width:2*_1e.r,height:2*_1e.r}; |
||
245 | } |
||
246 | return this.bbox; |
||
247 | }}); |
||
248 | dojo.declare("dojox.gfx.shape.Line",dojox.gfx.Shape,{constructor:function(_1f){ |
||
249 | this.shape=dojox.gfx.getDefault("Line"); |
||
250 | this.rawNode=_1f;
|
||
251 | },getBoundingBox:function(){ |
||
252 | if(!this.bbox){ |
||
253 | var _20=this.shape; |
||
254 | this.bbox={x:Math.min(_20.x1,_20.x2),y:Math.min(_20.y1,_20.y2),width:Math.abs(_20.x2-_20.x1),height:Math.abs(_20.y2-_20.y1)}; |
||
255 | } |
||
256 | return this.bbox; |
||
257 | }}); |
||
258 | dojo.declare("dojox.gfx.shape.Polyline",dojox.gfx.Shape,{constructor:function(_21){ |
||
259 | this.shape=dojox.gfx.getDefault("Polyline"); |
||
260 | this.rawNode=_21;
|
||
261 | },setShape:function(_22,_23){ |
||
262 | if(_22&&_22 instanceof Array){ |
||
263 | dojox.gfx.Shape.prototype.setShape.call(this,{points:_22}); |
||
264 | if(_23&&this.shape.points.length){ |
||
265 | this.shape.points.push(this.shape.points[0]); |
||
266 | } |
||
267 | }else{
|
||
268 | dojox.gfx.Shape.prototype.setShape.call(this,_22);
|
||
269 | } |
||
270 | return this; |
||
271 | },_normalizePoints:function(){ |
||
272 | var p=this.shape.points,l=p&&p.length; |
||
273 | if(l&&typeof p[0]=="number"){ |
||
274 | var _24=[];
|
||
275 | for(var i=0;i<l;i+=2){ |
||
276 | _24.push({x:p[i],y:p[i+1]}); |
||
277 | } |
||
278 | this.shape.points=_24;
|
||
279 | } |
||
280 | },getBoundingBox:function(){ |
||
281 | if(!this.bbox&&this.shape.points.length){ |
||
282 | var p=this.shape.points; |
||
283 | var l=p.length;
|
||
284 | var t=p[0]; |
||
285 | var _25={l:t.x,t:t.y,r:t.x,b:t.y}; |
||
286 | for(var i=1;i<l;++i){ |
||
287 | t=p[i]; |
||
288 | if(_25.l>t.x){
|
||
289 | _25.l=t.x; |
||
290 | } |
||
291 | if(_25.r<t.x){
|
||
292 | _25.r=t.x; |
||
293 | } |
||
294 | if(_25.t>t.y){
|
||
295 | _25.t=t.y; |
||
296 | } |
||
297 | if(_25.b<t.y){
|
||
298 | _25.b=t.y; |
||
299 | } |
||
300 | } |
||
301 | this.bbox={x:_25.l,y:_25.t,width:_25.r-_25.l,height:_25.b-_25.t}; |
||
302 | } |
||
303 | return this.bbox; |
||
304 | }}); |
||
305 | dojo.declare("dojox.gfx.shape.Image",dojox.gfx.Shape,{constructor:function(_26){ |
||
306 | this.shape=dojox.gfx.getDefault("Image"); |
||
307 | this.rawNode=_26;
|
||
308 | },getBoundingBox:function(){ |
||
309 | return this.shape; |
||
310 | },setStroke:function(){ |
||
311 | return this; |
||
312 | },setFill:function(){ |
||
313 | return this; |
||
314 | }}); |
||
315 | dojo.declare("dojox.gfx.shape.Text",dojox.gfx.Shape,{constructor:function(_27){ |
||
316 | this.fontStyle=null; |
||
317 | this.shape=dojox.gfx.getDefault("Text"); |
||
318 | this.rawNode=_27;
|
||
319 | },getFont:function(){ |
||
320 | return this.fontStyle; |
||
321 | },setFont:function(_28){ |
||
322 | this.fontStyle=typeof _28=="string"?dojox.gfx.splitFontString(_28):dojox.gfx.makeParameters(dojox.gfx.defaultFont,_28); |
||
323 | this._setFont();
|
||
324 | return this; |
||
325 | }}); |
||
326 | dojox.gfx.shape.Creator={createShape:function(_29){ |
||
327 | var gfx=dojox.gfx;
|
||
328 | switch(_29.type){
|
||
329 | case gfx.defaultPath.type:
|
||
330 | return this.createPath(_29); |
||
331 | case gfx.defaultRect.type:
|
||
332 | return this.createRect(_29); |
||
333 | case gfx.defaultCircle.type:
|
||
334 | return this.createCircle(_29); |
||
335 | case gfx.defaultEllipse.type:
|
||
336 | return this.createEllipse(_29); |
||
337 | case gfx.defaultLine.type:
|
||
338 | return this.createLine(_29); |
||
339 | case gfx.defaultPolyline.type:
|
||
340 | return this.createPolyline(_29); |
||
341 | case gfx.defaultImage.type:
|
||
342 | return this.createImage(_29); |
||
343 | case gfx.defaultText.type:
|
||
344 | return this.createText(_29); |
||
345 | case gfx.defaultTextPath.type:
|
||
346 | return this.createTextPath(_29); |
||
347 | } |
||
348 | return null; |
||
349 | },createGroup:function(){ |
||
350 | return this.createObject(dojox.gfx.Group); |
||
351 | },createRect:function(_2a){ |
||
352 | return this.createObject(dojox.gfx.Rect,_2a); |
||
353 | },createEllipse:function(_2b){ |
||
354 | return this.createObject(dojox.gfx.Ellipse,_2b); |
||
355 | },createCircle:function(_2c){ |
||
356 | return this.createObject(dojox.gfx.Circle,_2c); |
||
357 | },createLine:function(_2d){ |
||
358 | return this.createObject(dojox.gfx.Line,_2d); |
||
359 | },createPolyline:function(_2e){ |
||
360 | return this.createObject(dojox.gfx.Polyline,_2e); |
||
361 | },createImage:function(_2f){ |
||
362 | return this.createObject(dojox.gfx.Image,_2f); |
||
363 | },createText:function(_30){ |
||
364 | return this.createObject(dojox.gfx.Text,_30); |
||
365 | },createPath:function(_31){ |
||
366 | return this.createObject(dojox.gfx.Path,_31); |
||
367 | },createTextPath:function(_32){ |
||
368 | return this.createObject(dojox.gfx.TextPath,{}).setText(_32); |
||
369 | },createObject:function(_33,_34){ |
||
370 | return null; |
||
371 | }}; |
||
372 | } |