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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
|
//>>built
define("dojox/gfx/shape", ["./_base", "dojo/_base/lang", "dojo/_base/declare", "dojo/_base/window", "dojo/_base/sniff",
"dojo/_base/connect", "dojo/_base/array", "dojo/dom-construct", "dojo/_base/Color", "./matrix"],
function(g, lang, declare, win, has, events, arr, domConstruct, Color, matrixLib){
/*=====
dojox.gfx.shape = {
// summary:
// This module contains the core graphics Shape API.
// Different graphics renderer implementation modules (svg, canvas, vml, silverlight, etc.) extend this
// basic api to provide renderer-specific implementations for each shape.
};
=====*/
var shape = g.shape = {};
// a set of ids (keys=type)
var _ids = {};
// a simple set impl to map shape<->id
var registry = {};
shape.register = function(/*dojox.gfx.shape.Shape*/shape){
// summary:
// Register the specified shape into the graphics registry.
// shape: dojox.gfx.shape.Shape
// The shape to register.
// returns:
// The unique id associated with this shape.
// the id pattern : type+number (ex: Rect0,Rect1,etc)
var t = shape.declaredClass.split('.').pop();
var i = t in _ids ? ++_ids[t] : ((_ids[t] = 0));
var uid = t+i;
registry[uid] = shape;
return uid;
};
shape.byId = function(/*String*/id){
// summary:
// Returns the shape that matches the specified id.
// id: String
// The unique identifier for this Shape.
return registry[id]; //dojox.gfx.shape.Shape
};
shape.dispose = function(/*dojox.gfx.shape.Shape*/shape){
// summary:
// Removes the specified shape from the registry.
// shape: dojox.gfx.shape.Shape
// The shape to unregister.
delete registry[shape.getUID()];
};
declare("dojox.gfx.shape.Shape", null, {
// summary: a Shape object, which knows how to apply
// graphical attributes and transformations
constructor: function(){
// rawNode: Node
// underlying graphics-renderer-specific implementation object (if applicable)
this.rawNode = null;
// shape: Object: an abstract shape object
// (see dojox.gfx.defaultPath,
// dojox.gfx.defaultPolyline,
// dojox.gfx.defaultRect,
// dojox.gfx.defaultEllipse,
// dojox.gfx.defaultCircle,
// dojox.gfx.defaultLine,
// or dojox.gfx.defaultImage)
this.shape = null;
// matrix: dojox.gfx.Matrix2D
// a transformation matrix
this.matrix = null;
// fillStyle: Object
// a fill object
// (see dojox.gfx.defaultLinearGradient,
// dojox.gfx.defaultRadialGradient,
// dojox.gfx.defaultPattern,
// or dojo.Color)
this.fillStyle = null;
// strokeStyle: Object
// a stroke object
// (see dojox.gfx.defaultStroke)
this.strokeStyle = null;
// bbox: dojox.gfx.Rectangle
// a bounding box of this shape
// (see dojox.gfx.defaultRect)
this.bbox = null;
// virtual group structure
// parent: Object
// a parent or null
// (see dojox.gfx.Surface,
// dojox.gfx.shape.VirtualGroup,
// or dojox.gfx.Group)
this.parent = null;
// parentMatrix: dojox.gfx.Matrix2D
// a transformation matrix inherited from the parent
this.parentMatrix = null;
var uid = shape.register(this);
this.getUID = function(){
return uid;
}
},
// trivial getters
getNode: function(){
// summary: Different graphics rendering subsystems implement shapes in different ways. This
// method provides access to the underlying graphics subsystem object. Clients calling this
// method and using the return value must be careful not to try sharing or using the underlying node
// in a general way across renderer implementation.
// Returns the underlying graphics Node, or null if no underlying graphics node is used by this shape.
return this.rawNode; // Node
},
getShape: function(){
// summary: returns the current Shape object or null
// (see dojox.gfx.defaultPath,
// dojox.gfx.defaultPolyline,
// dojox.gfx.defaultRect,
// dojox.gfx.defaultEllipse,
// dojox.gfx.defaultCircle,
// dojox.gfx.defaultLine,
// or dojox.gfx.defaultImage)
return this.shape; // Object
},
getTransform: function(){
// summary: Returns the current transformation matrix applied to this Shape or null
return this.matrix; // dojox.gfx.Matrix2D
},
getFill: function(){
// summary: Returns the current fill object or null
// (see dojox.gfx.defaultLinearGradient,
// dojox.gfx.defaultRadialGradient,
// dojox.gfx.defaultPattern,
// or dojo.Color)
return this.fillStyle; // Object
},
getStroke: function(){
// summary: Returns the current stroke object or null
// (see dojox.gfx.defaultStroke)
return this.strokeStyle; // Object
},
getParent: function(){
// summary: Returns the parent Shape, Group or VirtualGroup or null if this Shape is unparented.
// (see dojox.gfx.Surface,
// dojox.gfx.shape.VirtualGroup,
// or dojox.gfx.Group)
return this.parent; // Object
},
getBoundingBox: function(){
// summary: Returns the bounding box Rectanagle for this shape or null if a BoundingBox cannot be
// calculated for the shape on the current renderer or for shapes with no geometric area (points).
// A bounding box is a rectangular geometric region
// defining the X and Y extent of the shape.
// (see dojox.gfx.defaultRect)
return this.bbox; // dojox.gfx.Rectangle
},
getTransformedBoundingBox: function(){
// summary: returns an array of four points or null
// four points represent four corners of the untransformed bounding box
var b = this.getBoundingBox();
if(!b){
return null; // null
}
var m = this._getRealMatrix(),
gm = matrixLib;
return [ // Array
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)
];
},
getEventSource: function(){
// summary: returns a Node, which is used as
// a source of events for this shape
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
return this.rawNode; // Node
},
// empty settings
setShape: function(shape){
// summary: sets a shape object
// (the default implementation simply ignores it)
// shape: Object
// a shape object
// (see dojox.gfx.defaultPath,
// dojox.gfx.defaultPolyline,
// dojox.gfx.defaultRect,
// dojox.gfx.defaultEllipse,
// dojox.gfx.defaultCircle,
// dojox.gfx.defaultLine,
// or dojox.gfx.defaultImage)
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
this.shape = g.makeParameters(this.shape, shape);
this.bbox = null;
return this; // self
},
setFill: function(fill){
// summary: sets a fill object
// (the default implementation simply ignores it)
// fill: Object
// a fill object
// (see dojox.gfx.defaultLinearGradient,
// dojox.gfx.defaultRadialGradient,
// dojox.gfx.defaultPattern,
// or dojo.Color)
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
if(!fill){
// don't fill
this.fillStyle = null;
return this; // self
}
var f = null;
if(typeof(fill) == "object" && "type" in fill){
// gradient or pattern
switch(fill.type){
case "linear":
f = g.makeParameters(g.defaultLinearGradient, fill);
break;
case "radial":
f = g.makeParameters(g.defaultRadialGradient, fill);
break;
case "pattern":
f = g.makeParameters(g.defaultPattern, fill);
break;
}
}else{
// color object
f = g.normalizeColor(fill);
}
this.fillStyle = f;
return this; // self
},
setStroke: function(stroke){
// summary: sets a stroke object
// (the default implementation simply ignores it)
// stroke: Object
// a stroke object
// (see dojox.gfx.defaultStroke)
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
if(!stroke){
// don't stroke
this.strokeStyle = null;
return this; // self
}
// normalize the stroke
if(typeof stroke == "string" || lang.isArray(stroke) || stroke instanceof Color){
stroke = {color: stroke};
}
var s = this.strokeStyle = g.makeParameters(g.defaultStroke, stroke);
s.color = g.normalizeColor(s.color);
return this; // self
},
setTransform: function(matrix){
// summary: sets a transformation matrix
// matrix: dojox.gfx.Matrix2D
// a matrix or a matrix-like object
// (see an argument of dojox.gfx.Matrix2D
// constructor for a list of acceptable arguments)
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
this.matrix = matrixLib.clone(matrix ? matrixLib.normalize(matrix) : matrixLib.identity);
return this._applyTransform(); // self
},
_applyTransform: function(){
// summary: physically sets a matrix
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
return this; // self
},
// z-index
moveToFront: function(){
// summary: moves a shape to front of its parent's list of shapes
var p = this.getParent();
if(p){
p._moveChildToFront(this);
this._moveToFront(); // execute renderer-specific action
}
return this; // self
},
moveToBack: function(){
// summary: moves a shape to back of its parent's list of shapes
var p = this.getParent();
if(p){
p._moveChildToBack(this);
this._moveToBack(); // execute renderer-specific action
}
return this;
},
_moveToFront: function(){
// summary: renderer-specific hook, see dojox.gfx.shape.Shape.moveToFront()
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
},
_moveToBack: function(){
// summary: renderer-specific hook, see dojox.gfx.shape.Shape.moveToFront()
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
},
// apply left & right transformation
applyRightTransform: function(matrix){
// summary: multiplies the existing matrix with an argument on right side
// (this.matrix * matrix)
// matrix: dojox.gfx.Matrix2D
// a matrix or a matrix-like object
// (see an argument of dojox.gfx.Matrix2D
// constructor for a list of acceptable arguments)
return matrix ? this.setTransform([this.matrix, matrix]) : this; // self
},
applyLeftTransform: function(matrix){
// summary: multiplies the existing matrix with an argument on left side
// (matrix * this.matrix)
// matrix: dojox.gfx.Matrix2D
// a matrix or a matrix-like object
// (see an argument of dojox.gfx.Matrix2D
// constructor for a list of acceptable arguments)
return matrix ? this.setTransform([matrix, this.matrix]) : this; // self
},
applyTransform: function(matrix){
// summary: a shortcut for dojox.gfx.Shape.applyRightTransform
// matrix: dojox.gfx.Matrix2D
// a matrix or a matrix-like object
// (see an argument of dojox.gfx.Matrix2D
// constructor for a list of acceptable arguments)
return matrix ? this.setTransform([this.matrix, matrix]) : this; // self
},
// virtual group methods
removeShape: function(silently){
// summary: removes the shape from its parent's list of shapes
// silently: Boolean
// if true, do not redraw a picture yet
if(this.parent){
this.parent.remove(this, silently);
}
return this; // self
},
_setParent: function(parent, matrix){
// summary: sets a parent
// parent: Object
// a parent or null
// (see dojox.gfx.Surface,
// dojox.gfx.shape.VirtualGroup,
// or dojox.gfx.Group)
// matrix: dojox.gfx.Matrix2D
// a 2D matrix or a matrix-like object
this.parent = parent;
return this._updateParentMatrix(matrix); // self
},
_updateParentMatrix: function(matrix){
// summary: updates the parent matrix with new matrix
// matrix: dojox.gfx.Matrix2D
// a 2D matrix or a matrix-like object
this.parentMatrix = matrix ? matrixLib.clone(matrix) : null;
return this._applyTransform(); // self
},
_getRealMatrix: function(){
// summary: returns the cumulative ('real') transformation matrix
// by combining the shape's matrix with its parent's matrix
var m = this.matrix;
var p = this.parent;
while(p){
if(p.matrix){
m = matrixLib.multiply(p.matrix, m);
}
p = p.parent;
}
return m; // dojox.gfx.Matrix2D
}
});
shape._eventsProcessing = {
connect: function(name, object, method){
// summary: connects a handler to an event on this shape
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
// redirect to fixCallback to normalize events and add the gfxTarget to the event. The latter
// is done by dojox.gfx.fixTarget which is defined by each renderer
return events.connect(this.getEventSource(), name, shape.fixCallback(this, g.fixTarget, object, method));
},
disconnect: function(token){
// summary: connects a handler by token from an event on this shape
// COULD BE RE-IMPLEMENTED BY THE RENDERER!
events.disconnect(token);
}
};
shape.fixCallback = function(gfxElement, fixFunction, scope, method){
// summary:
// Wraps the callback to allow for tests and event normalization
// before it gets invoked. This is where 'fixTarget' is invoked.
// gfxElement: Object
// The GFX object that triggers the action (ex.:
// dojox.gfx.Surface and dojox.gfx.Shape). A new event property
// 'gfxTarget' is added to the event to reference this object.
// for easy manipulation of GFX objects by the event handlers.
// fixFunction: Function
// The function that implements the logic to set the 'gfxTarget'
// property to the event. It should be 'dojox.gfx.fixTarget' for
// most of the cases
// scope: Object
// Optional. The scope to be used when invoking 'method'. If
// omitted, a global scope is used.
// method: Function|String
// The original callback to be invoked.
if(!method){
method = scope;
scope = null;
}
if(lang.isString(method)){
scope = scope || win.global;
if(!scope[method]){ throw(['dojox.gfx.shape.fixCallback: scope["', method, '"] is null (scope="', scope, '")'].join('')); }
return function(e){
return fixFunction(e,gfxElement) ? scope[method].apply(scope, arguments || []) : undefined; }; // Function
}
return !scope
? function(e){
return fixFunction(e,gfxElement) ? method.apply(scope, arguments) : undefined; }
: function(e){
return fixFunction(e,gfxElement) ? method.apply(scope, arguments || []) : undefined; }; // Function
};
lang.extend(shape.Shape, shape._eventsProcessing);
shape.Container = {
// summary: a container of shapes, which can be used
// as a foundation for renderer-specific groups, or as a way
// to logically group shapes (e.g, to propagate matricies)
_init: function() {
// children: Array: a list of children
this.children = [];
},
// group management
openBatch: function() {
// summary: starts a new batch, subsequent new child shapes will be held in
// the batch instead of appending to the container directly
},
closeBatch: function() {
// summary: submits the current batch, append all pending child shapes to DOM
},
add: function(shape){
// summary: adds a shape to the list
// shape: dojox.gfx.Shape
// the shape to add to the list
var oldParent = shape.getParent();
if(oldParent){
oldParent.remove(shape, true);
}
this.children.push(shape);
return shape._setParent(this, this._getRealMatrix()); // self
},
remove: function(shape, silently){
// summary: removes a shape from the list
// shape: dojox.gfx.shape.Shape
// the shape to remove
// silently: Boolean
// if true, do not redraw a picture yet
for(var i = 0; i < this.children.length; ++i){
if(this.children[i] == shape){
if(silently){
// skip for now
}else{
shape.parent = null;
shape.parentMatrix = null;
}
this.children.splice(i, 1);
break;
}
}
return this; // self
},
clear: function(){
// summary: removes all shapes from a group/surface
var shape;
for(var i = 0; i < this.children.length;++i){
shape = this.children[i];
shape.parent = null;
shape.parentMatrix = null;
}
this.children = [];
return this; // self
},
// moving child nodes
_moveChildToFront: function(shape){
// summary: moves a shape to front of the list of shapes
// shape: dojox.gfx.shape.Shape
// one of the child shapes to move to the front
for(var i = 0; i < this.children.length; ++i){
if(this.children[i] == shape){
this.children.splice(i, 1);
this.children.push(shape);
break;
}
}
return this; // self
},
_moveChildToBack: function(shape){
// summary: moves a shape to back of the list of shapes
// shape: dojox.gfx.shape.Shape
// one of the child shapes to move to the front
for(var i = 0; i < this.children.length; ++i){
if(this.children[i] == shape){
this.children.splice(i, 1);
this.children.unshift(shape);
break;
}
}
return this; // self
}
};
declare("dojox.gfx.shape.Surface", null, {
// summary: a surface object to be used for drawings
constructor: function(){
// underlying node
this.rawNode = null;
// the parent node
this._parent = null;
// the list of DOM nodes to be deleted in the case of destruction
this._nodes = [];
// the list of events to be detached in the case of destruction
this._events = [];
},
destroy: function(){
// summary: destroy all relevant external resources and release all
// external references to make this object garbage-collectible
arr.forEach(this._nodes, domConstruct.destroy);
this._nodes = [];
arr.forEach(this._events, events.disconnect);
this._events = [];
this.rawNode = null; // recycle it in _nodes, if it needs to be recycled
if(has("ie")){
while(this._parent.lastChild){
domConstruct.destroy(this._parent.lastChild);
}
}else{
this._parent.innerHTML = "";
}
this._parent = null;
},
getEventSource: function(){
// summary: returns a node, which can be used to attach event listeners
return this.rawNode; // Node
},
_getRealMatrix: function(){
// summary: always returns the identity matrix
return null; // dojox.gfx.Matrix2D
},
isLoaded: true,
onLoad: function(/*dojox.gfx.Surface*/ surface){
// summary: local event, fired once when the surface is created
// asynchronously, used only when isLoaded is false, required
// only for Silverlight.
},
whenLoaded: function(/*Object|Null*/ context, /*Function|String*/ method){
var f = lang.hitch(context, method);
if(this.isLoaded){
f(this);
}else{
var h = events.connect(this, "onLoad", function(surface){
events.disconnect(h);
f(surface);
});
}
}
});
lang.extend(shape.Surface, shape._eventsProcessing);
declare("dojox.gfx.Point", null, {
// summary: a hypothetical 2D point to be used for drawings - {x, y}
// description: This object is defined for documentation purposes.
// You should use the naked object instead: {x: 1, y: 2}.
});
declare("dojox.gfx.Rectangle", null, {
// summary: a hypothetical rectangle - {x, y, width, height}
// description: This object is defined for documentation purposes.
// You should use the naked object instead: {x: 1, y: 2, width: 100, height: 200}.
});
declare("dojox.gfx.shape.Rect", shape.Shape, {
// summary: a generic rectangle
constructor: function(rawNode){
// rawNode: Node
// The underlying graphics system object (typically a DOM Node)
this.shape = g.getDefault("Rect");
this.rawNode = rawNode;
},
getBoundingBox: function(){
// summary: returns the bounding box (its shape in this case)
return this.shape; // dojox.gfx.Rectangle
}
});
declare("dojox.gfx.shape.Ellipse", shape.Shape, {
// summary: a generic ellipse
constructor: function(rawNode){
// rawNode: Node
// a DOM Node
this.shape = g.getDefault("Ellipse");
this.rawNode = rawNode;
},
getBoundingBox: function(){
// summary: returns the bounding box
if(!this.bbox){
var shape = this.shape;
this.bbox = {x: shape.cx - shape.rx, y: shape.cy - shape.ry,
width: 2 * shape.rx, height: 2 * shape.ry};
}
return this.bbox; // dojox.gfx.Rectangle
}
});
declare("dojox.gfx.shape.Circle", shape.Shape, {
// summary: a generic circle
// (this is a helper object, which is defined for convenience)
constructor: function(rawNode){
// rawNode: Node
// a DOM Node
this.shape = g.getDefault("Circle");
this.rawNode = rawNode;
},
getBoundingBox: function(){
// summary: returns the bounding box
if(!this.bbox){
var shape = this.shape;
this.bbox = {x: shape.cx - shape.r, y: shape.cy - shape.r,
width: 2 * shape.r, height: 2 * shape.r};
}
return this.bbox; // dojox.gfx.Rectangle
}
});
declare("dojox.gfx.shape.Line", shape.Shape, {
// summary: a generic line
// (this is a helper object, which is defined for convenience)
constructor: function(rawNode){
// rawNode: Node
// a DOM Node
this.shape = g.getDefault("Line");
this.rawNode = rawNode;
},
getBoundingBox: function(){
// summary: returns the bounding box
if(!this.bbox){
var shape = this.shape;
this.bbox = {
x: Math.min(shape.x1, shape.x2),
y: Math.min(shape.y1, shape.y2),
width: Math.abs(shape.x2 - shape.x1),
height: Math.abs(shape.y2 - shape.y1)
};
}
return this.bbox; // dojox.gfx.Rectangle
}
});
declare("dojox.gfx.shape.Polyline", shape.Shape, {
// summary: a generic polyline/polygon
// (this is a helper object, which is defined for convenience)
constructor: function(rawNode){
// rawNode: Node
// a DOM Node
this.shape = g.getDefault("Polyline");
this.rawNode = rawNode;
},
setShape: function(points, closed){
// summary: sets a polyline/polygon shape object
// points: Object
// a polyline/polygon shape object
// closed: Boolean
// close the polyline to make a polygon
if(points && points instanceof Array){
// points: Array: an array of points
this.inherited(arguments, [{points: points}]);
if(closed && this.shape.points.length){
this.shape.points.push(this.shape.points[0]);
}
}else{
this.inherited(arguments, [points]);
}
return this; // self
},
_normalizePoints: function(){
// summary: normalize points to array of {x:number, y:number}
var p = this.shape.points, l = p && p.length;
if(l && typeof p[0] == "number"){
var points = [];
for(var i = 0; i < l; i += 2){
points.push({x: p[i], y: p[i + 1]});
}
this.shape.points = points;
}
},
getBoundingBox: function(){
// summary: returns the bounding box
if(!this.bbox && this.shape.points.length){
var p = this.shape.points;
var l = p.length;
var t = p[0];
var bbox = {l: t.x, t: t.y, r: t.x, b: t.y};
for(var i = 1; i < l; ++i){
t = p[i];
if(bbox.l > t.x) bbox.l = t.x;
if(bbox.r < t.x) bbox.r = t.x;
if(bbox.t > t.y) bbox.t = t.y;
if(bbox.b < t.y) bbox.b = t.y;
}
this.bbox = {
x: bbox.l,
y: bbox.t,
width: bbox.r - bbox.l,
height: bbox.b - bbox.t
};
}
return this.bbox; // dojox.gfx.Rectangle
}
});
declare("dojox.gfx.shape.Image", shape.Shape, {
// summary: a generic image
// (this is a helper object, which is defined for convenience)
constructor: function(rawNode){
// rawNode: Node
// a DOM Node
this.shape = g.getDefault("Image");
this.rawNode = rawNode;
},
getBoundingBox: function(){
// summary: returns the bounding box (its shape in this case)
return this.shape; // dojox.gfx.Rectangle
},
setStroke: function(){
// summary: ignore setting a stroke style
return this; // self
},
setFill: function(){
// summary: ignore setting a fill style
return this; // self
}
});
declare("dojox.gfx.shape.Text", shape.Shape, {
// summary: a generic text
constructor: function(rawNode){
// rawNode: Node
// a DOM Node
this.fontStyle = null;
this.shape = g.getDefault("Text");
this.rawNode = rawNode;
},
getFont: function(){
// summary: returns the current font object or null
return this.fontStyle; // Object
},
setFont: function(newFont){
// summary: sets a font for text
// newFont: Object
// a font object (see dojox.gfx.defaultFont) or a font string
this.fontStyle = typeof newFont == "string" ? g.splitFontString(newFont) :
g.makeParameters(g.defaultFont, newFont);
this._setFont();
return this; // self
}
});
shape.Creator = {
// summary: shape creators
createShape: function(shape){
// summary: creates a shape object based on its type; it is meant to be used
// by group-like objects
// shape: Object
// a shape descriptor object
switch(shape.type){
case g.defaultPath.type: return this.createPath(shape);
case g.defaultRect.type: return this.createRect(shape);
case g.defaultCircle.type: return this.createCircle(shape);
case g.defaultEllipse.type: return this.createEllipse(shape);
case g.defaultLine.type: return this.createLine(shape);
case g.defaultPolyline.type: return this.createPolyline(shape);
case g.defaultImage.type: return this.createImage(shape);
case g.defaultText.type: return this.createText(shape);
case g.defaultTextPath.type: return this.createTextPath(shape);
}
return null;
},
createGroup: function(){
// summary: creates a group shape
return this.createObject(g.Group); // dojox.gfx.Group
},
createRect: function(rect){
// summary: creates a rectangle shape
// rect: Object
// a path object (see dojox.gfx.defaultRect)
return this.createObject(g.Rect, rect); // dojox.gfx.Rect
},
createEllipse: function(ellipse){
// summary: creates an ellipse shape
// ellipse: Object
// an ellipse object (see dojox.gfx.defaultEllipse)
return this.createObject(g.Ellipse, ellipse); // dojox.gfx.Ellipse
},
createCircle: function(circle){
// summary: creates a circle shape
// circle: Object
// a circle object (see dojox.gfx.defaultCircle)
return this.createObject(g.Circle, circle); // dojox.gfx.Circle
},
createLine: function(line){
// summary: creates a line shape
// line: Object
// a line object (see dojox.gfx.defaultLine)
return this.createObject(g.Line, line); // dojox.gfx.Line
},
createPolyline: function(points){
// summary: creates a polyline/polygon shape
// points: Object
// a points object (see dojox.gfx.defaultPolyline)
// or an Array of points
return this.createObject(g.Polyline, points); // dojox.gfx.Polyline
},
createImage: function(image){
// summary: creates a image shape
// image: Object
// an image object (see dojox.gfx.defaultImage)
return this.createObject(g.Image, image); // dojox.gfx.Image
},
createText: function(text){
// summary: creates a text shape
// text: Object
// a text object (see dojox.gfx.defaultText)
return this.createObject(g.Text, text); // dojox.gfx.Text
},
createPath: function(path){
// summary: creates a path shape
// path: Object
// a path object (see dojox.gfx.defaultPath)
return this.createObject(g.Path, path); // dojox.gfx.Path
},
createTextPath: function(text){
// summary: creates a text shape
// text: Object
// a textpath object (see dojox.gfx.defaultTextPath)
return this.createObject(g.TextPath, {}).setText(text); // dojox.gfx.TextPath
},
createObject: function(shapeType, rawShape){
// summary: creates an instance of the passed shapeType class
// SHOULD BE RE-IMPLEMENTED BY THE RENDERER!
// shapeType: Function
// a class constructor to create an instance of
// rawShape: Object
// properties to be passed in to the classes 'setShape' method
return null; // dojox.gfx.Shape
}
};
return shape;
});
|