1 // Copyright 2006 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 // * Patterns only support repeat.
19 // * Radial gradient are not implemented. The VML version of these look very
20 // different from the canvas one.
21 // * Clipping paths are not implemented.
22 // * Coordsize. The width and height attribute have higher priority than the
23 // width and height style values which isn't correct.
24 // * Painting mode isn't implemented.
25 // * Canvas width/height should is using content-box by default. IE in
26 // Quirks mode will draw the canvas using border-box. Either change your
28 // (http://www.whatwg.org/specs/web-apps/current-work/#the-doctype)
29 // or use Box Sizing Behavior from WebFX
30 // (http://webfx.eae.net/dhtml/boxsizing/boxsizing.html)
31 // * Non uniform scaling does not correctly scale strokes.
32 // * Optimize. There is always room for speed improvements.
34 // Only add this code if we do not already have a canvas implementation
35 if (!document.createElement('canvas').getContext) {
39 // alias some functions to make (compiled) code shorter
47 // this is used for sub pixel precision
51 var IE_VERSION = +navigator.userAgent.match(/MSIE ([\d.]+)?/)[1];
54 * This funtion is assigned to the <canvas> elements as element.getContext().
56 * @return {CanvasRenderingContext2D_}
58 function getContext() {
59 return this.context_ ||
60 (this.context_ = new CanvasRenderingContext2D_(this));
63 var slice = Array.prototype.slice;
66 * Binds a function to an object. The returned function will always use the
67 * passed in {@code obj} as {@code this}.
71 * g = bind(f, obj, a, b)
72 * g(c, d) // will do f.call(obj, a, b, c, d)
74 * @param {Function} f The function to bind the object to
75 * @param {Object} obj The object that should act as this when the function
77 * @param {*} var_args Rest arguments that will be used as the initial
78 * arguments when the function is called
79 * @return {Function} A new function that has bound this
81 function bind(f, obj, var_args) {
82 var a = slice.call(arguments, 2);
84 return f.apply(obj, a.concat(slice.call(arguments)));
88 function encodeHtmlAttribute(s) {
89 return String(s).replace(/&/g, '&').replace(/"/g, '"');
92 function addNamespace(doc, prefix, urn) {
93 if (!doc.namespaces[prefix]) {
94 doc.namespaces.add(prefix, urn, '#default#VML');
98 function addNamespacesAndStylesheet(doc) {
99 addNamespace(doc, 'g_vml_', 'urn:schemas-microsoft-com:vml');
100 addNamespace(doc, 'g_o_', 'urn:schemas-microsoft-com:office:office');
102 // Setup default CSS. Only add one style sheet per document
103 if (!doc.styleSheets['ex_canvas_']) {
104 var ss = doc.createStyleSheet();
105 ss.owningElement.id = 'ex_canvas_';
106 ss.cssText = 'canvas{display:inline-block;overflow:hidden;' +
107 // default size is 300x150 in Gecko and Opera
108 'text-align:left;width:300px;height:150px}';
112 // Add namespaces and stylesheet at startup.
113 addNamespacesAndStylesheet(document);
115 var G_vmlCanvasManager_ = {
116 init: function (opt_doc) {
117 var doc = opt_doc || document;
118 // Create a dummy element so that IE will allow canvas elements to be
120 doc.createElement('canvas');
121 doc.attachEvent('onreadystatechange', bind(this.init_, this, doc));
124 init_: function (doc) {
125 // find all canvas elements
126 var els = doc.getElementsByTagName('canvas');
127 for (var i = 0; i < els.length; i++) {
128 this.initElement(els[i]);
133 * Public initializes a canvas element so that it can be used as canvas
134 * element from now on. This is called automatically before the page is
135 * loaded but if you are creating elements using createElement you need to
136 * make sure this is called on the element.
137 * @param {HTMLElement} el The canvas element to initialize.
138 * @return {HTMLElement} the element that was created.
140 initElement: function (el) {
141 if (!el.getContext) {
142 el.getContext = getContext;
144 // Add namespaces and stylesheet to document of the element.
145 addNamespacesAndStylesheet(el.ownerDocument);
147 // Remove fallback content. There is no way to hide text nodes so we
148 // just remove all childNodes. We could hide all elements and remove
149 // text nodes but who really cares about the fallback content.
152 // do not use inline function because that will leak memory
153 el.attachEvent('onpropertychange', onPropertyChange);
154 el.attachEvent('onresize', onResize);
156 var attrs = el.attributes;
157 if (attrs.width && attrs.width.specified) {
158 // TODO: use runtimeStyle and coordsize
159 // el.getContext().setWidth_(attrs.width.nodeValue);
160 el.style.width = attrs.width.nodeValue + 'px';
162 el.width = el.clientWidth;
164 if (attrs.height && attrs.height.specified) {
165 // TODO: use runtimeStyle and coordsize
166 // el.getContext().setHeight_(attrs.height.nodeValue);
167 el.style.height = attrs.height.nodeValue + 'px';
169 el.height = el.clientHeight;
171 //el.getContext().setCoordsize_()
177 function onPropertyChange(e) {
178 var el = e.srcElement;
180 switch (e.propertyName) {
182 el.getContext().clearRect();
183 el.style.width = el.attributes.width.nodeValue + 'px';
184 // In IE8 this does not trigger onresize.
185 el.firstChild.style.width = el.clientWidth + 'px';
188 el.getContext().clearRect();
189 el.style.height = el.attributes.height.nodeValue + 'px';
190 el.firstChild.style.height = el.clientHeight + 'px';
195 function onResize(e) {
196 var el = e.srcElement;
198 el.firstChild.style.width = el.clientWidth + 'px';
199 el.firstChild.style.height = el.clientHeight + 'px';
203 G_vmlCanvasManager_.init();
205 // precompute "00" to "FF"
207 for (var i = 0; i < 16; i++) {
208 for (var j = 0; j < 16; j++) {
209 decToHex[i * 16 + j] = i.toString(16) + j.toString(16);
213 function createMatrixIdentity() {
221 function matrixMultiply(m1, m2) {
222 var result = createMatrixIdentity();
224 for (var x = 0; x < 3; x++) {
225 for (var y = 0; y < 3; y++) {
228 for (var z = 0; z < 3; z++) {
229 sum += m1[x][z] * m2[z][y];
238 function copyState(o1, o2) {
239 o2.fillStyle = o1.fillStyle;
240 o2.lineCap = o1.lineCap;
241 o2.lineJoin = o1.lineJoin;
242 o2.lineWidth = o1.lineWidth;
243 o2.miterLimit = o1.miterLimit;
244 o2.shadowBlur = o1.shadowBlur;
245 o2.shadowColor = o1.shadowColor;
246 o2.shadowOffsetX = o1.shadowOffsetX;
247 o2.shadowOffsetY = o1.shadowOffsetY;
248 o2.strokeStyle = o1.strokeStyle;
249 o2.globalAlpha = o1.globalAlpha;
251 o2.textAlign = o1.textAlign;
252 o2.textBaseline = o1.textBaseline;
253 o2.arcScaleX_ = o1.arcScaleX_;
254 o2.arcScaleY_ = o1.arcScaleY_;
255 o2.lineScale_ = o1.lineScale_;
259 // aliceblue: '#F0F8FF',
260 // antiquewhite: '#FAEBD7',
261 // aquamarine: '#7FFFD4',
264 // bisque: '#FFE4C4',
266 // blanchedalmond: '#FFEBCD',
267 // blueviolet: '#8A2BE2',
269 // burlywood: '#DEB887',
270 // cadetblue: '#5F9EA0',
271 // chartreuse: '#7FFF00',
272 // chocolate: '#D2691E',
274 // cornflowerblue: '#6495ED',
275 // cornsilk: '#FFF8DC',
276 // crimson: '#DC143C',
278 // darkblue: '#00008B',
279 // darkcyan: '#008B8B',
280 // darkgoldenrod: '#B8860B',
281 // darkgray: '#A9A9A9',
282 // darkgreen: '#006400',
283 // darkgrey: '#A9A9A9',
284 // darkkhaki: '#BDB76B',
285 // darkmagenta: '#8B008B',
286 // darkolivegreen: '#556B2F',
287 // darkorange: '#FF8C00',
288 // darkorchid: '#9932CC',
289 // darkred: '#8B0000',
290 // darksalmon: '#E9967A',
291 // darkseagreen: '#8FBC8F',
292 // darkslateblue: '#483D8B',
293 // darkslategray: '#2F4F4F',
294 // darkslategrey: '#2F4F4F',
295 // darkturquoise: '#00CED1',
296 // darkviolet: '#9400D3',
297 // deeppink: '#FF1493',
298 // deepskyblue: '#00BFFF',
299 // dimgray: '#696969',
300 // dimgrey: '#696969',
301 // dodgerblue: '#1E90FF',
302 // firebrick: '#B22222',
303 // floralwhite: '#FFFAF0',
304 // forestgreen: '#228B22',
305 // gainsboro: '#DCDCDC',
306 // ghostwhite: '#F8F8FF',
308 // goldenrod: '#DAA520',
310 // greenyellow: '#ADFF2F',
311 // honeydew: '#F0FFF0',
312 // hotpink: '#FF69B4',
313 // indianred: '#CD5C5C',
314 // indigo: '#4B0082',
317 // lavender: '#E6E6FA',
318 // lavenderblush: '#FFF0F5',
319 // lawngreen: '#7CFC00',
320 // lemonchiffon: '#FFFACD',
321 // lightblue: '#ADD8E6',
322 // lightcoral: '#F08080',
323 // lightcyan: '#E0FFFF',
324 // lightgoldenrodyellow: '#FAFAD2',
325 // lightgreen: '#90EE90',
326 // lightgrey: '#D3D3D3',
327 // lightpink: '#FFB6C1',
328 // lightsalmon: '#FFA07A',
329 // lightseagreen: '#20B2AA',
330 // lightskyblue: '#87CEFA',
331 // lightslategray: '#778899',
332 // lightslategrey: '#778899',
333 // lightsteelblue: '#B0C4DE',
334 // lightyellow: '#FFFFE0',
335 // limegreen: '#32CD32',
337 // magenta: '#FF00FF',
338 // mediumaquamarine: '#66CDAA',
339 // mediumblue: '#0000CD',
340 // mediumorchid: '#BA55D3',
341 // mediumpurple: '#9370DB',
342 // mediumseagreen: '#3CB371',
343 // mediumslateblue: '#7B68EE',
344 // mediumspringgreen: '#00FA9A',
345 // mediumturquoise: '#48D1CC',
346 // mediumvioletred: '#C71585',
347 // midnightblue: '#191970',
348 // mintcream: '#F5FFFA',
349 // mistyrose: '#FFE4E1',
350 // moccasin: '#FFE4B5',
351 // navajowhite: '#FFDEAD',
352 // oldlace: '#FDF5E6',
353 // olivedrab: '#6B8E23',
354 // orange: '#FFA500',
355 // orangered: '#FF4500',
356 // orchid: '#DA70D6',
357 // palegoldenrod: '#EEE8AA',
358 // palegreen: '#98FB98',
359 // paleturquoise: '#AFEEEE',
360 // palevioletred: '#DB7093',
361 // papayawhip: '#FFEFD5',
362 // peachpuff: '#FFDAB9',
366 // powderblue: '#B0E0E6',
367 // rosybrown: '#BC8F8F',
368 // royalblue: '#4169E1',
369 // saddlebrown: '#8B4513',
370 // salmon: '#FA8072',
371 // sandybrown: '#F4A460',
372 // seagreen: '#2E8B57',
373 // seashell: '#FFF5EE',
374 // sienna: '#A0522D',
375 // skyblue: '#87CEEB',
376 // slateblue: '#6A5ACD',
377 // slategray: '#708090',
378 // slategrey: '#708090',
380 // springgreen: '#00FF7F',
381 // steelblue: '#4682B4',
383 // thistle: '#D8BFD8',
384 // tomato: '#FF6347',
385 // turquoise: '#40E0D0',
386 // violet: '#EE82EE',
388 // whitesmoke: '#F5F5F5',
389 // yellowgreen: '#9ACD32'
393 function getRgbHslContent(styleString) {
394 var start = styleString.indexOf('(', 3);
395 var end = styleString.indexOf(')', start + 1);
396 var parts = styleString.substring(start + 1, end).split(',');
397 // add alpha if needed
398 if (parts.length != 4 || styleString.charAt(3) != 'a') {
404 function percent(s) {
405 return parseFloat(s) / 100;
408 function clamp(v, min, max) {
409 return Math.min(max, Math.max(min, v));
412 function hslToRgb(parts) {
413 var r, g, b, h, s, l;
414 h = parseFloat(parts[0]) / 360 % 360;
417 s = clamp(percent(parts[1]), 0, 1);
418 l = clamp(percent(parts[2]), 0, 1);
420 r = g = b = l; // achromatic
422 var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
424 r = hueToRgb(p, q, h + 1 / 3);
425 g = hueToRgb(p, q, h);
426 b = hueToRgb(p, q, h - 1 / 3);
429 return '#' + decToHex[Math.floor(r * 255)] +
430 decToHex[Math.floor(g * 255)] +
431 decToHex[Math.floor(b * 255)];
434 function hueToRgb(m1, m2, h) {
441 return m1 + (m2 - m1) * 6 * h;
445 return m1 + (m2 - m1) * (2 / 3 - h) * 6;
450 var processStyleCache = {};
452 function processStyle(styleString) {
453 if (styleString in processStyleCache) {
454 return processStyleCache[styleString];
459 styleString = String(styleString);
460 if (styleString.charAt(0) == '#') {
462 } else if (/^rgb/.test(styleString)) {
463 var parts = getRgbHslContent(styleString);
465 for (var i = 0; i < 3; i++) {
466 if (parts[i].indexOf('%') != -1) {
467 n = Math.floor(percent(parts[i]) * 255);
471 str += decToHex[clamp(n, 0, 255)];
474 } else if (/^hsl/.test(styleString)) {
475 var parts = getRgbHslContent(styleString);
476 str = hslToRgb(parts);
479 str = /*colorData[styleString] ||*/styleString;
481 return processStyleCache[styleString] = { color: str, alpha: alpha };
484 var DEFAULT_STYLE = {
492 // Internal text style cache
493 // var fontStyleCache = {};
495 // function processFontStyle(styleString) {
496 // if (fontStyleCache[styleString]) {
497 // return fontStyleCache[styleString];
500 // var el = document.createElement('div');
501 // var style = el.style;
503 // style.font = styleString;
505 // // Ignore failures to set to invalid font.
508 // return fontStyleCache[styleString] = {
509 // style: style.fontStyle || DEFAULT_STYLE.style,
510 // variant: style.fontVariant || DEFAULT_STYLE.variant,
511 // weight: style.fontWeight || DEFAULT_STYLE.weight,
512 // size: style.fontSize || DEFAULT_STYLE.size,
513 // family: style.fontFamily || DEFAULT_STYLE.family
517 // function getComputedStyle(style, element) {
518 // var computedStyle = {};
520 // for (var p in style) {
521 // computedStyle[p] = style[p];
524 // // Compute the size
525 // var canvasFontSize = parseFloat(element.currentStyle.fontSize),
526 // fontSize = parseFloat(style.size);
528 // if (typeof style.size == 'number') {
529 // computedStyle.size = style.size;
530 // } else if (style.size.indexOf('px') != -1) {
531 // computedStyle.size = fontSize;
532 // } else if (style.size.indexOf('em') != -1) {
533 // computedStyle.size = canvasFontSize * fontSize;
534 // } else if(style.size.indexOf('%') != -1) {
535 // computedStyle.size = (canvasFontSize / 100) * fontSize;
536 // } else if (style.size.indexOf('pt') != -1) {
537 // computedStyle.size = fontSize / .75;
539 // computedStyle.size = canvasFontSize;
542 // // Different scaling between normal text and VML text. This was found using
543 // // trial and error to get the same size as non VML text.
544 // computedStyle.size *= 0.981;
546 // return computedStyle;
549 // function buildStyle(style) {
550 // return style.style + ' ' + style.variant + ' ' + style.weight + ' ' +
551 // style.size + 'px ' + style.family;
559 function processLineCap(lineCap) {
560 return lineCapMap[lineCap] || 'square';
564 * This class implements CanvasRenderingContext2D interface as described by
566 * @param {HTMLElement} canvasElement The element that the 2D context should
569 function CanvasRenderingContext2D_(canvasElement) {
570 this.m_ = createMatrixIdentity();
574 this.currentPath_ = [];
576 // Canvas context properties
577 this.strokeStyle = '#000';
578 this.fillStyle = '#000';
581 this.lineJoin = 'miter';
582 this.lineCap = 'butt';
583 this.miterLimit = Z * 1;
584 this.globalAlpha = 1;
585 //this.font = '10px sans-serif';
586 //this.textAlign = 'left';
587 //this.textBaseline = 'alphabetic';
588 this.canvas = canvasElement;
590 var cssText = 'width:' + canvasElement.clientWidth + 'px;height:' +
591 canvasElement.clientHeight + 'px;overflow:hidden;position:absolute';
592 var el = canvasElement.ownerDocument.createElement('div');
593 el.style.cssText = cssText;
594 canvasElement.appendChild(el);
596 var overlayEl = el.cloneNode(false);
597 // Use a non transparent background.
598 overlayEl.style.backgroundColor = 'red';
599 overlayEl.style.filter = 'alpha(opacity=0)';
600 canvasElement.appendChild(overlayEl);
608 var contextPrototype = CanvasRenderingContext2D_.prototype;
609 contextPrototype.clearRect = function () {
610 if (this.textMeasureEl_) {
611 this.textMeasureEl_.removeNode(true);
612 this.textMeasureEl_ = null;
614 this.element_.innerHTML = '';
617 contextPrototype.beginPath = function () {
618 // TODO: Branch current matrix so that save/restore has no effect
619 // as per safari docs.
620 this.currentPath_ = [];
623 contextPrototype.moveTo = function (aX, aY) {
624 var p = getCoords(this, aX, aY);
625 this.currentPath_.push({ type: 'moveTo', x: p.x, y: p.y });
626 this.currentX_ = p.x;
627 this.currentY_ = p.y;
630 contextPrototype.lineTo = function (aX, aY) {
631 var p = getCoords(this, aX, aY);
632 this.currentPath_.push({ type: 'lineTo', x: p.x, y: p.y });
634 this.currentX_ = p.x;
635 this.currentY_ = p.y;
638 contextPrototype.bezierCurveTo = function(aCP1x, aCP1y,
641 var p = getCoords(this, aX, aY);
642 var cp1 = getCoords(this, aCP1x, aCP1y);
643 var cp2 = getCoords(this, aCP2x, aCP2y);
644 bezierCurveTo(this, cp1, cp2, p);
647 // Helper function that takes the already fixed cordinates.
648 function bezierCurveTo(self, cp1, cp2, p) {
649 self.currentPath_.push({
650 type: 'bezierCurveTo',
658 self.currentX_ = p.x;
659 self.currentY_ = p.y;
662 contextPrototype.quadraticCurveTo = function(aCPx, aCPy, aX, aY) {
663 // the following is lifted almost directly from
664 // http://developer.mozilla.org/en/docs/Canvas_tutorial:Drawing_shapes
666 var cp = getCoords(this, aCPx, aCPy);
667 var p = getCoords(this, aX, aY);
670 x: this.currentX_ + 2.0 / 3.0 * (cp.x - this.currentX_),
671 y: this.currentY_ + 2.0 / 3.0 * (cp.y - this.currentY_)
674 x: cp1.x + (p.x - this.currentX_) / 3.0,
675 y: cp1.y + (p.y - this.currentY_) / 3.0
678 bezierCurveTo(this, cp1, cp2, p);
681 contextPrototype.arc = function (aX, aY, aRadius,
682 aStartAngle, aEndAngle, aClockwise) {
684 var arcType = aClockwise ? 'at' : 'wa';
686 var xStart = aX + mc(aStartAngle) * aRadius - Z2;
687 var yStart = aY + ms(aStartAngle) * aRadius - Z2;
689 var xEnd = aX + mc(aEndAngle) * aRadius - Z2;
690 var yEnd = aY + ms(aEndAngle) * aRadius - Z2;
692 // IE won't render arches drawn counter clockwise if xStart == xEnd.
693 if (xStart == xEnd && !aClockwise) {
694 xStart += 0.125; // Offset xStart by 1/80 of a pixel. Use something
695 // that can be represented in binary
698 var p = getCoords(this, aX, aY);
699 var pStart = getCoords(this, xStart, yStart);
700 var pEnd = getCoords(this, xEnd, yEnd);
702 this.currentPath_.push({ type: arcType,
714 // contextPrototype.rect = function(aX, aY, aWidth, aHeight) {
715 // this.moveTo(aX, aY);
716 // this.lineTo(aX + aWidth, aY);
717 // this.lineTo(aX + aWidth, aY + aHeight);
718 // this.lineTo(aX, aY + aHeight);
722 // contextPrototype.strokeRect = function(aX, aY, aWidth, aHeight) {
723 // var oldPath = this.currentPath_;
726 // this.moveTo(aX, aY);
727 // this.lineTo(aX + aWidth, aY);
728 // this.lineTo(aX + aWidth, aY + aHeight);
729 // this.lineTo(aX, aY + aHeight);
733 // this.currentPath_ = oldPath;
736 // contextPrototype.fillRect = function(aX, aY, aWidth, aHeight) {
737 // var oldPath = this.currentPath_;
740 // this.moveTo(aX, aY);
741 // this.lineTo(aX + aWidth, aY);
742 // this.lineTo(aX + aWidth, aY + aHeight);
743 // this.lineTo(aX, aY + aHeight);
747 // this.currentPath_ = oldPath;
750 // contextPrototype.createLinearGradient = function(aX0, aY0, aX1, aY1) {
751 // var gradient = new CanvasGradient_('gradient');
752 // gradient.x0_ = aX0;
753 // gradient.y0_ = aY0;
754 // gradient.x1_ = aX1;
755 // gradient.y1_ = aY1;
759 // contextPrototype.createRadialGradient = function(aX0, aY0, aR0,
761 // var gradient = new CanvasGradient_('gradientradial');
762 // gradient.x0_ = aX0;
763 // gradient.y0_ = aY0;
764 // gradient.r0_ = aR0;
765 // gradient.x1_ = aX1;
766 // gradient.y1_ = aY1;
767 // gradient.r1_ = aR1;
771 // contextPrototype.drawImage = function(image, var_args) {
772 // var dx, dy, dw, dh, sx, sy, sw, sh;
774 // // to find the original width we overide the width and height
775 // var oldRuntimeWidth = image.runtimeStyle.width;
776 // var oldRuntimeHeight = image.runtimeStyle.height;
777 // image.runtimeStyle.width = 'auto';
778 // image.runtimeStyle.height = 'auto';
780 // // get the original size
781 // var w = image.width;
782 // var h = image.height;
784 // // and remove overides
785 // image.runtimeStyle.width = oldRuntimeWidth;
786 // image.runtimeStyle.height = oldRuntimeHeight;
788 // if (arguments.length == 3) {
789 // dx = arguments[1];
790 // dy = arguments[2];
794 // } else if (arguments.length == 5) {
795 // dx = arguments[1];
796 // dy = arguments[2];
797 // dw = arguments[3];
798 // dh = arguments[4];
802 // } else if (arguments.length == 9) {
803 // sx = arguments[1];
804 // sy = arguments[2];
805 // sw = arguments[3];
806 // sh = arguments[4];
807 // dx = arguments[5];
808 // dy = arguments[6];
809 // dw = arguments[7];
810 // dh = arguments[8];
812 // throw Error('Invalid number of arguments');
815 // var d = getCoords(this, dx, dy);
825 // // For some reason that I've now forgotten, using divs didn't work
826 // vmlStr.push(' <g_vml_:group',
827 // ' coordsize="', Z * W, ',', Z * H, '"',
828 // ' coordorigin="0,0"' ,
829 // ' style="width:', W, 'px;height:', H, 'px;position:absolute;');
831 // // If filters are necessary (rotation exists), create them
832 // // filters are bog-slow, so only create them if abbsolutely necessary
833 // // The following check doesn't account for skews (which don't exist
834 // // in the canvas spec (yet) anyway.
836 // if (this.m_[0][0] != 1 || this.m_[0][1] ||
837 // this.m_[1][1] != 1 || this.m_[1][0]) {
840 // // Note the 12/21 reversal
841 // filter.push('M11=', this.m_[0][0], ',',
842 // 'M12=', this.m_[1][0], ',',
843 // 'M21=', this.m_[0][1], ',',
844 // 'M22=', this.m_[1][1], ',',
845 // 'Dx=', mr(d.x / Z), ',',
846 // 'Dy=', mr(d.y / Z), '');
848 // // Bounding box calculation (need to minimize displayed area so that
849 // // filters don't waste time on unused pixels.
851 // var c2 = getCoords(this, dx + dw, dy);
852 // var c3 = getCoords(this, dx, dy + dh);
853 // var c4 = getCoords(this, dx + dw, dy + dh);
855 // max.x = m.max(max.x, c2.x, c3.x, c4.x);
856 // max.y = m.max(max.y, c2.y, c3.y, c4.y);
858 // vmlStr.push('padding:0 ', mr(max.x / Z), 'px ', mr(max.y / Z),
859 // 'px 0;filter:progid:DXImageTransform.Microsoft.Matrix(',
860 // filter.join(''), ", sizingmethod='clip');");
863 // vmlStr.push('top:', mr(d.y / Z), 'px;left:', mr(d.x / Z), 'px;');
866 // vmlStr.push(' ">' ,
867 // '<g_vml_:image src="', image.src, '"',
868 // ' style="width:', Z * dw, 'px;',
869 // ' height:', Z * dh, 'px"',
870 // ' cropleft="', sx / w, '"',
871 // ' croptop="', sy / h, '"',
872 // ' cropright="', (w - sx - sw) / w, '"',
873 // ' cropbottom="', (h - sy - sh) / h, '"',
875 // '</g_vml_:group>');
877 // this.element_.insertAdjacentHTML('BeforeEnd', vmlStr.join(''));
880 contextPrototype.stroke = function (aFill) {
882 var lineOpen = false;
887 lineStr.push('<g_vml_:shape',
888 ' filled="', !!aFill, '"',
889 ' style="position:absolute;width:', W, 'px;height:', H, 'px;"',
890 ' coordorigin="0,0"',
891 ' coordsize="', Z * W, ',', Z * H, '"',
892 ' stroked="', !aFill, '"',
896 var min = { x: null, y: null };
897 var max = { x: null, y: null };
899 for (var i = 0; i < this.currentPath_.length; i++) {
900 var p = this.currentPath_[i];
906 lineStr.push(' m ', mr(p.x), ',', mr(p.y));
909 lineStr.push(' l ', mr(p.x), ',', mr(p.y));
915 case 'bezierCurveTo':
917 mr(p.cp1x), ',', mr(p.cp1y), ',',
918 mr(p.cp2x), ',', mr(p.cp2y), ',',
919 mr(p.x), ',', mr(p.y));
923 lineStr.push(' ', p.type, ' ',
924 mr(p.x - this.arcScaleX_ * p.radius), ',',
925 mr(p.y - this.arcScaleY_ * p.radius), ' ',
926 mr(p.x + this.arcScaleX_ * p.radius), ',',
927 mr(p.y + this.arcScaleY_ * p.radius), ' ',
928 mr(p.xStart), ',', mr(p.yStart), ' ',
929 mr(p.xEnd), ',', mr(p.yEnd));
934 // TODO: Following is broken for curves due to
935 // move to proper paths.
937 // Figure out dimensions so we can do gradient fills
940 if (min.x == null || p.x < min.x) {
943 if (max.x == null || p.x > max.x) {
946 if (min.y == null || p.y < min.y) {
949 if (max.y == null || p.y > max.y) {
957 appendStroke(this, lineStr);
959 appendFill(this, lineStr, min, max);
962 lineStr.push('</g_vml_:shape>');
964 this.element_.insertAdjacentHTML('beforeEnd', lineStr.join(''));
967 function appendStroke(ctx, lineStr) {
968 var a = processStyle(ctx.strokeStyle);
970 var opacity = a.alpha * ctx.globalAlpha;
971 var lineWidth = ctx.lineScale_ * ctx.lineWidth;
973 // VML cannot correctly render a line if the width is less than 1px.
974 // In that case, we dilute the color to make the line look thinner.
976 opacity *= lineWidth;
981 ' opacity="', opacity, '"',
982 ' joinstyle="', ctx.lineJoin, '"',
983 ' miterlimit="', ctx.miterLimit, '"',
984 ' endcap="', processLineCap(ctx.lineCap), '"',
985 ' weight="', lineWidth, 'px"',
986 ' color="', color, '" />'
990 function appendFill(ctx, lineStr, min, max) {
991 var fillStyle = ctx.fillStyle;
992 var arcScaleX = ctx.arcScaleX_;
993 var arcScaleY = ctx.arcScaleY_;
994 var width = max.x - min.x;
995 var height = max.y - min.y;
996 // if (fillStyle instanceof CanvasGradient_) {
997 // // TODO: Gradients transformed with the transformation matrix.
999 // var focus = {x: 0, y: 0};
1001 // // additional offset
1003 // // scale factor for offset
1004 // var expansion = 1;
1006 // if (fillStyle.type_ == 'gradient') {
1007 // var x0 = fillStyle.x0_ / arcScaleX;
1008 // var y0 = fillStyle.y0_ / arcScaleY;
1009 // var x1 = fillStyle.x1_ / arcScaleX;
1010 // var y1 = fillStyle.y1_ / arcScaleY;
1011 // var p0 = getCoords(ctx, x0, y0);
1012 // var p1 = getCoords(ctx, x1, y1);
1013 // var dx = p1.x - p0.x;
1014 // var dy = p1.y - p0.y;
1015 // angle = Math.atan2(dx, dy) * 180 / Math.PI;
1017 // // The angle should be a non-negative number.
1022 // // Very small angles produce an unexpected result because they are
1023 // // converted to a scientific notation string.
1024 // if (angle < 1e-6) {
1028 // var p0 = getCoords(ctx, fillStyle.x0_, fillStyle.y0_);
1030 // x: (p0.x - min.x) / width,
1031 // y: (p0.y - min.y) / height
1034 // width /= arcScaleX * Z;
1035 // height /= arcScaleY * Z;
1036 // var dimension = m.max(width, height);
1037 // shift = 2 * fillStyle.r0_ / dimension;
1038 // expansion = 2 * fillStyle.r1_ / dimension - shift;
1041 // // We need to sort the color stops in ascending order by offset,
1042 // // otherwise IE won't interpret it correctly.
1043 // var stops = fillStyle.colors_;
1044 // stops.sort(function(cs1, cs2) {
1045 // return cs1.offset - cs2.offset;
1048 // var length = stops.length;
1049 // var color1 = stops[0].color;
1050 // var color2 = stops[length - 1].color;
1051 // var opacity1 = stops[0].alpha * ctx.globalAlpha;
1052 // var opacity2 = stops[length - 1].alpha * ctx.globalAlpha;
1055 // for (var i = 0; i < length; i++) {
1056 // var stop = stops[i];
1057 // colors.push(stop.offset * expansion + shift + ' ' + stop.color);
1060 // // When colors attribute is used, the meanings of opacity and o:opacity2
1062 // lineStr.push('<g_vml_:fill type="', fillStyle.type_, '"',
1063 // ' method="none" focus="100%"',
1064 // ' color="', color1, '"',
1065 // ' color2="', color2, '"',
1066 // ' colors="', colors.join(','), '"',
1067 // ' opacity="', opacity2, '"',
1068 // ' g_o_:opacity2="', opacity1, '"',
1069 // ' angle="', angle, '"',
1070 // ' focusposition="', focus.x, ',', focus.y, '" />');
1071 // } else if (fillStyle instanceof CanvasPattern_) {
1072 // if (width && height) {
1073 // var deltaLeft = -min.x;
1074 // var deltaTop = -min.y;
1075 // lineStr.push('<g_vml_:fill',
1077 // deltaLeft / width * arcScaleX * arcScaleX, ',',
1078 // deltaTop / height * arcScaleY * arcScaleY, '"',
1080 // // TODO: Figure out the correct size to fit the scale.
1081 // //' size="', w, 'px ', h, 'px"',
1082 // ' src="', fillStyle.src_, '" />');
1085 var a = processStyle(ctx.fillStyle);
1086 var color = a.color;
1087 var opacity = a.alpha * ctx.globalAlpha;
1088 lineStr.push('<g_vml_:fill color="', color, '" opacity="', opacity,
1093 contextPrototype.fill = function () {
1097 contextPrototype.closePath = function () {
1098 this.currentPath_.push({ type: 'close' });
1101 function getCoords(ctx, aX, aY) {
1104 x: Z * (aX * m[0][0] + aY * m[1][0] + m[2][0]) - Z2,
1105 y: Z * (aX * m[0][1] + aY * m[1][1] + m[2][1]) - Z2
1109 contextPrototype.save = function () {
1112 this.aStack_.push(o);
1113 this.mStack_.push(this.m_);
1114 this.m_ = matrixMultiply(createMatrixIdentity(), this.m_);
1117 contextPrototype.restore = function () {
1118 if (this.aStack_.length) {
1119 copyState(this.aStack_.pop(), this);
1120 this.m_ = this.mStack_.pop();
1124 function matrixIsFinite(m) {
1125 return isFinite(m[0][0]) && isFinite(m[0][1]) &&
1126 isFinite(m[1][0]) && isFinite(m[1][1]) &&
1127 isFinite(m[2][0]) && isFinite(m[2][1]);
1130 function setM(ctx, m, updateLineScale) {
1131 if (!matrixIsFinite(m)) {
1136 if (updateLineScale) {
1137 // Get the line scale.
1138 // Determinant of this.m_ means how much the area is enlarged by the
1139 // transformation. So its square root can be used as a scale factor
1141 var det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
1142 ctx.lineScale_ = sqrt(abs(det));
1146 contextPrototype.translate = function (aX, aY) {
1153 setM(this, matrixMultiply(m1, this.m_), false);
1156 // contextPrototype.rotate = function(aRot) {
1157 // var c = mc(aRot);
1158 // var s = ms(aRot);
1166 // setM(this, matrixMultiply(m1, this.m_), false);
1169 contextPrototype.scale = function (aX, aY) {
1170 this.arcScaleX_ *= aX;
1171 this.arcScaleY_ *= aY;
1178 setM(this, matrixMultiply(m1, this.m_), true);
1181 // contextPrototype.transform = function(m11, m12, m21, m22, dx, dy) {
1188 // setM(this, matrixMultiply(m1, this.m_), true);
1191 // contextPrototype.setTransform = function(m11, m12, m21, m22, dx, dy) {
1198 // setM(this, m, true);
1202 * The text drawing function.
1203 * The maxWidth argument isn't taken in account, since no browser supports
1206 // contextPrototype.drawText_ = function(text, x, y, maxWidth, stroke) {
1211 // offset = {x: 0, y: 0},
1214 // var fontStyle = getComputedStyle(processFontStyle(this.font),
1217 // var fontStyleString = buildStyle(fontStyle);
1219 // var elementStyle = this.element_.currentStyle;
1220 // var textAlign = this.textAlign.toLowerCase();
1221 // switch (textAlign) {
1227 // textAlign = elementStyle.direction == 'ltr' ? 'right' : 'left';
1230 // textAlign = elementStyle.direction == 'rtl' ? 'right' : 'left';
1233 // textAlign = 'left';
1236 // // 1.75 is an arbitrary number, as there is no info about the text baseline
1237 // switch (this.textBaseline) {
1240 // offset.y = fontStyle.size / 1.75;
1246 // case 'alphabetic':
1247 // case 'ideographic':
1249 // offset.y = -fontStyle.size / 2.25;
1253 // switch(textAlign) {
1259 // left = right = delta / 2;
1263 // var d = getCoords(this, x + offset.x, y + offset.y);
1265 // lineStr.push('<g_vml_:line from="', -left ,' 0" to="', right ,' 0.05" ',
1266 // ' coordsize="100 100" coordorigin="0 0"',
1267 // ' filled="', !stroke, '" stroked="', !!stroke,
1268 // '" style="position:absolute;width:1px;height:1px;">');
1271 // appendStroke(this, lineStr);
1273 // // TODO: Fix the min and max params.
1274 // appendFill(this, lineStr, {x: -left, y: 0},
1275 // {x: right, y: fontStyle.size});
1278 // var skewM = m[0][0].toFixed(3) + ',' + m[1][0].toFixed(3) + ',' +
1279 // m[0][1].toFixed(3) + ',' + m[1][1].toFixed(3) + ',0,0';
1281 // var skewOffset = mr(d.x / Z) + ',' + mr(d.y / Z);
1283 // lineStr.push('<g_vml_:skew on="t" matrix="', skewM ,'" ',
1284 // ' offset="', skewOffset, '" origin="', left ,' 0" />',
1285 // '<g_vml_:path textpathok="true" />',
1286 // '<g_vml_:textpath on="true" string="',
1287 // encodeHtmlAttribute(text),
1288 // '" style="v-text-align:', textAlign,
1289 // ';font:', encodeHtmlAttribute(fontStyleString),
1290 // '" /></g_vml_:line>');
1292 // this.element_.insertAdjacentHTML('beforeEnd', lineStr.join(''));
1295 // contextPrototype.fillText = function(text, x, y, maxWidth) {
1296 // this.drawText_(text, x, y, maxWidth, false);
1299 // contextPrototype.strokeText = function(text, x, y, maxWidth) {
1300 // this.drawText_(text, x, y, maxWidth, true);
1303 // contextPrototype.measureText = function(text) {
1304 // if (!this.textMeasureEl_) {
1305 // var s = '<span style="position:absolute;' +
1306 // 'top:-20000px;left:0;padding:0;margin:0;border:none;' +
1307 // 'white-space:pre;"></span>';
1308 // this.element_.insertAdjacentHTML('beforeEnd', s);
1309 // this.textMeasureEl_ = this.element_.lastChild;
1311 // var doc = this.element_.ownerDocument;
1312 // this.textMeasureEl_.innerHTML = '';
1313 // this.textMeasureEl_.style.font = this.font;
1314 // // Don't use innerHTML or innerText because they allow markup/whitespace.
1315 // this.textMeasureEl_.appendChild(doc.createTextNode(text));
1316 // return {width: this.textMeasureEl_.offsetWidth};
1319 /******** STUBS ********/
1320 // contextPrototype.clip = function() {
1321 // // TODO: Implement
1324 // contextPrototype.arcTo = function() {
1325 // // TODO: Implement
1328 // contextPrototype.createPattern = function(image, repetition) {
1329 // return new CanvasPattern_(image, repetition);
1332 // // Gradient / Pattern Stubs
1333 // function CanvasGradient_(aType) {
1334 // this.type_ = aType;
1341 // this.colors_ = [];
1344 // CanvasGradient_.prototype.addColorStop = function(aOffset, aColor) {
1345 // aColor = processStyle(aColor);
1346 // this.colors_.push({offset: aOffset,
1347 // color: aColor.color,
1348 // alpha: aColor.alpha});
1351 // function CanvasPattern_(image, repetition) {
1352 // assertImageIsValid(image);
1353 // switch (repetition) {
1357 // this.repetition_ = 'repeat';
1361 // case 'no-repeat':
1362 // this.repetition_ = repetition;
1365 // throwException('SYNTAX_ERR');
1368 // this.src_ = image.src;
1369 // this.width_ = image.width;
1370 // this.height_ = image.height;
1373 function throwException(s) {
1374 throw new DOMException_(s);
1377 // function assertImageIsValid(img) {
1378 // if (!img || img.nodeType != 1 || img.tagName != 'IMG') {
1379 // throwException('TYPE_MISMATCH_ERR');
1381 // if (img.readyState != 'complete') {
1382 // throwException('INVALID_STATE_ERR');
1386 function DOMException_(s) {
1387 this.code = this[s];
1388 this.message = s + ': DOM Exception ' + this.code;
1390 var p = DOMException_.prototype = new Error;
1391 p.INDEX_SIZE_ERR = 1;
1392 p.DOMSTRING_SIZE_ERR = 2;
1393 p.HIERARCHY_REQUEST_ERR = 3;
1394 p.WRONG_DOCUMENT_ERR = 4;
1395 p.INVALID_CHARACTER_ERR = 5;
1396 p.NO_DATA_ALLOWED_ERR = 6;
1397 p.NO_MODIFICATION_ALLOWED_ERR = 7;
1398 p.NOT_FOUND_ERR = 8;
1399 p.NOT_SUPPORTED_ERR = 9;
1400 p.INUSE_ATTRIBUTE_ERR = 10;
1401 p.INVALID_STATE_ERR = 11;
1403 p.INVALID_MODIFICATION_ERR = 13;
1404 p.NAMESPACE_ERR = 14;
1405 p.INVALID_ACCESS_ERR = 15;
1406 p.VALIDATION_ERR = 16;
1407 p.TYPE_MISMATCH_ERR = 17;
1410 G_vmlCanvasManager = G_vmlCanvasManager_;
1411 CanvasRenderingContext2D = CanvasRenderingContext2D_;
1412 //CanvasGradient = CanvasGradient_;
1413 //CanvasPattern = CanvasPattern_;
1414 DOMException = DOMException_;