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
|
//>>built
define("dojox/gfx/canvas", ["./_base", "dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/_base/window", "dojo/dom-geometry",
"dojo/dom", "./_base", "./shape", "./path", "./arc", "./matrix", "./decompose"],
function(g, lang, arr, declare, win, domGeom, dom, gfxBase, gs, pathLib, ga, m, decompose ){
/*=====
dojox.gfx.canvas = {
// module:
// dojox/gfx/canvas
// summary:
// This the graphics rendering bridge for W3C Canvas compliant browsers.
// Since Canvas is an immediate mode graphics api, with no object graph or
// eventing capabilities, use of this module alone will only add in drawing support.
// The additional module, canvasWithEvents extends this module with additional support
// for handling events on Canvas. By default, the support for events is now included
// however, if only drawing capabilities are needed, canvas event module can be disabled
// using the dojoConfig option, canvasEvents:true|false.
// The id of the Canvas renderer is 'canvas'. This id can be used when switch Dojo's
// graphics context between renderer implementations. See dojox.gfx._base switchRenderer
// API.
};
g = dojox.gfx;
gs = dojox.gfx.shape;
pathLib.Path = dojox.gfx.path.Path;
pathLib.TextPath = dojox.gfx.path.TextPath;
canvas = dojox.gfx.canvas;
canvas.Shape = dojox.gfx.canvas.Shape;
gs.Shape = dojox.gfx.shape.Shape;
gs.Rect = dojox.gfx.shape.Rect;
gs.Ellipse = dojox.gfx.shape.Ellipse;
gs.Circle = dojox.gfx.shape.Circle;
gs.Line = dojox.gfx.shape.Line;
gs.PolyLine = dojox.gfx.shape.PolyLine;
gs.Image = dojox.gfx.shape.Image;
gs.Text = dojox.gfx.shape.Text;
gs.Surface = dojox.gfx.shape.Surface;
=====*/
var canvas = g.canvas = {};
var pattrnbuffer = null,
mp = m.multiplyPoint,
pi = Math.PI,
twoPI = 2 * pi,
halfPI = pi /2,
extend = lang.extend;
declare("dojox.gfx.canvas.Shape", gs.Shape, {
_render: function(/* Object */ ctx){
// summary: render the shape
ctx.save();
this._renderTransform(ctx);
this._renderShape(ctx);
this._renderFill(ctx, true);
this._renderStroke(ctx, true);
ctx.restore();
},
_renderTransform: function(/* Object */ ctx){
if("canvasTransform" in this){
var t = this.canvasTransform;
ctx.translate(t.dx, t.dy);
ctx.rotate(t.angle2);
ctx.scale(t.sx, t.sy);
ctx.rotate(t.angle1);
// The future implementation when vendors catch up with the spec:
// var t = this.matrix;
// ctx.transform(t.xx, t.yx, t.xy, t.yy, t.dx, t.dy);
}
},
_renderShape: function(/* Object */ ctx){
// nothing
},
_renderFill: function(/* Object */ ctx, /* Boolean */ apply){
if("canvasFill" in this){
var fs = this.fillStyle;
if("canvasFillImage" in this){
var w = fs.width, h = fs.height,
iw = this.canvasFillImage.width, ih = this.canvasFillImage.height,
// let's match the svg default behavior wrt. aspect ratio: xMidYMid meet
sx = w == iw ? 1 : w / iw,
sy = h == ih ? 1 : h / ih,
s = Math.min(sx,sy), //meet->math.min , slice->math.max
dx = (w - s * iw)/2,
dy = (h - s * ih)/2;
// the buffer used to scaled the image
pattrnbuffer.width = w; pattrnbuffer.height = h;
var copyctx = pattrnbuffer.getContext("2d");
copyctx.clearRect(0, 0, w, h);
copyctx.drawImage(this.canvasFillImage, 0, 0, iw, ih, dx, dy, s*iw, s*ih);
this.canvasFill = ctx.createPattern(pattrnbuffer, "repeat");
delete this.canvasFillImage;
}
ctx.fillStyle = this.canvasFill;
if(apply){
// offset the pattern
if (fs.type==="pattern" && (fs.x !== 0 || fs.y !== 0)) {
ctx.translate(fs.x,fs.y);
}
ctx.fill();
}
}else{
ctx.fillStyle = "rgba(0,0,0,0.0)";
}
},
_renderStroke: function(/* Object */ ctx, /* Boolean */ apply){
var s = this.strokeStyle;
if(s){
ctx.strokeStyle = s.color.toString();
ctx.lineWidth = s.width;
ctx.lineCap = s.cap;
if(typeof s.join == "number"){
ctx.lineJoin = "miter";
ctx.miterLimit = s.join;
}else{
ctx.lineJoin = s.join;
}
if(apply){ ctx.stroke(); }
}else if(!apply){
ctx.strokeStyle = "rgba(0,0,0,0.0)";
}
},
// events are not implemented
getEventSource: function(){ return null; },
connect: function(){},
disconnect: function(){}
});
var modifyMethod = function(shape, method, extra){
var old = shape.prototype[method];
shape.prototype[method] = extra ?
function(){
this.surface.makeDirty();
old.apply(this, arguments);
extra.call(this);
return this;
} :
function(){
this.surface.makeDirty();
return old.apply(this, arguments);
};
};
modifyMethod(canvas.Shape, "setTransform",
function(){
// prepare Canvas-specific structures
if(this.matrix){
this.canvasTransform = g.decompose(this.matrix);
}else{
delete this.canvasTransform;
}
});
modifyMethod(canvas.Shape, "setFill",
function(){
// prepare Canvas-specific structures
var fs = this.fillStyle, f;
if(fs){
if(typeof(fs) == "object" && "type" in fs){
var ctx = this.surface.rawNode.getContext("2d");
switch(fs.type){
case "linear":
case "radial":
f = fs.type == "linear" ?
ctx.createLinearGradient(fs.x1, fs.y1, fs.x2, fs.y2) :
ctx.createRadialGradient(fs.cx, fs.cy, 0, fs.cx, fs.cy, fs.r);
arr.forEach(fs.colors, function(step){
f.addColorStop(step.offset, g.normalizeColor(step.color).toString());
});
break;
case "pattern":
if (!pattrnbuffer) {
pattrnbuffer = document.createElement("canvas");
}
// no need to scale the image since the canvas.createPattern uses
// the original image data and not the scaled ones (see spec.)
// the scaling needs to be done at rendering time in a context buffer
var img =new Image();
this.surface.downloadImage(img, fs.src);
this.canvasFillImage = img;
}
}else{
// Set fill color using CSS RGBA func style
f = fs.toString();
}
this.canvasFill = f;
}else{
delete this.canvasFill;
}
});
modifyMethod(canvas.Shape, "setStroke");
modifyMethod(canvas.Shape, "setShape");
declare("dojox.gfx.canvas.Group", canvas.Shape, {
// summary: a group shape (Canvas), which can be used
// to logically group shapes (e.g, to propagate matricies)
constructor: function(){
gs.Container._init.call(this);
},
_render: function(/* Object */ ctx){
// summary: render the group
ctx.save();
this._renderTransform(ctx);
for(var i = 0; i < this.children.length; ++i){
this.children[i]._render(ctx);
}
ctx.restore();
}
});
declare("dojox.gfx.canvas.Rect", [canvas.Shape, gs.Rect], {
// summary: a rectangle shape (Canvas)
_renderShape: function(/* Object */ ctx){
var s = this.shape, r = Math.min(s.r, s.height / 2, s.width / 2),
xl = s.x, xr = xl + s.width, yt = s.y, yb = yt + s.height,
xl2 = xl + r, xr2 = xr - r, yt2 = yt + r, yb2 = yb - r;
ctx.beginPath();
ctx.moveTo(xl2, yt);
if(r){
ctx.arc(xr2, yt2, r, -halfPI, 0, false);
ctx.arc(xr2, yb2, r, 0, halfPI, false);
ctx.arc(xl2, yb2, r, halfPI, pi, false);
ctx.arc(xl2, yt2, r, pi, pi + halfPI, false);
}else{
ctx.lineTo(xr2, yt);
ctx.lineTo(xr, yb2);
ctx.lineTo(xl2, yb);
ctx.lineTo(xl, yt2);
}
ctx.closePath();
}
});
var bezierCircle = [];
(function(){
var u = ga.curvePI4;
bezierCircle.push(u.s, u.c1, u.c2, u.e);
for(var a = 45; a < 360; a += 45){
var r = m.rotateg(a);
bezierCircle.push(mp(r, u.c1), mp(r, u.c2), mp(r, u.e));
}
})();
declare("dojox.gfx.canvas.Ellipse", [canvas.Shape, gs.Ellipse], {
// summary: an ellipse shape (Canvas)
setShape: function(){
this.inherited(arguments);
// prepare Canvas-specific structures
var s = this.shape, t, c1, c2, r = [],
M = m.normalize([m.translate(s.cx, s.cy), m.scale(s.rx, s.ry)]);
t = mp(M, bezierCircle[0]);
r.push([t.x, t.y]);
for(var i = 1; i < bezierCircle.length; i += 3){
c1 = mp(M, bezierCircle[i]);
c2 = mp(M, bezierCircle[i + 1]);
t = mp(M, bezierCircle[i + 2]);
r.push([c1.x, c1.y, c2.x, c2.y, t.x, t.y]);
}
this.canvasEllipse = r;
return this;
},
_renderShape: function(/* Object */ ctx){
var r = this.canvasEllipse;
ctx.beginPath();
ctx.moveTo.apply(ctx, r[0]);
for(var i = 1; i < r.length; ++i){
ctx.bezierCurveTo.apply(ctx, r[i]);
}
ctx.closePath();
}
});
declare("dojox.gfx.canvas.Circle", [canvas.Shape, gs.Circle], {
// summary: a circle shape (Canvas)
_renderShape: function(/* Object */ ctx){
var s = this.shape;
ctx.beginPath();
ctx.arc(s.cx, s.cy, s.r, 0, twoPI, 1);
}
});
declare("dojox.gfx.canvas.Line", [canvas.Shape, gs.Line], {
// summary: a line shape (Canvas)
_renderShape: function(/* Object */ ctx){
var s = this.shape;
ctx.beginPath();
ctx.moveTo(s.x1, s.y1);
ctx.lineTo(s.x2, s.y2);
}
});
declare("dojox.gfx.canvas.Polyline", [canvas.Shape, gs.Polyline], {
// summary: a polyline/polygon shape (Canvas)
setShape: function(){
this.inherited(arguments);
var p = this.shape.points, f = p[0], r, c, i;
this.bbox = null;
// normalize this.shape.points as array of points: [{x,y}, {x,y}, ...]
this._normalizePoints();
// after _normalizePoints, if shape.points was [x1,y1,x2,y2,..], shape.points references a new array
// and p references the original points array
// prepare Canvas-specific structures, if needed
if(p.length){
if(typeof f == "number"){ // already in the canvas format [x1,y1,x2,y2,...]
r = p;
}else{ // convert into canvas-specific format
r = [];
for(i=0; i < p.length; ++i){
c = p[i];
r.push(c.x, c.y);
}
}
}else{
r = [];
}
this.canvasPolyline = r;
return this;
},
_renderShape: function(/* Object */ ctx){
var p = this.canvasPolyline;
if(p.length){
ctx.beginPath();
ctx.moveTo(p[0], p[1]);
for(var i = 2; i < p.length; i += 2){
ctx.lineTo(p[i], p[i + 1]);
}
}
}
});
declare("dojox.gfx.canvas.Image", [canvas.Shape, gs.Image], {
// summary: an image shape (Canvas)
setShape: function(){
this.inherited(arguments);
// prepare Canvas-specific structures
var img = new Image();
this.surface.downloadImage(img, this.shape.src);
this.canvasImage = img;
return this;
},
_renderShape: function(/* Object */ ctx){
var s = this.shape;
ctx.drawImage(this.canvasImage, s.x, s.y, s.width, s.height);
}
});
declare("dojox.gfx.canvas.Text", [canvas.Shape, gs.Text], {
_setFont:function(){
if (this.fontStyle){
this.canvasFont = g.makeFontString(this.fontStyle);
} else {
delete this.canvasFont;
}
},
getTextWidth: function(){
// summary: get the text width in pixels
var s = this.shape, w = 0, ctx;
if(s.text && s.text.length > 0){
ctx = this.surface.rawNode.getContext("2d");
ctx.save();
this._renderTransform(ctx);
this._renderFill(ctx, false);
this._renderStroke(ctx, false);
if (this.canvasFont)
ctx.font = this.canvasFont;
w = ctx.measureText(s.text).width;
ctx.restore();
}
return w;
},
// override to apply first fill and stroke (
// the base implementation is for path-based shape that needs to first define the path then to fill/stroke it.
// Here, we need the fillstyle or strokestyle to be set before calling fillText/strokeText.
_render: function(/* Object */ctx){
// summary: render the shape
// ctx : Object: the drawing context.
ctx.save();
this._renderTransform(ctx);
this._renderFill(ctx, false);
this._renderStroke(ctx, false);
this._renderShape(ctx);
ctx.restore();
},
_renderShape: function(ctx){
// summary: a text shape (Canvas)
// ctx : Object: the drawing context.
var ta, s = this.shape;
if(!s.text || s.text.length == 0){
return;
}
// text align
ta = s.align === 'middle' ? 'center' : s.align;
ctx.textAlign = ta;
if(this.canvasFont){
ctx.font = this.canvasFont;
}
if(this.canvasFill){
ctx.fillText(s.text, s.x, s.y);
}
if(this.strokeStyle){
ctx.beginPath(); // fix bug in FF3.6. Fixed in FF4b8
ctx.strokeText(s.text, s.x, s.y);
ctx.closePath();
}
}
});
modifyMethod(canvas.Text, "setFont");
// the next test is from https://github.com/phiggins42/has.js
if(win.global.CanvasRenderingContext2D){
// need to doublecheck canvas is supported since module can be loaded if building layers (ticket 14288)
var ctx2d = win.doc.createElement("canvas").getContext("2d");
if(ctx2d && typeof ctx2d.fillText != "function"){
canvas.Text.extend({
getTextWidth: function(){
return 0;
},
_renderShape: function(){
}
});
}
}
var pathRenderers = {
M: "_moveToA", m: "_moveToR",
L: "_lineToA", l: "_lineToR",
H: "_hLineToA", h: "_hLineToR",
V: "_vLineToA", v: "_vLineToR",
C: "_curveToA", c: "_curveToR",
S: "_smoothCurveToA", s: "_smoothCurveToR",
Q: "_qCurveToA", q: "_qCurveToR",
T: "_qSmoothCurveToA", t: "_qSmoothCurveToR",
A: "_arcTo", a: "_arcTo",
Z: "_closePath", z: "_closePath"
};
declare("dojox.gfx.canvas.Path", [canvas.Shape, pathLib.Path], {
// summary: a path shape (Canvas)
constructor: function(){
this.lastControl = {};
},
setShape: function(){
this.canvasPath = [];
return this.inherited(arguments);
},
_updateWithSegment: function(segment){
var last = lang.clone(this.last);
this[pathRenderers[segment.action]](this.canvasPath, segment.action, segment.args);
this.last = last;
this.inherited(arguments);
},
_renderShape: function(/* Object */ ctx){
var r = this.canvasPath;
ctx.beginPath();
for(var i = 0; i < r.length; i += 2){
ctx[r[i]].apply(ctx, r[i + 1]);
}
},
_moveToA: function(result, action, args){
result.push("moveTo", [args[0], args[1]]);
for(var i = 2; i < args.length; i += 2){
result.push("lineTo", [args[i], args[i + 1]]);
}
this.last.x = args[args.length - 2];
this.last.y = args[args.length - 1];
this.lastControl = {};
},
_moveToR: function(result, action, args){
if("x" in this.last){
result.push("moveTo", [this.last.x += args[0], this.last.y += args[1]]);
}else{
result.push("moveTo", [this.last.x = args[0], this.last.y = args[1]]);
}
for(var i = 2; i < args.length; i += 2){
result.push("lineTo", [this.last.x += args[i], this.last.y += args[i + 1]]);
}
this.lastControl = {};
},
_lineToA: function(result, action, args){
for(var i = 0; i < args.length; i += 2){
result.push("lineTo", [args[i], args[i + 1]]);
}
this.last.x = args[args.length - 2];
this.last.y = args[args.length - 1];
this.lastControl = {};
},
_lineToR: function(result, action, args){
for(var i = 0; i < args.length; i += 2){
result.push("lineTo", [this.last.x += args[i], this.last.y += args[i + 1]]);
}
this.lastControl = {};
},
_hLineToA: function(result, action, args){
for(var i = 0; i < args.length; ++i){
result.push("lineTo", [args[i], this.last.y]);
}
this.last.x = args[args.length - 1];
this.lastControl = {};
},
_hLineToR: function(result, action, args){
for(var i = 0; i < args.length; ++i){
result.push("lineTo", [this.last.x += args[i], this.last.y]);
}
this.lastControl = {};
},
_vLineToA: function(result, action, args){
for(var i = 0; i < args.length; ++i){
result.push("lineTo", [this.last.x, args[i]]);
}
this.last.y = args[args.length - 1];
this.lastControl = {};
},
_vLineToR: function(result, action, args){
for(var i = 0; i < args.length; ++i){
result.push("lineTo", [this.last.x, this.last.y += args[i]]);
}
this.lastControl = {};
},
_curveToA: function(result, action, args){
for(var i = 0; i < args.length; i += 6){
result.push("bezierCurveTo", args.slice(i, i + 6));
}
this.last.x = args[args.length - 2];
this.last.y = args[args.length - 1];
this.lastControl.x = args[args.length - 4];
this.lastControl.y = args[args.length - 3];
this.lastControl.type = "C";
},
_curveToR: function(result, action, args){
for(var i = 0; i < args.length; i += 6){
result.push("bezierCurveTo", [
this.last.x + args[i],
this.last.y + args[i + 1],
this.lastControl.x = this.last.x + args[i + 2],
this.lastControl.y = this.last.y + args[i + 3],
this.last.x + args[i + 4],
this.last.y + args[i + 5]
]);
this.last.x += args[i + 4];
this.last.y += args[i + 5];
}
this.lastControl.type = "C";
},
_smoothCurveToA: function(result, action, args){
for(var i = 0; i < args.length; i += 4){
var valid = this.lastControl.type == "C";
result.push("bezierCurveTo", [
valid ? 2 * this.last.x - this.lastControl.x : this.last.x,
valid ? 2 * this.last.y - this.lastControl.y : this.last.y,
args[i],
args[i + 1],
args[i + 2],
args[i + 3]
]);
this.lastControl.x = args[i];
this.lastControl.y = args[i + 1];
this.lastControl.type = "C";
}
this.last.x = args[args.length - 2];
this.last.y = args[args.length - 1];
},
_smoothCurveToR: function(result, action, args){
for(var i = 0; i < args.length; i += 4){
var valid = this.lastControl.type == "C";
result.push("bezierCurveTo", [
valid ? 2 * this.last.x - this.lastControl.x : this.last.x,
valid ? 2 * this.last.y - this.lastControl.y : this.last.y,
this.last.x + args[i],
this.last.y + args[i + 1],
this.last.x + args[i + 2],
this.last.y + args[i + 3]
]);
this.lastControl.x = this.last.x + args[i];
this.lastControl.y = this.last.y + args[i + 1];
this.lastControl.type = "C";
this.last.x += args[i + 2];
this.last.y += args[i + 3];
}
},
_qCurveToA: function(result, action, args){
for(var i = 0; i < args.length; i += 4){
result.push("quadraticCurveTo", args.slice(i, i + 4));
}
this.last.x = args[args.length - 2];
this.last.y = args[args.length - 1];
this.lastControl.x = args[args.length - 4];
this.lastControl.y = args[args.length - 3];
this.lastControl.type = "Q";
},
_qCurveToR: function(result, action, args){
for(var i = 0; i < args.length; i += 4){
result.push("quadraticCurveTo", [
this.lastControl.x = this.last.x + args[i],
this.lastControl.y = this.last.y + args[i + 1],
this.last.x + args[i + 2],
this.last.y + args[i + 3]
]);
this.last.x += args[i + 2];
this.last.y += args[i + 3];
}
this.lastControl.type = "Q";
},
_qSmoothCurveToA: function(result, action, args){
for(var i = 0; i < args.length; i += 2){
var valid = this.lastControl.type == "Q";
result.push("quadraticCurveTo", [
this.lastControl.x = valid ? 2 * this.last.x - this.lastControl.x : this.last.x,
this.lastControl.y = valid ? 2 * this.last.y - this.lastControl.y : this.last.y,
args[i],
args[i + 1]
]);
this.lastControl.type = "Q";
}
this.last.x = args[args.length - 2];
this.last.y = args[args.length - 1];
},
_qSmoothCurveToR: function(result, action, args){
for(var i = 0; i < args.length; i += 2){
var valid = this.lastControl.type == "Q";
result.push("quadraticCurveTo", [
this.lastControl.x = valid ? 2 * this.last.x - this.lastControl.x : this.last.x,
this.lastControl.y = valid ? 2 * this.last.y - this.lastControl.y : this.last.y,
this.last.x + args[i],
this.last.y + args[i + 1]
]);
this.lastControl.type = "Q";
this.last.x += args[i];
this.last.y += args[i + 1];
}
},
_arcTo: function(result, action, args){
var relative = action == "a";
for(var i = 0; i < args.length; i += 7){
var x1 = args[i + 5], y1 = args[i + 6];
if(relative){
x1 += this.last.x;
y1 += this.last.y;
}
var arcs = ga.arcAsBezier(
this.last, args[i], args[i + 1], args[i + 2],
args[i + 3] ? 1 : 0, args[i + 4] ? 1 : 0,
x1, y1
);
arr.forEach(arcs, function(p){
result.push("bezierCurveTo", p);
});
this.last.x = x1;
this.last.y = y1;
}
this.lastControl = {};
},
_closePath: function(result, action, args){
result.push("closePath", []);
this.lastControl = {};
}
});
arr.forEach(["moveTo", "lineTo", "hLineTo", "vLineTo", "curveTo",
"smoothCurveTo", "qCurveTo", "qSmoothCurveTo", "arcTo", "closePath"],
function(method){ modifyMethod(canvas.Path, method); }
);
declare("dojox.gfx.canvas.TextPath", [canvas.Shape, pathLib.TextPath], {
// summary: a text shape (Canvas)
_renderShape: function(/* Object */ ctx){
var s = this.shape;
// nothing for the moment
},
_setText: function(){
// not implemented
},
_setFont: function(){
// not implemented
}
});
declare("dojox.gfx.canvas.Surface", gs.Surface, {
// summary: a surface object to be used for drawings (Canvas)
constructor: function(){
gs.Container._init.call(this);
this.pendingImageCount = 0;
this.makeDirty();
},
setDimensions: function(width, height){
// summary: sets the width and height of the rawNode
// width: String: width of surface, e.g., "100px"
// height: String: height of surface, e.g., "100px"
this.width = g.normalizedLength(width); // in pixels
this.height = g.normalizedLength(height); // in pixels
if(!this.rawNode) return this;
var dirty = false;
if (this.rawNode.width != this.width){
this.rawNode.width = this.width;
dirty = true;
}
if (this.rawNode.height != this.height){
this.rawNode.height = this.height;
dirty = true;
}
if (dirty)
this.makeDirty();
return this; // self
},
getDimensions: function(){
// summary: returns an object with properties "width" and "height"
return this.rawNode ? {width: this.rawNode.width, height: this.rawNode.height} : null; // Object
},
_render: function(){
// summary: render the all shapes
if(this.pendingImageCount){ return; }
var ctx = this.rawNode.getContext("2d");
ctx.save();
ctx.clearRect(0, 0, this.rawNode.width, this.rawNode.height);
for(var i = 0; i < this.children.length; ++i){
this.children[i]._render(ctx);
}
ctx.restore();
if("pendingRender" in this){
clearTimeout(this.pendingRender);
delete this.pendingRender;
}
},
makeDirty: function(){
// summary: internal method, which is called when we may need to redraw
if(!this.pendingImagesCount && !("pendingRender" in this)){
this.pendingRender = setTimeout(lang.hitch(this, this._render), 0);
}
},
downloadImage: function(img, url){
// summary:
// internal method, which starts an image download and renders, when it is ready
// img: Image:
// the image object
// url: String:
// the url of the image
var handler = lang.hitch(this, this.onImageLoad);
if(!this.pendingImageCount++ && "pendingRender" in this){
clearTimeout(this.pendingRender);
delete this.pendingRender;
}
img.onload = handler;
img.onerror = handler;
img.onabort = handler;
img.src = url;
},
onImageLoad: function(){
if(!--this.pendingImageCount){ this._render(); }
},
// events are not implemented
getEventSource: function(){ return null; },
connect: function(){},
disconnect: function(){}
});
canvas.createSurface = function(parentNode, width, height){
// summary: creates a surface (Canvas)
// parentNode: Node: a parent node
// width: String: width of surface, e.g., "100px"
// height: String: height of surface, e.g., "100px"
if(!width && !height){
var pos = domGeom.position(parentNode);
width = width || pos.w;
height = height || pos.h;
}
if(typeof width == "number"){
width = width + "px";
}
if(typeof height == "number"){
height = height + "px";
}
var s = new canvas.Surface(),
p = dom.byId(parentNode),
c = p.ownerDocument.createElement("canvas");
c.width = g.normalizedLength(width); // in pixels
c.height = g.normalizedLength(height); // in pixels
p.appendChild(c);
s.rawNode = c;
s._parent = p;
s.surface = s;
return s; // dojox.gfx.Surface
};
// Extenders
var C = gs.Container, Container = {
add: function(shape){
this.surface.makeDirty();
return C.add.apply(this, arguments);
},
remove: function(shape, silently){
this.surface.makeDirty();
return C.remove.apply(this, arguments);
},
clear: function(){
this.surface.makeDirty();
return C.clear.apply(this, arguments);
},
_moveChildToFront: function(shape){
this.surface.makeDirty();
return C._moveChildToFront.apply(this, arguments);
},
_moveChildToBack: function(shape){
this.surface.makeDirty();
return C._moveChildToBack.apply(this, arguments);
}
};
var Creator = {
// summary: Canvas shape creators
createObject: function(shapeType, rawShape) {
// summary: creates an instance of the passed shapeType class
// shapeType: Function: a class constructor to create an instance of
// rawShape: Object: properties to be passed in to the classes "setShape" method
// overrideSize: Boolean: set the size explicitly, if true
var shape = new shapeType();
shape.surface = this.surface;
shape.setShape(rawShape);
this.add(shape);
return shape; // dojox.gfx.Shape
}
};
extend(canvas.Group, Container);
extend(canvas.Group, gs.Creator);
extend(canvas.Group, Creator);
extend(canvas.Surface, Container);
extend(canvas.Surface, gs.Creator);
extend(canvas.Surface, Creator);
// no event support -> nothing to fix.
canvas.fixTarget = function(event, gfxElement){
return true;
};
return canvas;
});
|