2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.gfx.canvas"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.gfx.canvas"] = true;
|
|
|
3 |
dojo.provide("dojox.gfx.canvas");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.gfx._base");
|
|
|
6 |
dojo.require("dojox.gfx.shape");
|
|
|
7 |
dojo.require("dojox.gfx.path");
|
|
|
8 |
dojo.require("dojox.gfx.arc");
|
|
|
9 |
dojo.require("dojox.gfx.decompose");
|
|
|
10 |
|
|
|
11 |
dojo.experimental("dojox.gfx.canvas");
|
|
|
12 |
|
|
|
13 |
(function(){
|
|
|
14 |
var g = dojox.gfx, gs = g.shape, ga = g.arc,
|
|
|
15 |
m = g.matrix, mp = m.multiplyPoint, twoPI = 2 * Math.PI;
|
|
|
16 |
|
|
|
17 |
dojo.extend(g.Shape, {
|
|
|
18 |
render: function(/* Object */ ctx){
|
|
|
19 |
// summary: render the shape
|
|
|
20 |
ctx.save();
|
|
|
21 |
this._renderTransform(ctx);
|
|
|
22 |
this._renderShape(ctx);
|
|
|
23 |
this._renderFill(ctx, true);
|
|
|
24 |
this._renderStroke(ctx, true);
|
|
|
25 |
ctx.restore();
|
|
|
26 |
},
|
|
|
27 |
_renderTransform: function(/* Object */ ctx){
|
|
|
28 |
if("canvasTransform" in this){
|
|
|
29 |
var t = this.canvasTransform;
|
|
|
30 |
ctx.translate(t.dx, t.dy);
|
|
|
31 |
ctx.rotate(t.angle2);
|
|
|
32 |
ctx.scale(t.sx, t.sy);
|
|
|
33 |
ctx.rotate(t.angle1);
|
|
|
34 |
// The future implementation when vendors catch up with the spec:
|
|
|
35 |
// var t = this.matrix;
|
|
|
36 |
// ctx.transform(t.xx, t.yx, t.xy, t.yy, t.dx, t.dy);
|
|
|
37 |
}
|
|
|
38 |
},
|
|
|
39 |
_renderShape: function(/* Object */ ctx){
|
|
|
40 |
// nothing
|
|
|
41 |
},
|
|
|
42 |
_renderFill: function(/* Object */ ctx, /* Boolean */ apply){
|
|
|
43 |
if("canvasFill" in this){
|
|
|
44 |
if("canvasFillImage" in this){
|
|
|
45 |
this.canvasFill = ctx.createPattern(this.canvasFillImage, "repeat");
|
|
|
46 |
delete this.canvasFillImage;
|
|
|
47 |
}
|
|
|
48 |
ctx.fillStyle = this.canvasFill;
|
|
|
49 |
if(apply){ ctx.fill(); }
|
|
|
50 |
}else{
|
|
|
51 |
ctx.fillStyle = "rgba(0,0,0,0.0)";
|
|
|
52 |
}
|
|
|
53 |
},
|
|
|
54 |
_renderStroke: function(/* Object */ ctx, /* Boolean */ apply){
|
|
|
55 |
var s = this.strokeStyle;
|
|
|
56 |
if(s){
|
|
|
57 |
ctx.strokeStyle = s.color.toString();
|
|
|
58 |
ctx.lineWidth = s.width;
|
|
|
59 |
ctx.lineCap = s.cap;
|
|
|
60 |
if(typeof s.join == "number"){
|
|
|
61 |
ctx.lineJoin = "miter";
|
|
|
62 |
ctx.miterLimit = s.join;
|
|
|
63 |
}else{
|
|
|
64 |
ctx.lineJoin = s.join;
|
|
|
65 |
}
|
|
|
66 |
if(apply){ ctx.stroke(); }
|
|
|
67 |
}else if(!apply){
|
|
|
68 |
ctx.strokeStyle = "rgba(0,0,0,0.0)";
|
|
|
69 |
}
|
|
|
70 |
},
|
|
|
71 |
|
|
|
72 |
// events are not implemented
|
|
|
73 |
getEventSource: function(){ return null; },
|
|
|
74 |
connect: function(){},
|
|
|
75 |
disconnect: function(){}
|
|
|
76 |
});
|
|
|
77 |
|
|
|
78 |
var modifyMethod = function(shape, method, extra){
|
|
|
79 |
var old = shape.prototype[method];
|
|
|
80 |
shape.prototype[method] = extra ?
|
|
|
81 |
function(){
|
|
|
82 |
this.surface.makeDirty();
|
|
|
83 |
old.apply(this, arguments);
|
|
|
84 |
extra.call(this);
|
|
|
85 |
return this;
|
|
|
86 |
} :
|
|
|
87 |
function(){
|
|
|
88 |
this.surface.makeDirty();
|
|
|
89 |
return old.apply(this, arguments);
|
|
|
90 |
};
|
|
|
91 |
};
|
|
|
92 |
|
|
|
93 |
modifyMethod(g.Shape, "setTransform",
|
|
|
94 |
function(){
|
|
|
95 |
// prepare Canvas-specific structures
|
|
|
96 |
if(this.matrix){
|
|
|
97 |
this.canvasTransform = g.decompose(this.matrix);
|
|
|
98 |
}else{
|
|
|
99 |
delete this.canvasTransform;
|
|
|
100 |
}
|
|
|
101 |
});
|
|
|
102 |
|
|
|
103 |
modifyMethod(g.Shape, "setFill",
|
|
|
104 |
function(){
|
|
|
105 |
// prepare Canvas-specific structures
|
|
|
106 |
var fs = this.fillStyle, f;
|
|
|
107 |
if(fs){
|
|
|
108 |
if(typeof(fs) == "object" && "type" in fs){
|
|
|
109 |
var ctx = this.surface.rawNode.getContext("2d");
|
|
|
110 |
switch(fs.type){
|
|
|
111 |
case "linear":
|
|
|
112 |
case "radial":
|
|
|
113 |
f = fs.type == "linear" ?
|
|
|
114 |
ctx.createLinearGradient(fs.x1, fs.y1, fs.x2, fs.y2) :
|
|
|
115 |
ctx.createRadialGradient(fs.cx, fs.cy, 0, fs.cx, fs.cy, fs.r);
|
|
|
116 |
dojo.forEach(fs.colors, function(step){
|
|
|
117 |
f.addColorStop(step.offset, g.normalizeColor(step.color).toString());
|
|
|
118 |
});
|
|
|
119 |
break;
|
|
|
120 |
case "pattern":
|
|
|
121 |
var img = new Image(fs.width, fs.height);
|
|
|
122 |
this.surface.downloadImage(img, fs.src);
|
|
|
123 |
this.canvasFillImage = img;
|
|
|
124 |
}
|
|
|
125 |
}else{
|
|
|
126 |
// Set fill color using CSS RGBA func style
|
|
|
127 |
f = fs.toString();
|
|
|
128 |
}
|
|
|
129 |
this.canvasFill = f;
|
|
|
130 |
}else{
|
|
|
131 |
delete this.canvasFill;
|
|
|
132 |
}
|
|
|
133 |
});
|
|
|
134 |
|
|
|
135 |
modifyMethod(g.Shape, "setStroke");
|
|
|
136 |
modifyMethod(g.Shape, "setShape");
|
|
|
137 |
|
|
|
138 |
dojo.declare("dojox.gfx.Group", g.Shape, {
|
|
|
139 |
// summary: a group shape (Canvas), which can be used
|
|
|
140 |
// to logically group shapes (e.g, to propagate matricies)
|
|
|
141 |
constructor: function(){
|
|
|
142 |
gs.Container._init.call(this);
|
|
|
143 |
},
|
|
|
144 |
render: function(/* Object */ ctx){
|
|
|
145 |
// summary: render the group
|
|
|
146 |
ctx.save();
|
|
|
147 |
this._renderTransform(ctx);
|
|
|
148 |
this._renderFill(ctx);
|
|
|
149 |
this._renderStroke(ctx);
|
|
|
150 |
for(var i = 0; i < this.children.length; ++i){
|
|
|
151 |
this.children[i].render(ctx);
|
|
|
152 |
}
|
|
|
153 |
ctx.restore();
|
|
|
154 |
}
|
|
|
155 |
});
|
|
|
156 |
|
|
|
157 |
dojo.declare("dojox.gfx.Rect", gs.Rect, {
|
|
|
158 |
// summary: a rectangle shape (Canvas)
|
|
|
159 |
_renderShape: function(/* Object */ ctx){
|
|
|
160 |
var s = this.shape, r = Math.min(s.r, s.height / 2, s.width / 2),
|
|
|
161 |
xl = s.x, xr = xl + s.width, yt = s.y, yb = yt + s.height,
|
|
|
162 |
xl2 = xl + r, xr2 = xr - r, yt2 = yt + r, yb2 = yb - r;
|
|
|
163 |
ctx.beginPath();
|
|
|
164 |
ctx.moveTo(xl2, yt);
|
|
|
165 |
ctx.lineTo(xr2, yt);
|
|
|
166 |
if(r){ ctx.arcTo(xr, yt, xr, yt2, r); }
|
|
|
167 |
ctx.lineTo(xr, yb2);
|
|
|
168 |
if(r){ ctx.arcTo(xr, yb, xr2, yb, r); }
|
|
|
169 |
ctx.lineTo(xl2, yb);
|
|
|
170 |
if(r){ ctx.arcTo(xl, yb, xl, yb2, r); }
|
|
|
171 |
ctx.lineTo(xl, yt2);
|
|
|
172 |
if(r){ ctx.arcTo(xl, yt, xl2, yt, r); }
|
|
|
173 |
ctx.closePath();
|
|
|
174 |
}
|
|
|
175 |
});
|
|
|
176 |
|
|
|
177 |
var bezierCircle = [];
|
|
|
178 |
(function(){
|
|
|
179 |
var u = ga.curvePI4;
|
|
|
180 |
bezierCircle.push(u.s, u.c1, u.c2, u.e);
|
|
|
181 |
for(var a = 45; a < 360; a += 45){
|
|
|
182 |
var r = m.rotateg(a);
|
|
|
183 |
bezierCircle.push(mp(r, u.c1), mp(r, u.c2), mp(r, u.e));
|
|
|
184 |
}
|
|
|
185 |
})();
|
|
|
186 |
|
|
|
187 |
dojo.declare("dojox.gfx.Ellipse", gs.Ellipse, {
|
|
|
188 |
// summary: an ellipse shape (Canvas)
|
|
|
189 |
setShape: function(){
|
|
|
190 |
g.Ellipse.superclass.setShape.apply(this, arguments);
|
|
|
191 |
// prepare Canvas-specific structures
|
|
|
192 |
var s = this.shape, t, c1, c2, r = [],
|
|
|
193 |
M = m.normalize([m.translate(s.cx, s.cy), m.scale(s.rx, s.ry)]);
|
|
|
194 |
t = mp(M, bezierCircle[0]);
|
|
|
195 |
r.push([t.x, t.y]);
|
|
|
196 |
for(var i = 1; i < bezierCircle.length; i += 3){
|
|
|
197 |
c1 = mp(M, bezierCircle[i]);
|
|
|
198 |
c2 = mp(M, bezierCircle[i + 1]);
|
|
|
199 |
t = mp(M, bezierCircle[i + 2]);
|
|
|
200 |
r.push([c1.x, c1.y, c2.x, c2.y, t.x, t.y]);
|
|
|
201 |
}
|
|
|
202 |
this.canvasEllipse = r;
|
|
|
203 |
return this;
|
|
|
204 |
},
|
|
|
205 |
_renderShape: function(/* Object */ ctx){
|
|
|
206 |
var r = this.canvasEllipse;
|
|
|
207 |
ctx.beginPath();
|
|
|
208 |
ctx.moveTo.apply(ctx, r[0]);
|
|
|
209 |
for(var i = 1; i < r.length; ++i){
|
|
|
210 |
ctx.bezierCurveTo.apply(ctx, r[i]);
|
|
|
211 |
}
|
|
|
212 |
ctx.closePath();
|
|
|
213 |
}
|
|
|
214 |
});
|
|
|
215 |
|
|
|
216 |
dojo.declare("dojox.gfx.Circle", gs.Circle, {
|
|
|
217 |
// summary: a circle shape (Canvas)
|
|
|
218 |
_renderShape: function(/* Object */ ctx){
|
|
|
219 |
var s = this.shape;
|
|
|
220 |
ctx.beginPath();
|
|
|
221 |
ctx.arc(s.cx, s.cy, s.r, 0, twoPI, 1);
|
|
|
222 |
}
|
|
|
223 |
});
|
|
|
224 |
|
|
|
225 |
dojo.declare("dojox.gfx.Line", gs.Line, {
|
|
|
226 |
// summary: a line shape (Canvas)
|
|
|
227 |
_renderShape: function(/* Object */ ctx){
|
|
|
228 |
var s = this.shape;
|
|
|
229 |
ctx.beginPath();
|
|
|
230 |
ctx.moveTo(s.x1, s.y1);
|
|
|
231 |
ctx.lineTo(s.x2, s.y2);
|
|
|
232 |
}
|
|
|
233 |
});
|
|
|
234 |
|
|
|
235 |
dojo.declare("dojox.gfx.Polyline", gs.Polyline, {
|
|
|
236 |
// summary: a polyline/polygon shape (Canvas)
|
|
|
237 |
setShape: function(){
|
|
|
238 |
g.Polyline.superclass.setShape.apply(this, arguments);
|
|
|
239 |
// prepare Canvas-specific structures
|
|
|
240 |
var p = this.shape.points, f = p[0], r = [], c, i;
|
|
|
241 |
if(p.length){
|
|
|
242 |
if(typeof f == "number"){
|
|
|
243 |
r.push(f, p[1]);
|
|
|
244 |
i = 2;
|
|
|
245 |
}else{
|
|
|
246 |
r.push(f.x, f.y);
|
|
|
247 |
i = 1;
|
|
|
248 |
}
|
|
|
249 |
for(; i < p.length; ++i){
|
|
|
250 |
c = p[i];
|
|
|
251 |
if(typeof c == "number"){
|
|
|
252 |
r.push(c, p[++i]);
|
|
|
253 |
}else{
|
|
|
254 |
r.push(c.x, c.y);
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
this.canvasPolyline = r;
|
|
|
259 |
return this;
|
|
|
260 |
},
|
|
|
261 |
_renderShape: function(/* Object */ ctx){
|
|
|
262 |
var p = this.canvasPolyline;
|
|
|
263 |
if(p.length){
|
|
|
264 |
ctx.beginPath();
|
|
|
265 |
ctx.moveTo(p[0], p[1]);
|
|
|
266 |
for(var i = 2; i < p.length; i += 2){
|
|
|
267 |
ctx.lineTo(p[i], p[i + 1]);
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
});
|
|
|
272 |
|
|
|
273 |
dojo.declare("dojox.gfx.Image", gs.Image, {
|
|
|
274 |
// summary: an image shape (Canvas)
|
|
|
275 |
setShape: function(){
|
|
|
276 |
g.Image.superclass.setShape.apply(this, arguments);
|
|
|
277 |
// prepare Canvas-specific structures
|
|
|
278 |
var img = new Image();
|
|
|
279 |
this.surface.downloadImage(img, this.shape.src);
|
|
|
280 |
this.canvasImage = img;
|
|
|
281 |
return this;
|
|
|
282 |
},
|
|
|
283 |
_renderShape: function(/* Object */ ctx){
|
|
|
284 |
var s = this.shape;
|
|
|
285 |
ctx.drawImage(this.canvasImage, s.x, s.y, s.width, s.height);
|
|
|
286 |
}
|
|
|
287 |
});
|
|
|
288 |
|
|
|
289 |
dojo.declare("dojox.gfx.Text", gs.Text, {
|
|
|
290 |
// summary: a text shape (Canvas)
|
|
|
291 |
_renderShape: function(/* Object */ ctx){
|
|
|
292 |
var s = this.shape;
|
|
|
293 |
// nothing for the moment
|
|
|
294 |
}
|
|
|
295 |
});
|
|
|
296 |
modifyMethod(g.Text, "setFont");
|
|
|
297 |
|
|
|
298 |
var pathRenderers = {
|
|
|
299 |
M: "_moveToA", m: "_moveToR",
|
|
|
300 |
L: "_lineToA", l: "_lineToR",
|
|
|
301 |
H: "_hLineToA", h: "_hLineToR",
|
|
|
302 |
V: "_vLineToA", v: "_vLineToR",
|
|
|
303 |
C: "_curveToA", c: "_curveToR",
|
|
|
304 |
S: "_smoothCurveToA", s: "_smoothCurveToR",
|
|
|
305 |
Q: "_qCurveToA", q: "_qCurveToR",
|
|
|
306 |
T: "_qSmoothCurveToA", t: "_qSmoothCurveToR",
|
|
|
307 |
A: "_arcTo", a: "_arcTo",
|
|
|
308 |
Z: "_closePath", z: "_closePath"
|
|
|
309 |
};
|
|
|
310 |
|
|
|
311 |
dojo.declare("dojox.gfx.Path", g.path.Path, {
|
|
|
312 |
// summary: a path shape (Canvas)
|
|
|
313 |
constructor: function(){
|
|
|
314 |
this.last = {};
|
|
|
315 |
this.lastControl = {};
|
|
|
316 |
},
|
|
|
317 |
setShape: function(){
|
|
|
318 |
this.canvasPath = [];
|
|
|
319 |
return g.Path.superclass.setShape.apply(this, arguments);
|
|
|
320 |
},
|
|
|
321 |
_updateWithSegment: function(segment){
|
|
|
322 |
var last = dojo.clone(this.last);
|
|
|
323 |
this[pathRenderers[segment.action]](this.canvasPath, segment.action, segment.args);
|
|
|
324 |
this.last = last;
|
|
|
325 |
g.Path.superclass._updateWithSegment.apply(this, arguments);
|
|
|
326 |
},
|
|
|
327 |
_renderShape: function(/* Object */ ctx){
|
|
|
328 |
var r = this.canvasPath;
|
|
|
329 |
ctx.beginPath();
|
|
|
330 |
for(var i = 0; i < r.length; i += 2){
|
|
|
331 |
ctx[r[i]].apply(ctx, r[i + 1]);
|
|
|
332 |
}
|
|
|
333 |
},
|
|
|
334 |
_moveToA: function(result, action, args){
|
|
|
335 |
result.push("moveTo", [args[0], args[1]]);
|
|
|
336 |
for(var i = 2; i < args.length; i += 2){
|
|
|
337 |
result.push("lineTo", [args[i], args[i + 1]]);
|
|
|
338 |
}
|
|
|
339 |
this.last.x = args[args.length - 2];
|
|
|
340 |
this.last.y = args[args.length - 1];
|
|
|
341 |
this.lastControl = {};
|
|
|
342 |
},
|
|
|
343 |
_moveToR: function(result, action, args){
|
|
|
344 |
if("x" in this.last){
|
|
|
345 |
result.push("moveTo", [this.last.x += args[0], this.last.y += args[1]]);
|
|
|
346 |
}else{
|
|
|
347 |
result.push("moveTo", [this.last.x = args[0], this.last.y = args[1]]);
|
|
|
348 |
}
|
|
|
349 |
for(var i = 2; i < args.length; i += 2){
|
|
|
350 |
result.push("lineTo", [this.last.x += args[i], this.last.y += args[i + 1]]);
|
|
|
351 |
}
|
|
|
352 |
this.lastControl = {};
|
|
|
353 |
},
|
|
|
354 |
_lineToA: function(result, action, args){
|
|
|
355 |
for(var i = 0; i < args.length; i += 2){
|
|
|
356 |
result.push("lineTo", [args[i], args[i + 1]]);
|
|
|
357 |
}
|
|
|
358 |
this.last.x = args[args.length - 2];
|
|
|
359 |
this.last.y = args[args.length - 1];
|
|
|
360 |
this.lastControl = {};
|
|
|
361 |
},
|
|
|
362 |
_lineToR: function(result, action, args){
|
|
|
363 |
for(var i = 0; i < args.length; i += 2){
|
|
|
364 |
result.push("lineTo", [this.last.x += args[i], this.last.y += args[i + 1]]);
|
|
|
365 |
}
|
|
|
366 |
this.lastControl = {};
|
|
|
367 |
},
|
|
|
368 |
_hLineToA: function(result, action, args){
|
|
|
369 |
for(var i = 0; i < args.length; ++i){
|
|
|
370 |
result.push("lineTo", [args[i], this.last.y]);
|
|
|
371 |
}
|
|
|
372 |
this.last.x = args[args.length - 1];
|
|
|
373 |
this.lastControl = {};
|
|
|
374 |
},
|
|
|
375 |
_hLineToR: function(result, action, args){
|
|
|
376 |
for(var i = 0; i < args.length; ++i){
|
|
|
377 |
result.push("lineTo", [this.last.x += args[i], this.last.y]);
|
|
|
378 |
}
|
|
|
379 |
this.lastControl = {};
|
|
|
380 |
},
|
|
|
381 |
_vLineToA: function(result, action, args){
|
|
|
382 |
for(var i = 0; i < args.length; ++i){
|
|
|
383 |
result.push("lineTo", [this.last.x, args[i]]);
|
|
|
384 |
}
|
|
|
385 |
this.last.y = args[args.length - 1];
|
|
|
386 |
this.lastControl = {};
|
|
|
387 |
},
|
|
|
388 |
_vLineToR: function(result, action, args){
|
|
|
389 |
for(var i = 0; i < args.length; ++i){
|
|
|
390 |
result.push("lineTo", [this.last.x, this.last.y += args[i]]);
|
|
|
391 |
}
|
|
|
392 |
this.lastControl = {};
|
|
|
393 |
},
|
|
|
394 |
_curveToA: function(result, action, args){
|
|
|
395 |
for(var i = 0; i < args.length; i += 6){
|
|
|
396 |
result.push("bezierCurveTo", args.slice(i, i + 6));
|
|
|
397 |
}
|
|
|
398 |
this.last.x = args[args.length - 2];
|
|
|
399 |
this.last.y = args[args.length - 1];
|
|
|
400 |
this.lastControl.x = args[args.length - 4];
|
|
|
401 |
this.lastControl.y = args[args.length - 3];
|
|
|
402 |
this.lastControl.type = "C";
|
|
|
403 |
},
|
|
|
404 |
_curveToR: function(result, action, args){
|
|
|
405 |
for(var i = 0; i < args.length; i += 6){
|
|
|
406 |
result.push("bezierCurveTo", [
|
|
|
407 |
this.last.x + args[i],
|
|
|
408 |
this.last.y + args[i + 1],
|
|
|
409 |
this.lastControl.x = this.last.x + args[i + 2],
|
|
|
410 |
this.lastControl.y = this.last.y + args[i + 3],
|
|
|
411 |
this.last.x + args[i + 4],
|
|
|
412 |
this.last.y + args[i + 5]
|
|
|
413 |
]);
|
|
|
414 |
this.last.x += args[i + 4];
|
|
|
415 |
this.last.y += args[i + 5];
|
|
|
416 |
}
|
|
|
417 |
this.lastControl.type = "C";
|
|
|
418 |
},
|
|
|
419 |
_smoothCurveToA: function(result, action, args){
|
|
|
420 |
for(var i = 0; i < args.length; i += 4){
|
|
|
421 |
var valid = this.lastControl.type == "C";
|
|
|
422 |
result.push("bezierCurveTo", [
|
|
|
423 |
valid ? 2 * this.last.x - this.lastControl.x : this.last.x,
|
|
|
424 |
valid ? 2 * this.last.y - this.lastControl.y : this.last.y,
|
|
|
425 |
args[i],
|
|
|
426 |
args[i + 1],
|
|
|
427 |
args[i + 2],
|
|
|
428 |
args[i + 3]
|
|
|
429 |
]);
|
|
|
430 |
this.lastControl.x = args[i];
|
|
|
431 |
this.lastControl.y = args[i + 1];
|
|
|
432 |
this.lastControl.type = "C";
|
|
|
433 |
}
|
|
|
434 |
this.last.x = args[args.length - 2];
|
|
|
435 |
this.last.y = args[args.length - 1];
|
|
|
436 |
},
|
|
|
437 |
_smoothCurveToR: function(result, action, args){
|
|
|
438 |
for(var i = 0; i < args.length; i += 4){
|
|
|
439 |
var valid = this.lastControl.type == "C";
|
|
|
440 |
result.push("bezierCurveTo", [
|
|
|
441 |
valid ? 2 * this.last.x - this.lastControl.x : this.last.x,
|
|
|
442 |
valid ? 2 * this.last.y - this.lastControl.y : this.last.y,
|
|
|
443 |
this.last.x + args[i],
|
|
|
444 |
this.last.y + args[i + 1],
|
|
|
445 |
this.last.x + args[i + 2],
|
|
|
446 |
this.last.y + args[i + 3]
|
|
|
447 |
]);
|
|
|
448 |
this.lastControl.x = this.last.x + args[i];
|
|
|
449 |
this.lastControl.y = this.last.y + args[i + 1];
|
|
|
450 |
this.lastControl.type = "C";
|
|
|
451 |
this.last.x += args[i + 2];
|
|
|
452 |
this.last.y += args[i + 3];
|
|
|
453 |
}
|
|
|
454 |
},
|
|
|
455 |
_qCurveToA: function(result, action, args){
|
|
|
456 |
for(var i = 0; i < args.length; i += 4){
|
|
|
457 |
result.push("quadraticCurveTo", args.slice(i, i + 4));
|
|
|
458 |
}
|
|
|
459 |
this.last.x = args[args.length - 2];
|
|
|
460 |
this.last.y = args[args.length - 1];
|
|
|
461 |
this.lastControl.x = args[args.length - 4];
|
|
|
462 |
this.lastControl.y = args[args.length - 3];
|
|
|
463 |
this.lastControl.type = "Q";
|
|
|
464 |
},
|
|
|
465 |
_qCurveToR: function(result, action, args){
|
|
|
466 |
for(var i = 0; i < args.length; i += 4){
|
|
|
467 |
result.push("quadraticCurveTo", [
|
|
|
468 |
this.lastControl.x = this.last.x + args[i],
|
|
|
469 |
this.lastControl.y = this.last.y + args[i + 1],
|
|
|
470 |
this.last.x + args[i + 2],
|
|
|
471 |
this.last.y + args[i + 3]
|
|
|
472 |
]);
|
|
|
473 |
this.last.x += args[i + 2];
|
|
|
474 |
this.last.y += args[i + 3];
|
|
|
475 |
}
|
|
|
476 |
this.lastControl.type = "Q";
|
|
|
477 |
},
|
|
|
478 |
_qSmoothCurveToA: function(result, action, args){
|
|
|
479 |
for(var i = 0; i < args.length; i += 2){
|
|
|
480 |
var valid = this.lastControl.type == "Q";
|
|
|
481 |
result.push("quadraticCurveTo", [
|
|
|
482 |
this.lastControl.x = valid ? 2 * this.last.x - this.lastControl.x : this.last.x,
|
|
|
483 |
this.lastControl.y = valid ? 2 * this.last.y - this.lastControl.y : this.last.y,
|
|
|
484 |
args[i],
|
|
|
485 |
args[i + 1]
|
|
|
486 |
]);
|
|
|
487 |
this.lastControl.type = "Q";
|
|
|
488 |
}
|
|
|
489 |
this.last.x = args[args.length - 2];
|
|
|
490 |
this.last.y = args[args.length - 1];
|
|
|
491 |
},
|
|
|
492 |
_qSmoothCurveToR: function(result, action, args){
|
|
|
493 |
for(var i = 0; i < args.length; i += 2){
|
|
|
494 |
var valid = this.lastControl.type == "Q";
|
|
|
495 |
result.push("quadraticCurveTo", [
|
|
|
496 |
this.lastControl.x = valid ? 2 * this.last.x - this.lastControl.x : this.last.x,
|
|
|
497 |
this.lastControl.y = valid ? 2 * this.last.y - this.lastControl.y : this.last.y,
|
|
|
498 |
this.last.x + args[i],
|
|
|
499 |
this.last.y + args[i + 1]
|
|
|
500 |
]);
|
|
|
501 |
this.lastControl.type = "Q";
|
|
|
502 |
this.last.x += args[i];
|
|
|
503 |
this.last.y += args[i + 1];
|
|
|
504 |
}
|
|
|
505 |
},
|
|
|
506 |
_arcTo: function(result, action, args){
|
|
|
507 |
var relative = action == "a";
|
|
|
508 |
for(var i = 0; i < args.length; i += 7){
|
|
|
509 |
var x1 = args[i + 5], y1 = args[i + 6];
|
|
|
510 |
if(relative){
|
|
|
511 |
x1 += this.last.x;
|
|
|
512 |
y1 += this.last.y;
|
|
|
513 |
}
|
|
|
514 |
var arcs = ga.arcAsBezier(
|
|
|
515 |
this.last, args[i], args[i + 1], args[i + 2],
|
|
|
516 |
args[i + 3] ? 1 : 0, args[i + 4] ? 1 : 0,
|
|
|
517 |
x1, y1
|
|
|
518 |
);
|
|
|
519 |
dojo.forEach(arcs, function(p){
|
|
|
520 |
result.push("bezierCurveTo", p);
|
|
|
521 |
});
|
|
|
522 |
this.last.x = x1;
|
|
|
523 |
this.last.y = y1;
|
|
|
524 |
}
|
|
|
525 |
this.lastControl = {};
|
|
|
526 |
},
|
|
|
527 |
_closePath: function(result, action, args){
|
|
|
528 |
result.push("closePath", []);
|
|
|
529 |
this.lastControl = {};
|
|
|
530 |
}
|
|
|
531 |
});
|
|
|
532 |
dojo.forEach(["moveTo", "lineTo", "hLineTo", "vLineTo", "curveTo",
|
|
|
533 |
"smoothCurveTo", "qCurveTo", "qSmoothCurveTo", "arcTo", "closePath"],
|
|
|
534 |
function(method){ modifyMethod(g.Path, method); }
|
|
|
535 |
);
|
|
|
536 |
|
|
|
537 |
dojo.declare("dojox.gfx.TextPath", g.path.TextPath, {
|
|
|
538 |
// summary: a text shape (Canvas)
|
|
|
539 |
_renderShape: function(/* Object */ ctx){
|
|
|
540 |
var s = this.shape;
|
|
|
541 |
// nothing for the moment
|
|
|
542 |
}
|
|
|
543 |
});
|
|
|
544 |
|
|
|
545 |
dojo.declare("dojox.gfx.Surface", gs.Surface, {
|
|
|
546 |
// summary: a surface object to be used for drawings (Canvas)
|
|
|
547 |
constructor: function(){
|
|
|
548 |
gs.Container._init.call(this);
|
|
|
549 |
this.pendingImageCount = 0;
|
|
|
550 |
this.makeDirty();
|
|
|
551 |
},
|
|
|
552 |
setDimensions: function(width, height){
|
|
|
553 |
// summary: sets the width and height of the rawNode
|
|
|
554 |
// width: String: width of surface, e.g., "100px"
|
|
|
555 |
// height: String: height of surface, e.g., "100px"
|
|
|
556 |
this.width = g.normalizedLength(width); // in pixels
|
|
|
557 |
this.height = g.normalizedLength(height); // in pixels
|
|
|
558 |
if(!this.rawNode) return this;
|
|
|
559 |
this.rawNode.width = width;
|
|
|
560 |
this.rawNode.height = height;
|
|
|
561 |
this.makeDirty();
|
|
|
562 |
return this; // self
|
|
|
563 |
},
|
|
|
564 |
getDimensions: function(){
|
|
|
565 |
// summary: returns an object with properties "width" and "height"
|
|
|
566 |
return this.rawNode ? {width: this.rawNode.width, height: this.rawNode.height} : null; // Object
|
|
|
567 |
},
|
|
|
568 |
render: function(){
|
|
|
569 |
// summary: render the all shapes
|
|
|
570 |
if(this.pendingImageCount){ return; }
|
|
|
571 |
var ctx = this.rawNode.getContext("2d");
|
|
|
572 |
ctx.save();
|
|
|
573 |
ctx.clearRect(0, 0, this.rawNode.width, this.rawNode.height);
|
|
|
574 |
for(var i = 0; i < this.children.length; ++i){
|
|
|
575 |
this.children[i].render(ctx);
|
|
|
576 |
}
|
|
|
577 |
ctx.restore();
|
|
|
578 |
if("pendingRender" in this){
|
|
|
579 |
clearTimeout(this.pendingRender);
|
|
|
580 |
delete this.pendingRender;
|
|
|
581 |
}
|
|
|
582 |
},
|
|
|
583 |
makeDirty: function(){
|
|
|
584 |
// summary: internal method, which is called when we may need to redraw
|
|
|
585 |
if(!this.pendingImagesCount && !("pendingRender" in this)){
|
|
|
586 |
this.pendingRender = setTimeout(dojo.hitch(this, this.render), 0);
|
|
|
587 |
}
|
|
|
588 |
},
|
|
|
589 |
downloadImage: function(img, url){
|
|
|
590 |
// summary:
|
|
|
591 |
// internal method, which starts an image download and renders, when it is ready
|
|
|
592 |
// img: Image:
|
|
|
593 |
// the image object
|
|
|
594 |
// url: String:
|
|
|
595 |
// the url of the image
|
|
|
596 |
var handler = dojo.hitch(this, this.onImageLoad);
|
|
|
597 |
if(!this.pendingImageCount++ && "pendingRender" in this){
|
|
|
598 |
clearTimeout(this.pendingRender);
|
|
|
599 |
delete this.pendingRender;
|
|
|
600 |
}
|
|
|
601 |
img.onload = handler;
|
|
|
602 |
img.onerror = handler;
|
|
|
603 |
img.onabort = handler;
|
|
|
604 |
img.src = url;
|
|
|
605 |
},
|
|
|
606 |
onImageLoad: function(){
|
|
|
607 |
if(!--this.pendingImageCount){ this.render(); }
|
|
|
608 |
},
|
|
|
609 |
|
|
|
610 |
// events are not implemented
|
|
|
611 |
getEventSource: function(){ return null; },
|
|
|
612 |
connect: function(){},
|
|
|
613 |
disconnect: function(){}
|
|
|
614 |
});
|
|
|
615 |
|
|
|
616 |
g.createSurface = function(parentNode, width, height){
|
|
|
617 |
// summary: creates a surface (Canvas)
|
|
|
618 |
// parentNode: Node: a parent node
|
|
|
619 |
// width: String: width of surface, e.g., "100px"
|
|
|
620 |
// height: String: height of surface, e.g., "100px"
|
|
|
621 |
|
|
|
622 |
if(!width){ width = "100%"; }
|
|
|
623 |
if(!height){ height = "100%"; }
|
|
|
624 |
var s = new g.Surface(),
|
|
|
625 |
p = dojo.byId(parentNode),
|
|
|
626 |
c = p.ownerDocument.createElement("canvas");
|
|
|
627 |
c.width = width;
|
|
|
628 |
c.height = height;
|
|
|
629 |
p.appendChild(c);
|
|
|
630 |
s.rawNode = c;
|
|
|
631 |
s.surface = s;
|
|
|
632 |
return s; // dojox.gfx.Surface
|
|
|
633 |
};
|
|
|
634 |
|
|
|
635 |
// Extenders
|
|
|
636 |
|
|
|
637 |
var C = gs.Container, Container = {
|
|
|
638 |
add: function(shape){
|
|
|
639 |
this.surface.makeDirty();
|
|
|
640 |
return C.add.apply(this, arguments);
|
|
|
641 |
},
|
|
|
642 |
remove: function(shape, silently){
|
|
|
643 |
this.surface.makeDirty();
|
|
|
644 |
return C.remove.apply(this, arguments);
|
|
|
645 |
},
|
|
|
646 |
clear: function(){
|
|
|
647 |
this.surface.makeDirty();
|
|
|
648 |
return C.clear.apply(this, arguments);
|
|
|
649 |
},
|
|
|
650 |
_moveChildToFront: function(shape){
|
|
|
651 |
this.surface.makeDirty();
|
|
|
652 |
return C._moveChildToFront.apply(this, arguments);
|
|
|
653 |
},
|
|
|
654 |
_moveChildToBack: function(shape){
|
|
|
655 |
this.surface.makeDirty();
|
|
|
656 |
return C._moveChildToBack.apply(this, arguments);
|
|
|
657 |
}
|
|
|
658 |
};
|
|
|
659 |
|
|
|
660 |
dojo.mixin(gs.Creator, {
|
|
|
661 |
// summary: Canvas shape creators
|
|
|
662 |
createObject: function(shapeType, rawShape) {
|
|
|
663 |
// summary: creates an instance of the passed shapeType class
|
|
|
664 |
// shapeType: Function: a class constructor to create an instance of
|
|
|
665 |
// rawShape: Object: properties to be passed in to the classes "setShape" method
|
|
|
666 |
// overrideSize: Boolean: set the size explicitly, if true
|
|
|
667 |
var shape = new shapeType();
|
|
|
668 |
shape.surface = this.surface;
|
|
|
669 |
shape.setShape(rawShape);
|
|
|
670 |
this.add(shape);
|
|
|
671 |
return shape; // dojox.gfx.Shape
|
|
|
672 |
}
|
|
|
673 |
});
|
|
|
674 |
|
|
|
675 |
dojo.extend(g.Group, Container);
|
|
|
676 |
dojo.extend(g.Group, gs.Creator);
|
|
|
677 |
|
|
|
678 |
dojo.extend(g.Surface, Container);
|
|
|
679 |
dojo.extend(g.Surface, gs.Creator);
|
|
|
680 |
})();
|
|
|
681 |
|
|
|
682 |
}
|