canvas.js 25 KB

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