canvas.js 24 KB

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