Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

excanvas.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. // Copyright 2006 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Known Issues: (From VML version)
  15. //
  16. // * Patterns are not implemented.
  17. // * Radial gradient are not implemented. The VML version of these look very
  18. // different from the canvas one.
  19. // * Coordsize. The width and height attribute have higher priority than the
  20. // width and height style values which isn't correct.
  21. // * Painting mode isn't implemented.
  22. // * Canvas width/height should is using content-box by default. IE in
  23. // Quirks mode will draw the canvas using border-box. Either change your
  24. // doctype to HTML5
  25. // (http://www.whatwg.org/specs/web-apps/current-work/#the-doctype)
  26. // or use Box Sizing Behavior from WebFX
  27. // (http://webfx.eae.net/dhtml/boxsizing/boxsizing.html)
  28. // * Optimize. There is always room for speed improvements.
  29. //Known Issues: Silverlight version
  30. //
  31. // * Doing a transformation during a path (ie lineTo, transform, lineTo) will
  32. // not work corerctly because the transform is done to the whole path (ie
  33. // transform, lineTo, lineTo)
  34. // * Patterns are not yet implemented.
  35. // only add this code if we do not already have a canvas implementation
  36. if (!window.CanvasRenderingContext2D) {
  37. (function () {
  38. var xamlId;
  39. var G_vmlCanvasManager_ = {
  40. init: function (opt_doc) {
  41. var doc = opt_doc || document;
  42. // Create a dummy element so that IE will allow canvas elements to be
  43. // recognized.
  44. doc.createElement('canvas');
  45. if (/MSIE/.test(navigator.userAgent) && !window.opera) {
  46. var self = this;
  47. createXamlScriptTag();
  48. doc.attachEvent('onreadystatechange', function () {
  49. self.init_(doc);
  50. });
  51. }
  52. },
  53. init_: function (doc) {
  54. // setup default css
  55. var ss = doc.createStyleSheet();
  56. ss.cssText = 'canvas{display:inline-block;overflow:hidden;' +
  57. // default size is 300x150 in Gecko and Opera
  58. 'text-align:left;width:300px;height:150px}' +
  59. 'canvas *{width:100%;height:100%;border:0;' +
  60. 'background:transparen;margin:0}' +
  61. 'canvas div {position:relative}' +
  62. // Place a div on top of the plugin.
  63. 'canvas div div{position:absolute;top:0;' +
  64. // needs to be "non transparent"
  65. 'filter:alpha(opacity=0);background:red}';
  66. // find all canvas elements
  67. var els = doc.getElementsByTagName('canvas');
  68. for (var i = 0; i < els.length; i++) {
  69. if (!els[i].getContext) {
  70. this.initElement(els[i]);
  71. }
  72. }
  73. },
  74. /**
  75. * Public initializes a canvas element so that it can be used as canvas
  76. * element from now on. This is called automatically before the page is
  77. * loaded but if you are creating elements using createElement you need to
  78. * make sure this is called on the element.
  79. * @param {HTMLElement} el The canvas element to initialize.
  80. * @return {HTMLElement} the element that was created.
  81. */
  82. initElement: function (el) {
  83. el.getContext = function () {
  84. if (this.context_) {
  85. return this.context_;
  86. }
  87. return this.context_ = new CanvasRenderingContext2D_(this);
  88. };
  89. var attrs = el.attributes;
  90. if (attrs.width && attrs.width.specified) {
  91. // TODO: use runtimeStyle and coordsize
  92. // el.getContext().setWidth_(attrs.width.nodeValue);
  93. el.style.width = attrs.width.nodeValue + 'px';
  94. } else {
  95. el.width = el.clientWidth;
  96. }
  97. if (attrs.height && attrs.height.specified) {
  98. // TODO: use runtimeStyle and coordsize
  99. // el.getContext().setHeight_(attrs.height.nodeValue);
  100. el.style.height = attrs.height.nodeValue + 'px';
  101. } else {
  102. el.height = el.clientHeight;
  103. }
  104. // insert object tag
  105. el.innerHTML = getObjectHtml();
  106. // do not use inline function because that will leak memory
  107. el.attachEvent('onpropertychange', onPropertyChange);
  108. return el;
  109. }
  110. };
  111. function onPropertyChange(e) {
  112. var el = e.srcElement;
  113. switch (e.propertyName) {
  114. case 'width':
  115. el.style.width = el.attributes.width.nodeValue + 'px';
  116. el.getContext().clearRect();
  117. break;
  118. case 'height':
  119. el.style.height = el.attributes.height.nodeValue + 'px';
  120. el.getContext().clearRect();
  121. break;
  122. }
  123. }
  124. G_vmlCanvasManager_.init();
  125. function createXamlScriptTag() {
  126. // This script tag contains the boilerplate XAML.
  127. document.write('<script type=text/xaml>' +
  128. '<Canvas x:Name="root" ' +
  129. 'xmlns="http://schemas.microsoft.com/client/2007" ' +
  130. 'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ' +
  131. 'Width="300" ' +
  132. 'Height="150" ' +
  133. 'Background="Transparent"> ' +
  134. '</Canvas>' +
  135. '</script>');
  136. // Find the id of the writtenscript file.
  137. var scripts = document.scripts;
  138. var script = scripts[scripts.length - 1];
  139. xamlId = script.uniqueID;
  140. script.id = xamlId;
  141. }
  142. function getObjectHtml(fn) {
  143. return '<div><object type="application/x-silverlight" >' +
  144. '<param name="windowless" value="true">' +
  145. '<param name="background" value="transparent">' +
  146. '<param name="source" value="#' + xamlId + '">' +
  147. '</object><div></div></div>';
  148. }
  149. function hasSilverlight() {
  150. try {
  151. new ActiveXObject('AgControl.AgControl');
  152. return true;
  153. } catch(_) {
  154. return false;
  155. }
  156. }
  157. // precompute "00" to "FF"
  158. var dec2hex = [];
  159. for (var i = 0; i < 16; i++) {
  160. for (var j = 0; j < 16; j++) {
  161. dec2hex[i * 16 + j] = i.toString(16) + j.toString(16);
  162. }
  163. }
  164. function createMatrixIdentity() {
  165. return [
  166. [1, 0, 0],
  167. [0, 1, 0],
  168. [0, 0, 1]
  169. ];
  170. }
  171. function matrixMultiply(m1, m2) {
  172. var result = createMatrixIdentity();
  173. for (var x = 0; x < 3; x++) {
  174. for (var y = 0; y < 3; y++) {
  175. var sum = 0;
  176. for (var z = 0; z < 3; z++) {
  177. sum += m1[x][z] * m2[z][y];
  178. }
  179. result[x][y] = sum;
  180. }
  181. }
  182. return result;
  183. }
  184. function doTransform(ctx) {
  185. transformObject(ctx, getRoot(ctx), ctx.m_);
  186. }
  187. function transformObject(ctx, obj, m) {
  188. var transform = obj.renderTransform;
  189. var matrix;
  190. if (!transform) {
  191. transform = create(ctx, '<MatrixTransform/>');
  192. matrix = create(ctx, '<Matrix/>');
  193. transform.matrix = matrix;
  194. obj.renderTransform = transform;
  195. } else {
  196. matrix = transform.matrix;
  197. }
  198. matrix.m11 = m[0][0];
  199. matrix.m12 = m[0][1];
  200. matrix.m21 = m[1][0];
  201. matrix.m22 = m[1][1];
  202. matrix.offsetX = m[2][0];
  203. matrix.offsetY = m[2][1];
  204. }
  205. function copyState(o1, o2) {
  206. o2.fillStyle = o1.fillStyle;
  207. o2.lineCap = o1.lineCap;
  208. o2.lineJoin = o1.lineJoin;
  209. o2.lineWidth = o1.lineWidth;
  210. o2.miterLimit = o1.miterLimit;
  211. o2.shadowBlur = o1.shadowBlur;
  212. o2.shadowColor = o1.shadowColor;
  213. o2.shadowOffsetX = o1.shadowOffsetX;
  214. o2.shadowOffsetY = o1.shadowOffsetY;
  215. o2.strokeStyle = o1.strokeStyle;
  216. o2.globalAlpha = o1.globalAlpha;
  217. o2.arcScaleX_ = o1.arcScaleX_;
  218. o2.arcScaleY_ = o1.arcScaleY_;
  219. }
  220. // precompute "00" to "FF"
  221. var decToHex = [];
  222. for (var i = 0; i < 16; i++) {
  223. for (var j = 0; j < 16; j++) {
  224. decToHex[i * 16 + j] = i.toString(16) + j.toString(16);
  225. }
  226. }
  227. // Silverlight does not support spelling gray as grey.
  228. var colorData = {
  229. darkgrey: '#A9A9A9',
  230. darkslategrey: '#2F4F4F',
  231. dimgrey: '#696969',
  232. grey: '#808080',
  233. lightgrey: '#D3D3D3',
  234. lightslategrey: '#778899',
  235. slategrey: '#708090'
  236. };
  237. function getRgbHslContent(styleString) {
  238. var start = styleString.indexOf('(', 3);
  239. var end = styleString.indexOf(')', start + 1);
  240. var parts = styleString.substring(start + 1, end).split(',');
  241. // add alpha if needed
  242. if (parts.length == 4 && styleString.substr(3, 1) == 'a') {
  243. alpha = +parts[3];
  244. } else {
  245. parts[3] = 1;
  246. }
  247. return parts;
  248. }
  249. function percent(s) {
  250. return parseFloat(s) / 100;
  251. }
  252. function clamp(v, min, max) {
  253. return Math.min(max, Math.max(min, v));
  254. }
  255. function hslToRgb(parts){
  256. var r, g, b;
  257. h = parseFloat(parts[0]) / 360 % 360;
  258. if (h < 0)
  259. h++;
  260. s = clamp(percent(parts[1]), 0, 1);
  261. l = clamp(percent(parts[2]), 0, 1);
  262. if (s == 0) {
  263. r = g = b = l; // achromatic
  264. } else {
  265. var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
  266. var p = 2 * l - q;
  267. r = hueToRgb(p, q, h + 1 / 3);
  268. g = hueToRgb(p, q, h);
  269. b = hueToRgb(p, q, h - 1 / 3);
  270. }
  271. return decToHex[Math.floor(r * 255)] +
  272. decToHex[Math.floor(g * 255)] +
  273. decToHex[Math.floor(b * 255)];
  274. }
  275. function hueToRgb(m1, m2, h) {
  276. if (h < 0)
  277. h++;
  278. if (h > 1)
  279. h--;
  280. if (6 * h < 1)
  281. return m1 + (m2 - m1) * 6 * h;
  282. else if (2 * h < 1)
  283. return m2;
  284. else if (3 * h < 2)
  285. return m1 + (m2 - m1) * (2 / 3 - h) * 6;
  286. else
  287. return m1;
  288. }
  289. function translateColor(styleString) {
  290. var str, alpha = 1;
  291. styleString = String(styleString);
  292. if (styleString.charAt(0) == '#') {
  293. return styleString;
  294. } else if (/^rgb/.test(styleString)) {
  295. var parts = getRgbHslContent(styleString);
  296. var str = '', n;
  297. for (var i = 0; i < 3; i++) {
  298. if (parts[i].indexOf('%') != -1) {
  299. n = Math.floor(percent(parts[i]) * 255);
  300. } else {
  301. n = +parts[i];
  302. }
  303. str += decToHex[clamp(n, 0, 255)];
  304. }
  305. alpha = parts[3];
  306. } else if (/^hsl/.test(styleString)) {
  307. var parts = getRgbHslContent(styleString);
  308. str = hslToRgb(parts);
  309. alpha = parts[3];
  310. } else if (styleString in colorData) {
  311. return colorData[styleString];
  312. } else {
  313. return styleString;
  314. }
  315. return '#' + dec2hex[Math.floor(alpha * 255)] + str;
  316. }
  317. function processLineCap(lineCap) {
  318. switch (lineCap) {
  319. case 'butt':
  320. return 'flat';
  321. case 'round':
  322. return 'round';
  323. case 'square':
  324. default:
  325. return 'square';
  326. }
  327. }
  328. function getRoot(ctx) {
  329. return ctx.canvas.firstChild.firstChild.content.findName('root');
  330. }
  331. function create(ctx, s, opt_args) {
  332. if (opt_args) {
  333. s = s.replace(/\%(\d+)/g, function(match, index) {
  334. return opt_args[+index - 1];
  335. });
  336. }
  337. try {
  338. return ctx.canvas.firstChild.firstChild.content.createFromXaml(s);
  339. } catch (ex) {
  340. throw Error('Could not create XAML from: ' + s);
  341. }
  342. }
  343. function drawShape(ctx, s, opt_args) {
  344. var canvas = ctx.lastCanvas_ || create(ctx, '<Canvas/>');
  345. var shape = create(ctx, s, opt_args);
  346. canvas.children.add(shape);
  347. transformObject(ctx, canvas, ctx.m_);
  348. if (!ctx.lastCanvas_) {
  349. getRoot(ctx).children.add(canvas);
  350. ctx.lastCanvas_ = canvas;
  351. }
  352. return shape;
  353. }
  354. function createBrushObject(ctx, value) {
  355. if (value instanceof CanvasGradient_) {
  356. return value.createBrush_(ctx);
  357. } else if (value instanceof CanvasPattern_) {
  358. throw Error('Not implemented');
  359. } else {
  360. return create(ctx, '<SolidColorBrush Color="%1"/>',
  361. [translateColor(value)]);
  362. }
  363. }
  364. /**
  365. * This class implements CanvasRenderingContext2D interface as described by
  366. * the WHATWG.
  367. * @param {HTMLElement} surfaceElement The element that the 2D context should
  368. * be associated with
  369. */
  370. function CanvasRenderingContext2D_(surfaceElement) {
  371. this.m_ = createMatrixIdentity();
  372. this.lastCanvas_ = null;
  373. this.mStack_ = [];
  374. this.aStack_ = [];
  375. this.currentPath_ = [];
  376. // Canvas context properties
  377. this.strokeStyle = '#000';
  378. this.fillStyle = '#000';
  379. this.lineWidth = 1;
  380. this.lineJoin = 'miter';
  381. this.lineCap = 'butt';
  382. this.miterLimit = 10;
  383. this.globalAlpha = 1;
  384. this.canvas = surfaceElement;
  385. };
  386. var contextPrototype = CanvasRenderingContext2D_.prototype;
  387. contextPrototype.clearRect = function() {
  388. var root = getRoot(this);
  389. root.children.clear();
  390. // TODO: Implement
  391. this.currentPath_ = [];
  392. this.lastCanvas_ = null;
  393. };
  394. contextPrototype.beginPath = function() {
  395. // TODO: Branch current matrix so that save/restore has no effect
  396. // as per safari docs.
  397. this.currentPath_ = [];
  398. };
  399. contextPrototype.moveTo = function(aX, aY) {
  400. this.currentPath_.push('M' + aX + ',' + aY);
  401. };
  402. contextPrototype.lineTo = function(aX, aY) {
  403. if (this.currentPath_.length == 0) return;
  404. this.currentPath_.push('L' + aX + ',' + aY);
  405. };
  406. contextPrototype.bezierCurveTo = function(aCP1x, aCP1y,
  407. aCP2x, aCP2y,
  408. aX, aY) {
  409. if (this.currentPath_.length == 0) return;
  410. this.currentPath_.push('C' + aCP1x + ',' + aCP1y + ' ' +
  411. aCP2x + ',' + aCP2y + ' ' +
  412. aX + ' ' + aY);
  413. };
  414. contextPrototype.quadraticCurveTo = function(aCPx, aCPy, aX, aY) {
  415. if (this.currentPath_.length == 0) return;
  416. this.currentPath_.push('Q' + aCPx + ',' + aCPy + ' ' +
  417. aX + ',' + aY);
  418. };
  419. contextPrototype.arcTo = function(x1, y1, x2, y2, radius) {
  420. if (this.currentPath_.length == 0) return;
  421. // TODO: Implement
  422. };
  423. contextPrototype.arc = function(aX, aY, aRadius,
  424. aStartAngle, aEndAngle, aClockwise) {
  425. var deltaAngle = Math.abs(aStartAngle - aEndAngle);
  426. // If start and stop are the same WebKit and Moz does nothing
  427. if (aStartAngle == aEndAngle) {
  428. // different browsers behave differently here so we do the easiest thing
  429. return;
  430. }
  431. var endX = aX + aRadius * Math.cos(aEndAngle);
  432. var endY = aY + aRadius * Math.sin(aEndAngle);
  433. if (deltaAngle >= 2 * Math.PI) {
  434. // if larger than 2PI
  435. this.arc(aX, aY, aRadius, aStartAngle, aStartAngle + Math.PI, aClockwise);
  436. this.arc(aX, aY, aRadius, aStartAngle + Math.PI,
  437. aStartAngle + 2 * Math.PI, aClockwise);
  438. // now move to end point
  439. this.moveTo(endX, endY);
  440. return;
  441. }
  442. var startX = aX + aRadius * Math.cos(aStartAngle);
  443. var startY = aY + aRadius * Math.sin(aStartAngle);
  444. var rotationAngle = deltaAngle * 180 / Math.PI; // sign, abs?
  445. var sweepDirection = aClockwise ? 0 : 1;
  446. var isLargeArc = rotationAngle >= 180 == Boolean(aClockwise) ? 0 : 1;
  447. if (this.currentPath_.length != 0) {
  448. // add line to start point
  449. this.lineTo(startX, startY);
  450. } else {
  451. this.moveTo(startX, startY);
  452. }
  453. this.currentPath_.push('A' + aRadius + ',' + aRadius + ' ' +
  454. rotationAngle + ' ' +
  455. isLargeArc + ' ' +
  456. sweepDirection + ' ' +
  457. endX + ',' + endY);
  458. };
  459. contextPrototype.rect = function(aX, aY, aWidth, aHeight) {
  460. this.moveTo(aX, aY);
  461. this.lineTo(aX + aWidth, aY);
  462. this.lineTo(aX + aWidth, aY + aHeight);
  463. this.lineTo(aX, aY + aHeight);
  464. this.closePath();
  465. };
  466. contextPrototype.strokeRect = function(aX, aY, aWidth, aHeight) {
  467. // Will destroy any existing path (same as FF behaviour)
  468. this.beginPath();
  469. this.moveTo(aX, aY);
  470. this.lineTo(aX + aWidth, aY);
  471. this.lineTo(aX + aWidth, aY + aHeight);
  472. this.lineTo(aX, aY + aHeight);
  473. this.closePath();
  474. this.stroke();
  475. this.currentPath_ = [];
  476. };
  477. contextPrototype.fillRect = function(aX, aY, aWidth, aHeight) {
  478. // Will destroy any existing path (same as FF behaviour)
  479. this.beginPath();
  480. this.moveTo(aX, aY);
  481. this.lineTo(aX + aWidth, aY);
  482. this.lineTo(aX + aWidth, aY + aHeight);
  483. this.lineTo(aX, aY + aHeight);
  484. this.closePath();
  485. this.fill();
  486. this.currentPath_ = [];
  487. };
  488. contextPrototype.createLinearGradient = function(aX0, aY0, aX1, aY1) {
  489. return new LinearCanvasGradient_(aX0, aY0, aX1, aY1);
  490. };
  491. contextPrototype.createRadialGradient = function(x0, y0,
  492. r0, x1,
  493. y1, r1) {
  494. return new RadialCanvasGradient_(x0, y0, r0, x1, y1, r1);
  495. };
  496. contextPrototype.drawImage = function (image, var_args) {
  497. var dx, dy, dw, dh, sx, sy, sw, sh;
  498. // For Silverlight we don't need to get the size of the image since
  499. // Silverlight uses the image original dimension if not provided.
  500. if (arguments.length == 3) {
  501. dx = arguments[1];
  502. dy = arguments[2];
  503. // Keep sx, sy, sw, dw, sh and dh undefined
  504. } else if (arguments.length == 5) {
  505. dx = arguments[1];
  506. dy = arguments[2];
  507. dw = arguments[3];
  508. dh = arguments[4];
  509. // Keep sx, sy, sw and sh undefined
  510. } else if (arguments.length == 9) {
  511. sx = arguments[1];
  512. sy = arguments[2];
  513. sw = arguments[3];
  514. sh = arguments[4];
  515. dx = arguments[5];
  516. dy = arguments[6];
  517. dw = arguments[7];
  518. dh = arguments[8];
  519. } else {
  520. throw Error('Invalid number of arguments');
  521. }
  522. var slImage;
  523. // If we have a source rect we need to clip the image.
  524. if (arguments.length == 9) {
  525. slImage = drawShape(this, '<Image Source="%1"/>', [image.src]);
  526. var clipRect = create(this,
  527. '<RectangleGeometry Rect="%1,%2,%3,%4"/>', [sx, sy, sw, sh]);
  528. slImage.clip = clipRect;
  529. var m = createMatrixIdentity();
  530. // translate to 0,0
  531. m[2][0] = -sx;
  532. m[2][1] = -sy;
  533. // scale
  534. var m2 = createMatrixIdentity();
  535. m2[0][0] = dw / sw;
  536. m2[1][1] = dh / sh;
  537. m = matrixMultiply(m, m2);
  538. // translate to destination
  539. m[2][0] += dx;
  540. m[2][1] += dy;
  541. transformObject(this, slImage, m);
  542. } else {
  543. slImage = drawShape(this,
  544. '<Image Source="%1" Canvas.Left="%2" Canvas.Top="%3"/>',
  545. [image.src, dx, dy]);
  546. if (dw != undefined || dh != undefined) {
  547. slImage.width = dw;
  548. slImage.height = dh;
  549. slImage.stretch = 'fill';
  550. }
  551. }
  552. };
  553. contextPrototype.stroke = function() {
  554. if (this.currentPath_.length == 0) return;
  555. var path = drawShape(this, '<Path Data="%1"/>',
  556. [this.currentPath_.join(' ')]);
  557. path.stroke = createBrushObject(this, this.strokeStyle);
  558. path.opacity = this.globalAlpha;
  559. path.strokeThickness = this.lineWidth;
  560. path.strokeMiterLimit = this.miterLimit;
  561. path.strokeLineJoin = this.lineJoin;
  562. // Canvas does not differentiate start from end
  563. path.strokeEndLineCap = path.strokeStartLineCap =
  564. processLineCap(this.lineCap);
  565. };
  566. contextPrototype.fill = function() {
  567. if (this.currentPath_.length == 0) return;
  568. var path = drawShape(this, '<Path Data="%1"/>',
  569. [this.currentPath_.join(' ')]);
  570. // The spec says to use non zero but Silverlight uses EvenOdd by defaul
  571. path.data.fillRule = 'NonZero';
  572. path.fill = createBrushObject(this, this.fillStyle);
  573. path.fill.opacity = this.globalAlpha;
  574. // TODO: What about even-odd etc?
  575. };
  576. contextPrototype.closePath = function() {
  577. this.currentPath_.push('z');
  578. };
  579. /**
  580. * Sets the transformation matrix and marks things as dirty
  581. */
  582. function setM(self, m) {
  583. self.m_ = m;
  584. self.lastCanvas_ = null;
  585. };
  586. contextPrototype.save = function() {
  587. var o = {};
  588. copyState(this, o);
  589. this.aStack_.push(o);
  590. this.mStack_.push(this.m_);
  591. setM(this, matrixMultiply(createMatrixIdentity(), this.m_));
  592. };
  593. contextPrototype.restore = function() {
  594. if (this.aStack_.length) {
  595. copyState(this.aStack_.pop(), this);
  596. this.m_ = this.mStack_.pop();
  597. }
  598. };
  599. contextPrototype.translate = function(aX, aY) {
  600. var m1 = [
  601. [1, 0, 0],
  602. [0, 1, 0],
  603. [aX, aY, 1]
  604. ];
  605. setM(this, matrixMultiply(m1, this.m_));
  606. };
  607. contextPrototype.rotate = function(aRot) {
  608. var c = Math.cos(aRot);
  609. var s = Math.sin(aRot);
  610. var m1 = [
  611. [c, s, 0],
  612. [-s, c, 0],
  613. [0, 0, 1]
  614. ];
  615. setM(this, matrixMultiply(m1, this.m_));
  616. };
  617. contextPrototype.scale = function(aX, aY) {
  618. var m1 = [
  619. [aX, 0, 0],
  620. [0, aY, 0],
  621. [0, 0, 1]
  622. ];
  623. setM(this, matrixMultiply(m1, this.m_));
  624. };
  625. contextPrototype.transform = function(m11, m12, m21, m22, dx, dy) {
  626. var m1 = [
  627. [m11, m12, 0],
  628. [m21, m22, 0],
  629. [ dx, dy, 1]
  630. ];
  631. setM(this, matrixMultiply(m1, this.m_));
  632. };
  633. contextPrototype.setTransform = function(m11, m12, m21, m22, dx, dy) {
  634. setM(this, [
  635. [m11, m12, 0],
  636. [m21, m22, 0],
  637. [ dx, dy, 1],
  638. ]);
  639. };
  640. contextPrototype.clip = function() {
  641. if (this.currentPath_.length) {
  642. var clip = this.currentPath_.join(' ');
  643. var canvas = create(this, '<Canvas Width="%1" Height="%2" Clip="%3"/>',
  644. [getRoot(this).width, getRoot(this).height, clip]);
  645. var parent = this.lastCanvas_ || getRoot(this);
  646. parent.children.add(canvas);
  647. this.lastCanvas_ = canvas;
  648. }
  649. };
  650. contextPrototype.createPattern = function() {
  651. return new CanvasPattern_;
  652. };
  653. // Gradient / Pattern Stubs
  654. function CanvasGradient_() {
  655. this.colors_ = [];
  656. }
  657. CanvasGradient_.prototype.addColorStop = function(aOffset, aColor) {
  658. aColor = translateColor(aColor);
  659. this.colors_.push({offset: aOffset, color: aColor});
  660. };
  661. CanvasGradient_.prototype.createStops_ = function(ctx, brushObj, colors) {
  662. var gradientStopCollection = brushObj.gradientStops;
  663. for (var i = 0, c; c = colors[i]; i++) {
  664. var color = translateColor(c.color);
  665. gradientStopCollection.add(create(ctx,
  666. '<GradientStop Color="%1" Offset="%2"/>', [color, c.offset]));
  667. }
  668. };
  669. function LinearCanvasGradient_(x0, y0, x1, y1) {
  670. CanvasGradient_.call(this);
  671. this.x0_ = x0;
  672. this.y0_ = y0;
  673. this.x1_ = x1;
  674. this.y1_ = y1;
  675. }
  676. LinearCanvasGradient_.prototype = new CanvasGradient_;
  677. LinearCanvasGradient_.prototype.createBrush_ = function(ctx) {
  678. var brushObj = create(ctx, '<LinearGradientBrush MappingMode="Absolute" ' +
  679. 'StartPoint="%1,%2" EndPoint="%3,%4"/>',
  680. [this.x0_, this.y0_, this.x1_, this.y1_]);
  681. this.createStops_(ctx, brushObj, this.colors_);
  682. return brushObj;
  683. };
  684. function isNanOrInfinite(v) {
  685. return isNaN(v) || !isFinite(v);
  686. }
  687. function RadialCanvasGradient_(x0, y0, r0, x1, y1, r1) {
  688. if (r0 < 0 || r1 < 0 || isNanOrInfinite(x0) || isNanOrInfinite(y0) ||
  689. isNanOrInfinite(x1) || isNanOrInfinite(y1)) {
  690. // IE does not support DOMException so this is as close as we get.
  691. var error = Error('DOMException.INDEX_SIZE_ERR');
  692. error.code = 1;
  693. throw error;
  694. }
  695. CanvasGradient_.call(this);
  696. this.x0_ = x0;
  697. this.y0_ = y0;
  698. this.r0_ = r0;
  699. this.x1_ = x1;
  700. this.y1_ = y1;
  701. this.r1_ = r1;
  702. }
  703. RadialCanvasGradient_.prototype = new CanvasGradient_;
  704. CanvasGradient_.prototype.createBrush_ = function(ctx) {
  705. if (this.x0_ == this.x1_ && this.y0_ == this.y1_ && this.r0_ == this.r1_) {
  706. return null;
  707. }
  708. var radius = Math.max(this.r0_, this.r1_);
  709. var minRadius = Math.min(this.r0_, this.r1_);
  710. var brushObj = create(ctx, '<RadialGradientBrush MappingMode="Absolute" ' +
  711. 'GradientOrigin="%1,%2" Center="%3,%4" ' +
  712. 'RadiusX="%5" RadiusY="%5"/>',
  713. [this.x0_, this.y0_, this.x1_, this.y1_, radius]);
  714. var colors = this.colors_.concat();
  715. if (this.r1_ < this.r0_) {
  716. // reverse color stop array
  717. colors.reverse();
  718. for (var i = 0, c; c = colors[i]; i++) {
  719. c.offset = 1 - c.offset;
  720. }
  721. }
  722. // sort the color stops
  723. colors.sort(function(c1, c2) {
  724. return c1.offset - c2.offset;
  725. });
  726. if (minRadius > 0) {
  727. // We need to adjust the color stops since SL always have the inner radius
  728. // at (0, 0) so we change the stops in case the min radius is not 0.
  729. for (var i = 0, c; c = colors[i]; i++) {
  730. c.offset = minRadius / radius + (radius - minRadius) / radius * c.offset;
  731. }
  732. }
  733. this.createStops_(ctx, brushObj, colors);
  734. return brushObj;
  735. };
  736. function CanvasPattern_() {}
  737. // set up externs
  738. G_vmlCanvasManager = G_vmlCanvasManager_;
  739. CanvasRenderingContext2D = CanvasRenderingContext2D_;
  740. CanvasGradient = CanvasGradient_;
  741. CanvasPattern = CanvasPattern_;
  742. })();
  743. } // if