2 html2canvas 0.4.1 <http://html2canvas.hertzen.com>
3 Copyright (c) 2013 Niklas von Hertzen
5 Released under MIT License
8 (function(window, document, undefined){
12 var _html2canvas = {},
17 _html2canvas.Util = {};
19 _html2canvas.Util.log = function(a) {
20 if (_html2canvas.logging && window.console && window.console.log) {
21 window.console.log(a);
25 _html2canvas.Util.trimText = (function(isNative){
26 return function(input) {
27 return isNative ? isNative.apply(input) : ((input || '') + '').replace( /^\s+|\s+$/g , '' );
29 })(String.prototype.trim);
31 _html2canvas.Util.asFloat = function(v) {
36 // TODO: support all possible length values
37 var TEXT_SHADOW_PROPERTY = /((rgba|rgb)\([^\)]+\)(\s-?\d+px){0,})/g;
38 var TEXT_SHADOW_VALUES = /(-?\d+px)|(#.+)|(rgb\(.+\))|(rgba\(.+\))/g;
39 _html2canvas.Util.parseTextShadows = function (value) {
40 if (!value || value === 'none') {
44 // find multiple shadow declarations
45 var shadows = value.match(TEXT_SHADOW_PROPERTY),
47 for (var i = 0; shadows && (i < shadows.length); i++) {
48 var s = shadows[i].match(TEXT_SHADOW_VALUES);
51 offsetX: s[1] ? s[1].replace('px', '') : 0,
52 offsetY: s[2] ? s[2].replace('px', '') : 0,
53 blur: s[3] ? s[3].replace('px', '') : 0
60 _html2canvas.Util.parseBackgroundImage = function (value) {
61 var whitespace = ' \r\n\t',
62 method, definition, prefix, prefix_i, block, results = [],
63 c, mode = 0, numParen = 0, quote, args;
65 var appendResult = function(){
67 if(definition.substr( 0, 1 ) === '"') {
68 definition = definition.substr( 1, definition.length - 2 );
71 args.push(definition);
73 if(method.substr( 0, 1 ) === '-' &&
74 (prefix_i = method.indexOf( '-', 1 ) + 1) > 0) {
75 prefix = method.substr( 0, prefix_i);
76 method = method.substr( prefix_i );
80 method: method.toLowerCase(),
85 args = []; //for some odd reason, setting .length = 0 didn't work in safari
93 for(var i = 0, ii = value.length; i<ii; i++) {
95 if(mode === 0 && whitespace.indexOf( c ) > -1){
103 else if(quote === c) {
110 else if(mode === 0) {
121 else if(mode === 1) {
135 else if(mode === 0) {
139 else if (mode === 1) {
140 if(numParen === 0 && !method.match(/^url$/i)) {
141 args.push(definition);
151 if(mode === 0) { method += c; }
152 else { definition += c; }
159 _html2canvas.Util.Bounds = function (element) {
160 var clientRect, bounds = {};
162 if (element.getBoundingClientRect){
163 clientRect = element.getBoundingClientRect();
165 // TODO add scroll position to bounds, so no scrolling of window necessary
166 bounds.top = clientRect.top;
167 bounds.bottom = clientRect.bottom || (clientRect.top + clientRect.height);
168 bounds.left = clientRect.left;
170 bounds.width = element.offsetWidth;
171 bounds.height = element.offsetHeight;
177 // TODO ideally, we'd want everything to go through this function instead of Util.Bounds,
178 // but would require further work to calculate the correct positions for elements with offsetParents
179 _html2canvas.Util.OffsetBounds = function (element) {
180 var parent = element.offsetParent ? _html2canvas.Util.OffsetBounds(element.offsetParent) : {top: 0, left: 0};
183 top: element.offsetTop + parent.top,
184 bottom: element.offsetTop + element.offsetHeight + parent.top,
185 left: element.offsetLeft + parent.left,
186 width: element.offsetWidth,
187 height: element.offsetHeight
191 function toPX(element, attribute, value ) {
192 var rsLeft = element.runtimeStyle && element.runtimeStyle[attribute],
194 style = element.style;
196 // Check if we are not dealing with pixels, (Opera has issues with this)
197 // Ported from jQuery css.js
198 // From the awesome hack by Dean Edwards
199 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
201 // If we're not dealing with a regular pixel number
202 // but a number that has a weird ending, we need to convert it to pixels
204 if ( !/^-?[0-9]+\.?[0-9]*(?:px)?$/i.test( value ) && /^-?\d/.test(value) ) {
205 // Remember the original values
208 // Put in the new values to get a computed value out
210 element.runtimeStyle.left = element.currentStyle.left;
212 style.left = attribute === "fontSize" ? "1em" : (value || 0);
213 value = style.pixelLeft + "px";
215 // Revert the changed values
218 element.runtimeStyle.left = rsLeft;
222 if (!/^(thin|medium|thick)$/i.test(value)) {
223 return Math.round(parseFloat(value)) + "px";
229 function asInt(val) {
230 return parseInt(val, 10);
233 function isPercentage(value) {
234 return value.toString().indexOf("%") !== -1;
237 function parseBackgroundSizePosition(value, element, attribute, index) {
238 value = (value || '').split(',');
239 value = value[index || 0] || value[0] || 'auto';
240 value = _html2canvas.Util.trimText(value).split(' ');
241 if(attribute === 'backgroundSize' && (value[0] && value[0].match(/^(cover|contain|auto)$/))) {
244 value[0] = (value[0].indexOf( "%" ) === -1) ? toPX(element, attribute + "X", value[0]) : value[0];
245 if(value[1] === undefined) {
246 if(attribute === 'backgroundSize') {
250 // IE 9 doesn't return double digit always
254 value[1] = (value[1].indexOf("%") === -1) ? toPX(element, attribute + "Y", value[1]) : value[1];
259 _html2canvas.Util.getCSS = function (element, attribute, index) {
260 if (previousElement !== element) {
261 computedCSS = document.defaultView.getComputedStyle(element, null);
264 var value = computedCSS[attribute];
266 if (/^background(Size|Position)$/.test(attribute)) {
267 return parseBackgroundSizePosition(value, element, attribute, index);
268 } else if (/border(Top|Bottom)(Left|Right)Radius/.test(attribute)) {
269 var arr = value.split(" ");
270 if (arr.length <= 1) {
273 return arr.map(asInt);
279 _html2canvas.Util.resizeBounds = function( current_width, current_height, target_width, target_height, stretch_mode ){
280 var target_ratio = target_width / target_height,
281 current_ratio = current_width / current_height,
282 output_width, output_height;
284 if(!stretch_mode || stretch_mode === 'auto') {
285 output_width = target_width;
286 output_height = target_height;
287 } else if(target_ratio < current_ratio ^ stretch_mode === 'contain') {
288 output_height = target_height;
289 output_width = target_height * current_ratio;
291 output_width = target_width;
292 output_height = target_width / current_ratio;
297 height: output_height
301 _html2canvas.Util.BackgroundPosition = function(element, bounds, image, imageIndex, backgroundSize ) {
302 var backgroundPosition = _html2canvas.Util.getCSS(element, 'backgroundPosition', imageIndex),
305 if (backgroundPosition.length === 1){
306 backgroundPosition = [backgroundPosition[0], backgroundPosition[0]];
309 if (isPercentage(backgroundPosition[0])){
310 leftPosition = (bounds.width - (backgroundSize || image).width) * (parseFloat(backgroundPosition[0]) / 100);
312 leftPosition = parseInt(backgroundPosition[0], 10);
315 if (backgroundPosition[1] === 'auto') {
316 topPosition = leftPosition / image.width * image.height;
317 } else if (isPercentage(backgroundPosition[1])){
318 topPosition = (bounds.height - (backgroundSize || image).height) * parseFloat(backgroundPosition[1]) / 100;
320 topPosition = parseInt(backgroundPosition[1], 10);
323 if (backgroundPosition[0] === 'auto') {
324 leftPosition = topPosition / image.height * image.width;
327 return {left: leftPosition, top: topPosition};
330 _html2canvas.Util.BackgroundSize = function(element, bounds, image, imageIndex) {
331 var backgroundSize = _html2canvas.Util.getCSS(element, 'backgroundSize', imageIndex), width, height;
333 if (backgroundSize.length === 1) {
334 backgroundSize = [backgroundSize[0], backgroundSize[0]];
337 if (isPercentage(backgroundSize[0])) {
338 width = bounds.width * parseFloat(backgroundSize[0]) / 100;
339 } else if (/contain|cover/.test(backgroundSize[0])) {
340 return _html2canvas.Util.resizeBounds(image.width, image.height, bounds.width, bounds.height, backgroundSize[0]);
342 width = parseInt(backgroundSize[0], 10);
345 if (backgroundSize[0] === 'auto' && backgroundSize[1] === 'auto') {
346 height = image.height;
347 } else if (backgroundSize[1] === 'auto') {
348 height = width / image.width * image.height;
349 } else if (isPercentage(backgroundSize[1])) {
350 height = bounds.height * parseFloat(backgroundSize[1]) / 100;
352 height = parseInt(backgroundSize[1], 10);
355 if (backgroundSize[0] === 'auto') {
356 width = height / image.height * image.width;
359 return {width: width, height: height};
362 _html2canvas.Util.BackgroundRepeat = function(element, imageIndex) {
363 var backgroundRepeat = _html2canvas.Util.getCSS(element, "backgroundRepeat").split(",").map(_html2canvas.Util.trimText);
364 return backgroundRepeat[imageIndex] || backgroundRepeat[0];
367 _html2canvas.Util.Extend = function (options, defaults) {
368 for (var key in options) {
369 if (options.hasOwnProperty(key)) {
370 defaults[key] = options[key];
378 * Derived from jQuery.contents()
379 * Copyright 2010, John Resig
380 * Dual licensed under the MIT or GPL Version 2 licenses.
381 * http://jquery.org/license
383 _html2canvas.Util.Children = function( elem ) {
386 children = (elem.nodeName && elem.nodeName.toUpperCase() === "IFRAME") ? elem.contentDocument || elem.contentWindow.document : (function(array) {
388 if (array !== null) {
389 (function(first, second ) {
390 var i = first.length,
393 if (typeof second.length === "number") {
394 for (var l = second.length; j < l; j++) {
395 first[i++] = second[j];
398 while (second[j] !== undefined) {
399 first[i++] = second[j++];
412 _html2canvas.Util.log("html2canvas.Util.Children failed with exception: " + ex.message);
418 _html2canvas.Util.isTransparent = function(backgroundColor) {
419 return (!backgroundColor || backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)");
422 _html2canvas.Util.Font = (function () {
426 return function(font, fontSize, doc) {
427 if (fontData[font + "-" + fontSize] !== undefined) {
428 return fontData[font + "-" + fontSize];
431 var container = doc.createElement('div'),
432 img = doc.createElement('img'),
433 span = doc.createElement('span'),
434 sampleText = 'Hidden Text',
439 container.style.visibility = "hidden";
440 container.style.fontFamily = font;
441 container.style.fontSize = fontSize;
442 container.style.margin = 0;
443 container.style.padding = 0;
445 doc.body.appendChild(container);
447 // http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever (handtinywhite.gif)
448 img.src = "data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=";
452 img.style.margin = 0;
453 img.style.padding = 0;
454 img.style.verticalAlign = "baseline";
456 span.style.fontFamily = font;
457 span.style.fontSize = fontSize;
458 span.style.margin = 0;
459 span.style.padding = 0;
461 span.appendChild(doc.createTextNode(sampleText));
462 container.appendChild(span);
463 container.appendChild(img);
464 baseline = (img.offsetTop - span.offsetTop) + 1;
466 container.removeChild(span);
467 container.appendChild(doc.createTextNode(sampleText));
469 container.style.lineHeight = "normal";
470 img.style.verticalAlign = "super";
472 middle = (img.offsetTop-container.offsetTop) + 1;
479 fontData[font + "-" + fontSize] = metricsObj;
481 doc.body.removeChild(container);
488 var Util = _html2canvas.Util,
491 _html2canvas.Generate = Generate;
494 /^(-webkit-linear-gradient)\(([a-z\s]+)([\w\d\.\s,%\(\)]+)\)$/,
495 /^(-o-linear-gradient)\(([a-z\s]+)([\w\d\.\s,%\(\)]+)\)$/,
496 /^(-webkit-gradient)\((linear|radial),\s((?:\d{1,3}%?)\s(?:\d{1,3}%?),\s(?:\d{1,3}%?)\s(?:\d{1,3}%?))([\w\d\.\s,%\(\)\-]+)\)$/,
497 /^(-moz-linear-gradient)\(((?:\d{1,3}%?)\s(?:\d{1,3}%?))([\w\d\.\s,%\(\)]+)\)$/,
498 /^(-webkit-radial-gradient)\(((?:\d{1,3}%?)\s(?:\d{1,3}%?)),\s(\w+)\s([a-z\-]+)([\w\d\.\s,%\(\)]+)\)$/,
499 /^(-moz-radial-gradient)\(((?:\d{1,3}%?)\s(?:\d{1,3}%?)),\s(\w+)\s?([a-z\-]*)([\w\d\.\s,%\(\)]+)\)$/,
500 /^(-o-radial-gradient)\(((?:\d{1,3}%?)\s(?:\d{1,3}%?)),\s(\w+)\s([a-z\-]+)([\w\d\.\s,%\(\)]+)\)$/
504 * TODO: Add IE10 vendor prefix (-ms) support
505 * TODO: Add W3C gradient (linear-gradient) support
506 * TODO: Add old Webkit -webkit-gradient(radial, ...) support
507 * TODO: Maybe some RegExp optimizations are possible ;o)
509 Generate.parseGradient = function(css, bounds) {
510 var gradient, i, len = reGradients.length, m1, stop, m2, m2Len, step, m3, tl,tr,br,bl;
512 for(i = 0; i < len; i+=1){
513 m1 = css.match(reGradients[i]);
521 case '-webkit-linear-gradient':
522 case '-o-linear-gradient':
534 m2 = m1[2].match(/\w+/g);
537 for(i = 0; i < m2Len; i+=1){
541 gradient.y1 = bounds.height;
545 gradient.x0 = bounds.width;
550 gradient.y0 = bounds.height;
556 gradient.x1 = bounds.width;
561 if(gradient.x0 === null && gradient.x1 === null){ // center
562 gradient.x0 = gradient.x1 = bounds.width / 2;
564 if(gradient.y0 === null && gradient.y1 === null){ // center
565 gradient.y0 = gradient.y1 = bounds.height / 2;
568 // get colors and stops
569 m2 = m1[3].match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\)(?:\s\d{1,3}(?:%|px))?)+/g);
572 step = 1 / Math.max(m2Len - 1, 1);
573 for(i = 0; i < m2Len; i+=1){
574 m3 = m2[i].match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\))\s*(\d{1,3})?(%|px)?/);
576 stop = parseFloat(m3[2]);
579 } else { // px - stupid opera
580 stop /= bounds.width;
585 gradient.colorStops.push({
593 case '-webkit-gradient':
596 type: m1[2] === 'radial' ? 'circle' : m1[2], // TODO: Add radial gradient support for older mozilla definitions
605 m2 = m1[3].match(/(\d{1,3})%?\s(\d{1,3})%?,\s(\d{1,3})%?\s(\d{1,3})%?/);
607 gradient.x0 = (m2[1] * bounds.width) / 100;
608 gradient.y0 = (m2[2] * bounds.height) / 100;
609 gradient.x1 = (m2[3] * bounds.width) / 100;
610 gradient.y1 = (m2[4] * bounds.height) / 100;
613 // get colors and stops
614 m2 = m1[4].match(/((?:from|to|color-stop)\((?:[0-9\.]+,\s)?(?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\)\))+/g);
617 for(i = 0; i < m2Len; i+=1){
618 m3 = m2[i].match(/(from|to|color-stop)\(([0-9\.]+)?(?:,\s)?((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\))\)/);
619 stop = parseFloat(m3[2]);
620 if(m3[1] === 'from') {
626 gradient.colorStops.push({
634 case '-moz-linear-gradient':
646 m2 = m1[2].match(/(\d{1,3})%?\s(\d{1,3})%?/);
648 // m2[1] == 0% -> left
649 // m2[1] == 50% -> center
650 // m2[1] == 100% -> right
652 // m2[2] == 0% -> top
653 // m2[2] == 50% -> center
654 // m2[2] == 100% -> bottom
657 gradient.x0 = (m2[1] * bounds.width) / 100;
658 gradient.y0 = (m2[2] * bounds.height) / 100;
659 gradient.x1 = bounds.width - gradient.x0;
660 gradient.y1 = bounds.height - gradient.y0;
663 // get colors and stops
664 m2 = m1[3].match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\)(?:\s\d{1,3}%)?)+/g);
667 step = 1 / Math.max(m2Len - 1, 1);
668 for(i = 0; i < m2Len; i+=1){
669 m3 = m2[i].match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\))\s*(\d{1,3})?(%)?/);
671 stop = parseFloat(m3[2]);
672 if(m3[3]){ // percentage
678 gradient.colorStops.push({
686 case '-webkit-radial-gradient':
687 case '-moz-radial-gradient':
688 case '-o-radial-gradient':
704 m2 = m1[2].match(/(\d{1,3})%?\s(\d{1,3})%?/);
706 gradient.cx = (m2[1] * bounds.width) / 100;
707 gradient.cy = (m2[2] * bounds.height) / 100;
711 m2 = m1[3].match(/\w+/);
712 m3 = m1[4].match(/[a-z\-]*/);
715 case 'farthest-corner':
716 case 'cover': // is equivalent to farthest-corner
717 case '': // mozilla removes "cover" from definition :(
718 tl = Math.sqrt(Math.pow(gradient.cx, 2) + Math.pow(gradient.cy, 2));
719 tr = Math.sqrt(Math.pow(gradient.cx, 2) + Math.pow(gradient.y1 - gradient.cy, 2));
720 br = Math.sqrt(Math.pow(gradient.x1 - gradient.cx, 2) + Math.pow(gradient.y1 - gradient.cy, 2));
721 bl = Math.sqrt(Math.pow(gradient.x1 - gradient.cx, 2) + Math.pow(gradient.cy, 2));
722 gradient.rx = gradient.ry = Math.max(tl, tr, br, bl);
724 case 'closest-corner':
725 tl = Math.sqrt(Math.pow(gradient.cx, 2) + Math.pow(gradient.cy, 2));
726 tr = Math.sqrt(Math.pow(gradient.cx, 2) + Math.pow(gradient.y1 - gradient.cy, 2));
727 br = Math.sqrt(Math.pow(gradient.x1 - gradient.cx, 2) + Math.pow(gradient.y1 - gradient.cy, 2));
728 bl = Math.sqrt(Math.pow(gradient.x1 - gradient.cx, 2) + Math.pow(gradient.cy, 2));
729 gradient.rx = gradient.ry = Math.min(tl, tr, br, bl);
731 case 'farthest-side':
732 if(m2[0] === 'circle'){
733 gradient.rx = gradient.ry = Math.max(
736 gradient.x1 - gradient.cx,
737 gradient.y1 - gradient.cy
741 gradient.type = m2[0];
743 gradient.rx = Math.max(
745 gradient.x1 - gradient.cx
747 gradient.ry = Math.max(
749 gradient.y1 - gradient.cy
754 case 'contain': // is equivalent to closest-side
755 if(m2[0] === 'circle'){
756 gradient.rx = gradient.ry = Math.min(
759 gradient.x1 - gradient.cx,
760 gradient.y1 - gradient.cy
764 gradient.type = m2[0];
766 gradient.rx = Math.min(
768 gradient.x1 - gradient.cx
770 gradient.ry = Math.min(
772 gradient.y1 - gradient.cy
777 // TODO: add support for "30px 40px" sizes (webkit only)
782 m2 = m1[5].match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\)(?:\s\d{1,3}(?:%|px))?)+/g);
785 step = 1 / Math.max(m2Len - 1, 1);
786 for(i = 0; i < m2Len; i+=1){
787 m3 = m2[i].match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\))\s*(\d{1,3})?(%|px)?/);
789 stop = parseFloat(m3[2]);
792 } else { // px - stupid opera
793 stop /= bounds.width;
798 gradient.colorStops.push({
811 function addScrollStops(grad) {
812 return function(colorStop) {
814 grad.addColorStop(colorStop.stop, colorStop.color);
817 Util.log(['failed to add color stop: ', e, '; tried to add: ', colorStop]);
822 Generate.Gradient = function(src, bounds) {
823 if(bounds.width === 0 || bounds.height === 0) {
827 var canvas = document.createElement('canvas'),
828 ctx = canvas.getContext('2d'),
831 canvas.width = bounds.width;
832 canvas.height = bounds.height;
834 // TODO: add support for multi defined background gradients
835 gradient = _html2canvas.Generate.parseGradient(src, bounds);
838 switch(gradient.type) {
840 grad = ctx.createLinearGradient(gradient.x0, gradient.y0, gradient.x1, gradient.y1);
841 gradient.colorStops.forEach(addScrollStops(grad));
842 ctx.fillStyle = grad;
843 ctx.fillRect(0, 0, bounds.width, bounds.height);
847 grad = ctx.createRadialGradient(gradient.cx, gradient.cy, 0, gradient.cx, gradient.cy, gradient.rx);
848 gradient.colorStops.forEach(addScrollStops(grad));
849 ctx.fillStyle = grad;
850 ctx.fillRect(0, 0, bounds.width, bounds.height);
854 var canvasRadial = document.createElement('canvas'),
855 ctxRadial = canvasRadial.getContext('2d'),
856 ri = Math.max(gradient.rx, gradient.ry),
859 canvasRadial.width = canvasRadial.height = di;
861 grad = ctxRadial.createRadialGradient(gradient.rx, gradient.ry, 0, gradient.rx, gradient.ry, ri);
862 gradient.colorStops.forEach(addScrollStops(grad));
864 ctxRadial.fillStyle = grad;
865 ctxRadial.fillRect(0, 0, di, di);
867 ctx.fillStyle = gradient.colorStops[gradient.colorStops.length - 1].color;
868 ctx.fillRect(0, 0, canvas.width, canvas.height);
869 ctx.drawImage(canvasRadial, gradient.cx - gradient.rx, gradient.cy - gradient.ry, 2 * gradient.rx, 2 * gradient.ry);
877 Generate.ListAlpha = function(number) {
882 modulus = number % 26;
883 tmp = String.fromCharCode((modulus) + 64) + tmp;
884 number = number / 26;
885 }while((number*26) > 26);
890 Generate.ListRoman = function(number) {
891 var romanArray = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"],
892 decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1],
895 len = romanArray.length;
897 if (number <= 0 || number >= 4000) {
901 for (v=0; v < len; v+=1) {
902 while (number >= decimal[v]) {
903 number -= decimal[v];
904 roman += romanArray[v];
911 function h2cRenderContext(width, height) {
921 'arguments': arguments
924 translate: function() {
928 'arguments': arguments
935 'arguments': arguments
942 'arguments': arguments
945 restore: function() {
949 'arguments': arguments
952 fillRect: function () {
956 'arguments': arguments
959 createPattern: function() {
962 name: "createPattern",
963 'arguments': arguments
966 drawShape: function() {
980 'arguments': arguments
986 'arguments': arguments
992 'arguments': arguments
995 bezierCurveTo: function() {
997 name: "bezierCurveTo",
998 'arguments': arguments
1001 quadraticCurveTo: function() {
1003 name: "quadraticCurveTo",
1004 'arguments': arguments
1010 drawImage: function () {
1014 'arguments': arguments
1017 fillText: function () {
1021 'arguments': arguments
1024 setVariable: function (variable, value) {
1034 _html2canvas.Parse = function (images, options, cb) {
1037 var element = (( options.elements === undefined ) ? document.body : options.elements[0]), // select body by default
1039 doc = element.ownerDocument,
1040 Util = _html2canvas.Util,
1041 support = Util.Support(options, doc),
1042 ignoreElementsRegExp = new RegExp("(" + options.ignoreElements + ")"),
1044 getCSS = Util.getCSS,
1045 pseudoHide = "___html2canvas___pseudoelement",
1046 hidePseudoElementsStyles = doc.createElement('style');
1048 hidePseudoElementsStyles.innerHTML = '.' + pseudoHide +
1049 '-parent:before { content: "" !important; display: none !important; }' +
1050 '.' + pseudoHide + '-parent:after { content: "" !important; display: none !important; }';
1052 body.appendChild(hidePseudoElementsStyles);
1054 images = images || {};
1059 var background = getCSS(document.documentElement, "backgroundColor"),
1060 transparentBackground = (Util.isTransparent(background) && element === document.body),
1061 stack = renderElement(element, null, false, transparentBackground);
1063 // create pseudo elements in a single pass to prevent synchronous layouts
1064 addPseudoElements(element);
1066 parseChildren(element, stack, function() {
1067 if (transparentBackground) {
1068 background = stack.backgroundColor;
1071 removePseudoElements();
1073 Util.log('Done parsing, moving to Render.');
1076 backgroundColor: background,
1082 // Given a root element, find all pseudo elements below, create elements mocking pseudo element styles
1083 // so we can process them as normal elements, and hide the original pseudo elements so they don't interfere
1085 function addPseudoElements(el) {
1086 // These are done in discrete steps to prevent a relayout loop caused by addClass() invalidating
1087 // layouts & getPseudoElement calling getComputedStyle.
1088 var jobs = [], classes = [];
1089 getPseudoElementClasses();
1090 findPseudoElements(el);
1093 function getPseudoElementClasses(){
1094 var findPsuedoEls = /:before|:after/;
1095 var sheets = document.styleSheets;
1096 for (var i = 0, j = sheets.length; i < j; i++) {
1098 var rules = sheets[i].cssRules;
1099 for (var k = 0, l = rules.length; k < l; k++) {
1100 if(findPsuedoEls.test(rules[k].selectorText)) {
1101 classes.push(rules[k].selectorText);
1105 catch(e) { // will throw security exception for style sheets loaded from external domains
1109 // Trim off the :after and :before (or ::after and ::before)
1110 for (i = 0, j = classes.length; i < j; i++) {
1111 classes[i] = classes[i].match(/(^[^:]*)/)[1];
1115 // Using the list of elements we know how pseudo el styles, create fake pseudo elements.
1116 function findPseudoElements(el) {
1117 var els = document.querySelectorAll(classes.join(','));
1118 for(var i = 0, j = els.length; i < j; i++) {
1119 createPseudoElements(els[i]);
1123 // Create pseudo elements & add them to a job queue.
1124 function createPseudoElements(el) {
1125 var before = getPseudoElement(el, ':before'),
1126 after = getPseudoElement(el, ':after');
1129 jobs.push({type: 'before', pseudo: before, el: el});
1133 jobs.push({type: 'after', pseudo: after, el: el});
1137 // Adds a class to the pseudo's parent to prevent the original before/after from messing
1139 // Execute the inserts & addClass() calls in a batch to prevent relayouts.
1140 function runJobs() {
1142 jobs.forEach(function(job){
1143 addClass(job.el, pseudoHide + "-parent");
1147 jobs.forEach(function(job){
1148 if(job.type === 'before'){
1149 job.el.insertBefore(job.pseudo, job.el.firstChild);
1151 job.el.appendChild(job.pseudo);
1159 // Delete our fake pseudo elements from the DOM. This will remove those actual elements
1160 // and the classes on their parents that hide the actual pseudo elements.
1161 // Note that NodeLists are 'live' collections so you can't use a for loop here. They are
1162 // actually deleted from the NodeList after each iteration.
1163 function removePseudoElements(){
1164 // delete pseudo elements
1165 body.removeChild(hidePseudoElementsStyles);
1166 var pseudos = document.getElementsByClassName(pseudoHide + "-element");
1167 while (pseudos.length) {
1168 pseudos[0].parentNode.removeChild(pseudos[0]);
1171 // Remove pseudo hiding classes
1172 var parents = document.getElementsByClassName(pseudoHide + "-parent");
1173 while(parents.length) {
1174 removeClass(parents[0], pseudoHide + "-parent");
1178 function addClass (el, className) {
1180 el.classList.add(className);
1182 el.className = el.className + " " + className;
1186 function removeClass (el, className) {
1188 el.classList.remove(className);
1190 el.className = el.className.replace(className, "").trim();
1194 function hasClass (el, className) {
1195 return el.className.indexOf(className) > -1;
1198 // Note that this doesn't work in < IE8, but we don't support that anyhow
1199 function nodeListToArray (nodeList) {
1200 return Array.prototype.slice.call(nodeList);
1203 function documentWidth () {
1205 Math.max(doc.body.scrollWidth, doc.documentElement.scrollWidth),
1206 Math.max(doc.body.offsetWidth, doc.documentElement.offsetWidth),
1207 Math.max(doc.body.clientWidth, doc.documentElement.clientWidth)
1211 function documentHeight () {
1213 Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight),
1214 Math.max(doc.body.offsetHeight, doc.documentElement.offsetHeight),
1215 Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
1219 function getCSSInt(element, attribute) {
1220 var val = parseInt(getCSS(element, attribute), 10);
1221 return (isNaN(val)) ? 0 : val; // borders in old IE are throwing 'medium' for demo.html
1224 function renderRect (ctx, x, y, w, h, bgcolor) {
1225 if (bgcolor !== "transparent"){
1226 ctx.setVariable("fillStyle", bgcolor);
1227 ctx.fillRect(x, y, w, h);
1232 function capitalize(m, p1, p2) {
1234 return p1 + p2.toUpperCase();
1238 function textTransform (text, transform) {
1241 return text.toLowerCase();
1243 return text.replace( /(^|\s|:|-|\(|\))([a-z])/g, capitalize);
1245 return text.toUpperCase();
1251 function noLetterSpacing(letter_spacing) {
1252 return (/^(normal|none|0px)$/.test(letter_spacing));
1255 function drawText(currentText, x, y, ctx){
1256 if (currentText !== null && Util.trimText(currentText).length > 0) {
1257 ctx.fillText(currentText, x, y);
1262 function setTextVariables(ctx, el, text_decoration, color) {
1264 bold = getCSS(el, "fontWeight"),
1265 family = getCSS(el, "fontFamily"),
1266 size = getCSS(el, "fontSize"),
1267 shadows = Util.parseTextShadows(getCSS(el, "textShadow"));
1269 switch(parseInt(bold, 10)){
1278 ctx.setVariable("fillStyle", color);
1279 ctx.setVariable("font", [getCSS(el, "fontStyle"), getCSS(el, "fontVariant"), bold, size, family].join(" "));
1280 ctx.setVariable("textAlign", (align) ? "right" : "left");
1282 if (shadows.length) {
1283 // TODO: support multiple text shadows
1284 // apply the first text shadow
1285 ctx.setVariable("shadowColor", shadows[0].color);
1286 ctx.setVariable("shadowOffsetX", shadows[0].offsetX);
1287 ctx.setVariable("shadowOffsetY", shadows[0].offsetY);
1288 ctx.setVariable("shadowBlur", shadows[0].blur);
1291 if (text_decoration !== "none"){
1292 return Util.Font(family, size, doc);
1296 function renderTextDecoration(ctx, text_decoration, bounds, metrics, color) {
1297 switch(text_decoration) {
1299 // Draws a line at the baseline of the font
1300 // TODO As some browsers display the line as more than 1px if the font-size is big, need to take that into account both in position and size
1301 renderRect(ctx, bounds.left, Math.round(bounds.top + metrics.baseline + metrics.lineWidth), bounds.width, 1, color);
1304 renderRect(ctx, bounds.left, Math.round(bounds.top), bounds.width, 1, color);
1306 case "line-through":
1307 // TODO try and find exact position for line-through
1308 renderRect(ctx, bounds.left, Math.ceil(bounds.top + metrics.middle + metrics.lineWidth), bounds.width, 1, color);
1313 function getTextBounds(state, text, textDecoration, isLast, transform) {
1315 if (support.rangeBounds && !transform) {
1316 if (textDecoration !== "none" || Util.trimText(text).length !== 0) {
1317 bounds = textRangeBounds(text, state.node, state.textOffset);
1319 state.textOffset += text.length;
1320 } else if (state.node && typeof state.node.nodeValue === "string" ){
1321 var newTextNode = (isLast) ? state.node.splitText(text.length) : null;
1322 bounds = textWrapperBounds(state.node, transform);
1323 state.node = newTextNode;
1328 function textRangeBounds(text, textNode, textOffset) {
1329 var range = doc.createRange();
1330 range.setStart(textNode, textOffset);
1331 range.setEnd(textNode, textOffset + text.length);
1332 return range.getBoundingClientRect();
1335 function textWrapperBounds(oldTextNode, transform) {
1336 var parent = oldTextNode.parentNode,
1337 wrapElement = doc.createElement('wrapper'),
1338 backupText = oldTextNode.cloneNode(true);
1340 wrapElement.appendChild(oldTextNode.cloneNode(true));
1341 parent.replaceChild(wrapElement, oldTextNode);
1343 var bounds = transform ? Util.OffsetBounds(wrapElement) : Util.Bounds(wrapElement);
1344 parent.replaceChild(backupText, wrapElement);
1348 function renderText(el, textNode, stack) {
1349 var ctx = stack.ctx,
1350 color = getCSS(el, "color"),
1351 textDecoration = getCSS(el, "textDecoration"),
1352 textAlign = getCSS(el, "textAlign"),
1360 if (Util.trimText(textNode.nodeValue).length > 0) {
1361 textNode.nodeValue = textTransform(textNode.nodeValue, getCSS(el, "textTransform"));
1362 textAlign = textAlign.replace(["-webkit-auto"],["auto"]);
1364 textList = (!options.letterRendering && /^(left|right|justify|auto)$/.test(textAlign) && noLetterSpacing(getCSS(el, "letterSpacing"))) ?
1365 textNode.nodeValue.split(/(\b| )/)
1366 : textNode.nodeValue.split("");
1368 metrics = setTextVariables(ctx, el, textDecoration, color);
1370 if (options.chinese) {
1371 textList.forEach(function(word, index) {
1372 if (/.*[\u4E00-\u9FA5].*$/.test(word)) {
1373 word = word.split("");
1374 word.unshift(index, 1);
1375 textList.splice.apply(textList, word);
1380 textList.forEach(function(text, index) {
1381 var bounds = getTextBounds(state, text, textDecoration, (index < textList.length - 1), stack.transform.matrix);
1383 drawText(text, bounds.left, bounds.bottom, ctx);
1384 renderTextDecoration(ctx, textDecoration, bounds, metrics, color);
1390 function listPosition (element, val) {
1391 var boundElement = doc.createElement( "boundelement" ),
1395 boundElement.style.display = "inline";
1397 originalType = element.style.listStyleType;
1398 element.style.listStyleType = "none";
1400 boundElement.appendChild(doc.createTextNode(val));
1402 element.insertBefore(boundElement, element.firstChild);
1404 bounds = Util.Bounds(boundElement);
1405 element.removeChild(boundElement);
1406 element.style.listStyleType = originalType;
1410 function elementIndex(el) {
1413 childs = el.parentNode.childNodes;
1415 if (el.parentNode) {
1416 while(childs[++i] !== el) {
1417 if (childs[i].nodeType === 1) {
1427 function listItemText(element, type) {
1428 var currentIndex = elementIndex(element), text;
1431 text = currentIndex;
1433 case "decimal-leading-zero":
1434 text = (currentIndex.toString().length === 1) ? currentIndex = "0" + currentIndex.toString() : currentIndex.toString();
1437 text = _html2canvas.Generate.ListRoman( currentIndex );
1440 text = _html2canvas.Generate.ListRoman( currentIndex ).toLowerCase();
1443 text = _html2canvas.Generate.ListAlpha( currentIndex ).toLowerCase();
1446 text = _html2canvas.Generate.ListAlpha( currentIndex );
1453 function renderListItem(element, stack, elBounds) {
1457 type = getCSS(element, "listStyleType"),
1460 if (/^(decimal|decimal-leading-zero|upper-alpha|upper-latin|upper-roman|lower-alpha|lower-greek|lower-latin|lower-roman)$/i.test(type)) {
1461 text = listItemText(element, type);
1462 listBounds = listPosition(element, text);
1463 setTextVariables(ctx, element, "none", getCSS(element, "color"));
1465 if (getCSS(element, "listStylePosition") === "inside") {
1466 ctx.setVariable("textAlign", "left");
1472 drawText(text, x, listBounds.bottom, ctx);
1476 function loadImage (src){
1477 var img = images[src];
1478 return (img && img.succeeded === true) ? img.img : false;
1481 function clipBounds(src, dst){
1482 var x = Math.max(src.left, dst.left),
1483 y = Math.max(src.top, dst.top),
1484 x2 = Math.min((src.left + src.width), (dst.left + dst.width)),
1485 y2 = Math.min((src.top + src.height), (dst.top + dst.height));
1495 function setZ(element, stack, parentStack){
1497 isPositioned = stack.cssPosition !== 'static',
1498 zIndex = isPositioned ? getCSS(element, 'zIndex') : 'auto',
1499 opacity = getCSS(element, 'opacity'),
1500 isFloated = getCSS(element, 'cssFloat') !== 'none';
1502 // https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context
1503 // When a new stacking context should be created:
1504 // the root element (HTML),
1505 // positioned (absolutely or relatively) with a z-index value other than "auto",
1506 // elements with an opacity value less than 1. (See the specification for opacity),
1507 // on mobile WebKit and Chrome 22+, position: fixed always creates a new stacking context, even when z-index is "auto" (See this post)
1509 stack.zIndex = newContext = h2czContext(zIndex);
1510 newContext.isPositioned = isPositioned;
1511 newContext.isFloated = isFloated;
1512 newContext.opacity = opacity;
1513 newContext.ownStacking = (zIndex !== 'auto' || opacity < 1);
1514 newContext.depth = parentStack ? (parentStack.zIndex.depth + 1) : 0;
1517 parentStack.zIndex.children.push(stack);
1521 function h2czContext(zindex) {
1529 function renderImage(ctx, element, image, bounds, borders) {
1531 var paddingLeft = getCSSInt(element, 'paddingLeft'),
1532 paddingTop = getCSSInt(element, 'paddingTop'),
1533 paddingRight = getCSSInt(element, 'paddingRight'),
1534 paddingBottom = getCSSInt(element, 'paddingBottom');
1543 bounds.left + paddingLeft + borders[3].width, //dx
1544 bounds.top + paddingTop + borders[0].width, // dy
1545 bounds.width - (borders[1].width + borders[3].width + paddingLeft + paddingRight), //dw
1546 bounds.height - (borders[0].width + borders[2].width + paddingTop + paddingBottom) //dh
1550 function getBorderData(element) {
1551 return ["Top", "Right", "Bottom", "Left"].map(function(side) {
1553 width: getCSSInt(element, 'border' + side + 'Width'),
1554 color: getCSS(element, 'border' + side + 'Color')
1559 function getBorderRadiusData(element) {
1560 return ["TopLeft", "TopRight", "BottomRight", "BottomLeft"].map(function(side) {
1561 return getCSS(element, 'border' + side + 'Radius');
1565 function getCurvePoints(x, y, r1, r2) {
1566 var kappa = 4 * ((Math.sqrt(2) - 1) / 3);
1567 var ox = (r1) * kappa, // control point offset horizontal
1568 oy = (r2) * kappa, // control point offset vertical
1569 xm = x + r1, // x-middle
1570 ym = y + r2; // y-middle
1572 topLeft: bezierCurve({
1585 topRight: bezierCurve({
1598 bottomRight: bezierCurve({
1611 bottomLeft: bezierCurve({
1627 function bezierCurve(start, startControl, endControl, end) {
1629 var lerp = function (a, b, t) {
1631 x:a.x + (b.x - a.x) * t,
1632 y:a.y + (b.y - a.y) * t
1638 startControl: startControl,
1639 endControl: endControl,
1641 subdivide: function(t) {
1642 var ab = lerp(start, startControl, t),
1643 bc = lerp(startControl, endControl, t),
1644 cd = lerp(endControl, end, t),
1645 abbc = lerp(ab, bc, t),
1646 bccd = lerp(bc, cd, t),
1647 dest = lerp(abbc, bccd, t);
1648 return [bezierCurve(start, ab, abbc, dest), bezierCurve(dest, bccd, cd, end)];
1650 curveTo: function(borderArgs) {
1651 borderArgs.push(["bezierCurve", startControl.x, startControl.y, endControl.x, endControl.y, end.x, end.y]);
1653 curveToReversed: function(borderArgs) {
1654 borderArgs.push(["bezierCurve", endControl.x, endControl.y, startControl.x, startControl.y, start.x, start.y]);
1659 function parseCorner(borderArgs, radius1, radius2, corner1, corner2, x, y) {
1660 if (radius1[0] > 0 || radius1[1] > 0) {
1661 borderArgs.push(["line", corner1[0].start.x, corner1[0].start.y]);
1662 corner1[0].curveTo(borderArgs);
1663 corner1[1].curveTo(borderArgs);
1665 borderArgs.push(["line", x, y]);
1668 if (radius2[0] > 0 || radius2[1] > 0) {
1669 borderArgs.push(["line", corner2[0].start.x, corner2[0].start.y]);
1673 function drawSide(borderData, radius1, radius2, outer1, inner1, outer2, inner2) {
1674 var borderArgs = [];
1676 if (radius1[0] > 0 || radius1[1] > 0) {
1677 borderArgs.push(["line", outer1[1].start.x, outer1[1].start.y]);
1678 outer1[1].curveTo(borderArgs);
1680 borderArgs.push([ "line", borderData.c1[0], borderData.c1[1]]);
1683 if (radius2[0] > 0 || radius2[1] > 0) {
1684 borderArgs.push(["line", outer2[0].start.x, outer2[0].start.y]);
1685 outer2[0].curveTo(borderArgs);
1686 borderArgs.push(["line", inner2[0].end.x, inner2[0].end.y]);
1687 inner2[0].curveToReversed(borderArgs);
1689 borderArgs.push([ "line", borderData.c2[0], borderData.c2[1]]);
1690 borderArgs.push([ "line", borderData.c3[0], borderData.c3[1]]);
1693 if (radius1[0] > 0 || radius1[1] > 0) {
1694 borderArgs.push(["line", inner1[1].end.x, inner1[1].end.y]);
1695 inner1[1].curveToReversed(borderArgs);
1697 borderArgs.push([ "line", borderData.c4[0], borderData.c4[1]]);
1703 function calculateCurvePoints(bounds, borderRadius, borders) {
1705 var x = bounds.left,
1707 width = bounds.width,
1708 height = bounds.height,
1710 tlh = borderRadius[0][0],
1711 tlv = borderRadius[0][1],
1712 trh = borderRadius[1][0],
1713 trv = borderRadius[1][1],
1714 brh = borderRadius[2][0],
1715 brv = borderRadius[2][1],
1716 blh = borderRadius[3][0],
1717 blv = borderRadius[3][1],
1719 topWidth = width - trh,
1720 rightHeight = height - brv,
1721 bottomWidth = width - brh,
1722 leftHeight = height - blv;
1725 topLeftOuter: getCurvePoints(
1730 ).topLeft.subdivide(0.5),
1732 topLeftInner: getCurvePoints(
1733 x + borders[3].width,
1734 y + borders[0].width,
1735 Math.max(0, tlh - borders[3].width),
1736 Math.max(0, tlv - borders[0].width)
1737 ).topLeft.subdivide(0.5),
1739 topRightOuter: getCurvePoints(
1744 ).topRight.subdivide(0.5),
1746 topRightInner: getCurvePoints(
1747 x + Math.min(topWidth, width + borders[3].width),
1748 y + borders[0].width,
1749 (topWidth > width + borders[3].width) ? 0 :trh - borders[3].width,
1750 trv - borders[0].width
1751 ).topRight.subdivide(0.5),
1753 bottomRightOuter: getCurvePoints(
1758 ).bottomRight.subdivide(0.5),
1760 bottomRightInner: getCurvePoints(
1761 x + Math.min(bottomWidth, width + borders[3].width),
1762 y + Math.min(rightHeight, height + borders[0].width),
1763 Math.max(0, brh - borders[1].width),
1764 Math.max(0, brv - borders[2].width)
1765 ).bottomRight.subdivide(0.5),
1767 bottomLeftOuter: getCurvePoints(
1772 ).bottomLeft.subdivide(0.5),
1774 bottomLeftInner: getCurvePoints(
1775 x + borders[3].width,
1777 Math.max(0, blh - borders[3].width),
1778 Math.max(0, blv - borders[2].width)
1779 ).bottomLeft.subdivide(0.5)
1783 function getBorderClip(element, borderPoints, borders, radius, bounds) {
1784 var backgroundClip = getCSS(element, 'backgroundClip'),
1787 switch(backgroundClip) {
1790 parseCorner(borderArgs, radius[0], radius[1], borderPoints.topLeftInner, borderPoints.topRightInner, bounds.left + borders[3].width, bounds.top + borders[0].width);
1791 parseCorner(borderArgs, radius[1], radius[2], borderPoints.topRightInner, borderPoints.bottomRightInner, bounds.left + bounds.width - borders[1].width, bounds.top + borders[0].width);
1792 parseCorner(borderArgs, radius[2], radius[3], borderPoints.bottomRightInner, borderPoints.bottomLeftInner, bounds.left + bounds.width - borders[1].width, bounds.top + bounds.height - borders[2].width);
1793 parseCorner(borderArgs, radius[3], radius[0], borderPoints.bottomLeftInner, borderPoints.topLeftInner, bounds.left + borders[3].width, bounds.top + bounds.height - borders[2].width);
1797 parseCorner(borderArgs, radius[0], radius[1], borderPoints.topLeftOuter, borderPoints.topRightOuter, bounds.left, bounds.top);
1798 parseCorner(borderArgs, radius[1], radius[2], borderPoints.topRightOuter, borderPoints.bottomRightOuter, bounds.left + bounds.width, bounds.top);
1799 parseCorner(borderArgs, radius[2], radius[3], borderPoints.bottomRightOuter, borderPoints.bottomLeftOuter, bounds.left + bounds.width, bounds.top + bounds.height);
1800 parseCorner(borderArgs, radius[3], radius[0], borderPoints.bottomLeftOuter, borderPoints.topLeftOuter, bounds.left, bounds.top + bounds.height);
1807 function parseBorders(element, bounds, borders){
1808 var x = bounds.left,
1810 width = bounds.width,
1811 height = bounds.height,
1818 // http://www.w3.org/TR/css3-background/#the-border-radius
1819 borderRadius = getBorderRadiusData(element),
1820 borderPoints = calculateCurvePoints(bounds, borderRadius, borders),
1822 clip: getBorderClip(element, borderPoints, borders, borderRadius, bounds),
1826 for (borderSide = 0; borderSide < 4; borderSide++) {
1828 if (borders[borderSide].width > 0) {
1832 bh = height - (borders[2].width);
1834 switch(borderSide) {
1837 bh = borders[0].width;
1839 borderArgs = drawSide({
1842 c3: [bx + bw - borders[1].width, by + bh],
1843 c4: [bx + borders[3].width, by + bh]
1844 }, borderRadius[0], borderRadius[1],
1845 borderPoints.topLeftOuter, borderPoints.topLeftInner, borderPoints.topRightOuter, borderPoints.topRightInner);
1849 bx = x + width - (borders[1].width);
1850 bw = borders[1].width;
1852 borderArgs = drawSide({
1854 c2: [bx + bw, by + bh + borders[2].width],
1856 c4: [bx, by + borders[0].width]
1857 }, borderRadius[1], borderRadius[2],
1858 borderPoints.topRightOuter, borderPoints.topRightInner, borderPoints.bottomRightOuter, borderPoints.bottomRightInner);
1862 by = (by + height) - (borders[2].width);
1863 bh = borders[2].width;
1865 borderArgs = drawSide({
1866 c1: [bx + bw, by + bh],
1868 c3: [bx + borders[3].width, by],
1869 c4: [bx + bw - borders[3].width, by]
1870 }, borderRadius[2], borderRadius[3],
1871 borderPoints.bottomRightOuter, borderPoints.bottomRightInner, borderPoints.bottomLeftOuter, borderPoints.bottomLeftInner);
1875 bw = borders[3].width;
1877 borderArgs = drawSide({
1878 c1: [bx, by + bh + borders[2].width],
1880 c3: [bx + bw, by + borders[0].width],
1881 c4: [bx + bw, by + bh]
1882 }, borderRadius[3], borderRadius[0],
1883 borderPoints.bottomLeftOuter, borderPoints.bottomLeftInner, borderPoints.topLeftOuter, borderPoints.topLeftInner);
1887 borderData.borders.push({
1889 color: borders[borderSide].color
1898 function createShape(ctx, args) {
1899 var shape = ctx.drawShape();
1900 args.forEach(function(border, index) {
1901 shape[(index === 0) ? "moveTo" : border[0] + "To" ].apply(null, border.slice(1));
1906 function renderBorders(ctx, borderArgs, color) {
1907 if (color !== "transparent") {
1908 ctx.setVariable( "fillStyle", color);
1909 createShape(ctx, borderArgs);
1915 function renderFormValue (el, bounds, stack){
1917 var valueWrap = doc.createElement('valuewrap'),
1918 cssPropertyArray = ['lineHeight','textAlign','fontFamily','color','fontSize','paddingLeft','paddingTop','width','height','border','borderLeftWidth','borderTopWidth'],
1922 cssPropertyArray.forEach(function(property) {
1924 valueWrap.style[property] = getCSS(el, property);
1926 // Older IE has issues with "border"
1927 Util.log("html2canvas: Parse: Exception caught in renderFormValue: " + e.message);
1931 valueWrap.style.borderColor = "black";
1932 valueWrap.style.borderStyle = "solid";
1933 valueWrap.style.display = "block";
1934 valueWrap.style.position = "absolute";
1936 if (/^(submit|reset|button|text|password)$/.test(el.type) || el.nodeName === "SELECT"){
1937 valueWrap.style.lineHeight = getCSS(el, "height");
1940 valueWrap.style.top = bounds.top + "px";
1941 valueWrap.style.left = bounds.left + "px";
1943 textValue = (el.nodeName === "SELECT") ? (el.options[el.selectedIndex] || 0).text : el.value;
1945 textValue = el.placeholder;
1948 textNode = doc.createTextNode(textValue);
1950 valueWrap.appendChild(textNode);
1951 body.appendChild(valueWrap);
1953 renderText(el, textNode, stack);
1954 body.removeChild(valueWrap);
1957 function drawImage (ctx) {
1958 ctx.drawImage.apply(ctx, Array.prototype.slice.call(arguments, 1));
1962 function getPseudoElement(el, which) {
1963 var elStyle = window.getComputedStyle(el, which);
1964 var parentStyle = window.getComputedStyle(el);
1965 // If no content attribute is present, the pseudo element is hidden,
1966 // or the parent has a content property equal to the content on the pseudo element,
1968 if(!elStyle || !elStyle.content || elStyle.content === "none" || elStyle.content === "-moz-alt-content" ||
1969 elStyle.display === "none" || parentStyle.content === elStyle.content) {
1972 var content = elStyle.content + '';
1974 // Strip inner quotes
1975 if(content[0] === "'" || content[0] === "\"") {
1976 content = content.replace(/(^['"])|(['"]$)/g, '');
1979 var isImage = content.substr( 0, 3 ) === 'url',
1980 elps = document.createElement( isImage ? 'img' : 'span' );
1982 elps.className = pseudoHide + "-element ";
1984 Object.keys(elStyle).filter(indexedProperty).forEach(function(prop) {
1985 // Prevent assigning of read only CSS Rules, ex. length, parentRule
1987 elps.style[prop] = elStyle[prop];
1989 Util.log(['Tried to assign readonly property ', prop, 'Error:', e]);
1994 elps.src = Util.parseBackgroundImage(content)[0].args[0];
1996 elps.innerHTML = content;
2001 function indexedProperty(property) {
2002 return (isNaN(window.parseInt(property, 10)));
2005 function renderBackgroundRepeat(ctx, image, backgroundPosition, bounds) {
2006 var offsetX = Math.round(bounds.left + backgroundPosition.left),
2007 offsetY = Math.round(bounds.top + backgroundPosition.top);
2009 ctx.createPattern(image);
2010 ctx.translate(offsetX, offsetY);
2012 ctx.translate(-offsetX, -offsetY);
2015 function backgroundRepeatShape(ctx, image, backgroundPosition, bounds, left, top, width, height) {
2017 args.push(["line", Math.round(left), Math.round(top)]);
2018 args.push(["line", Math.round(left + width), Math.round(top)]);
2019 args.push(["line", Math.round(left + width), Math.round(height + top)]);
2020 args.push(["line", Math.round(left), Math.round(height + top)]);
2021 createShape(ctx, args);
2024 renderBackgroundRepeat(ctx, image, backgroundPosition, bounds);
2028 function renderBackgroundColor(ctx, backgroundBounds, bgcolor) {
2031 backgroundBounds.left,
2032 backgroundBounds.top,
2033 backgroundBounds.width,
2034 backgroundBounds.height,
2039 function renderBackgroundRepeating(el, bounds, ctx, image, imageIndex) {
2040 var backgroundSize = Util.BackgroundSize(el, bounds, image, imageIndex),
2041 backgroundPosition = Util.BackgroundPosition(el, bounds, image, imageIndex, backgroundSize),
2042 backgroundRepeat = Util.BackgroundRepeat(el, imageIndex);
2044 image = resizeImage(image, backgroundSize);
2046 switch (backgroundRepeat) {
2048 case "repeat no-repeat":
2049 backgroundRepeatShape(ctx, image, backgroundPosition, bounds,
2050 bounds.left, bounds.top + backgroundPosition.top, 99999, image.height);
2053 case "no-repeat repeat":
2054 backgroundRepeatShape(ctx, image, backgroundPosition, bounds,
2055 bounds.left + backgroundPosition.left, bounds.top, image.width, 99999);
2058 backgroundRepeatShape(ctx, image, backgroundPosition, bounds,
2059 bounds.left + backgroundPosition.left, bounds.top + backgroundPosition.top, image.width, image.height);
2062 renderBackgroundRepeat(ctx, image, backgroundPosition, {
2066 height: image.height
2072 function renderBackgroundImage(element, bounds, ctx) {
2073 var backgroundImage = getCSS(element, "backgroundImage"),
2074 backgroundImages = Util.parseBackgroundImage(backgroundImage),
2076 imageIndex = backgroundImages.length;
2078 while(imageIndex--) {
2079 backgroundImage = backgroundImages[imageIndex];
2081 if (!backgroundImage.args || backgroundImage.args.length === 0) {
2085 var key = backgroundImage.method === 'url' ?
2086 backgroundImage.args[0] :
2087 backgroundImage.value;
2089 image = loadImage(key);
2091 // TODO add support for background-origin
2093 renderBackgroundRepeating(element, bounds, ctx, image, imageIndex);
2095 Util.log("html2canvas: Error loading background:", backgroundImage);
2100 function resizeImage(image, bounds) {
2101 if(image.width === bounds.width && image.height === bounds.height) {
2105 var ctx, canvas = doc.createElement('canvas');
2106 canvas.width = bounds.width;
2107 canvas.height = bounds.height;
2108 ctx = canvas.getContext("2d");
2109 drawImage(ctx, image, 0, 0, image.width, image.height, 0, 0, bounds.width, bounds.height );
2113 function setOpacity(ctx, element, parentStack) {
2114 return ctx.setVariable("globalAlpha", getCSS(element, "opacity") * ((parentStack) ? parentStack.opacity : 1));
2117 function removePx(str) {
2118 return str.replace("px", "");
2121 function getTransform(element, parentStack) {
2122 var transformRegExp = /(matrix)\((.+)\)/;
2123 var transform = getCSS(element, "transform") || getCSS(element, "-webkit-transform") || getCSS(element, "-moz-transform") || getCSS(element, "-ms-transform") || getCSS(element, "-o-transform");
2124 var transformOrigin = getCSS(element, "transform-origin") || getCSS(element, "-webkit-transform-origin") || getCSS(element, "-moz-transform-origin") || getCSS(element, "-ms-transform-origin") || getCSS(element, "-o-transform-origin") || "0px 0px";
2126 transformOrigin = transformOrigin.split(" ").map(removePx).map(Util.asFloat);
2129 if (transform && transform !== "none") {
2130 var match = transform.match(transformRegExp);
2134 matrix = match[2].split(",").map(Util.trimText).map(Util.asFloat);
2141 origin: transformOrigin,
2146 function createStack(element, parentStack, bounds, transform) {
2147 var ctx = h2cRenderContext((!parentStack) ? documentWidth() : bounds.width , (!parentStack) ? documentHeight() : bounds.height),
2150 opacity: setOpacity(ctx, element, parentStack),
2151 cssPosition: getCSS(element, "position"),
2152 borders: getBorderData(element),
2153 transform: transform,
2154 clip: (parentStack && parentStack.clip) ? Util.Extend( {}, parentStack.clip ) : null
2157 setZ(element, stack, parentStack);
2159 // TODO correct overflow for absolute content residing under a static position
2160 if (options.useOverflow === true && /(hidden|scroll|auto)/.test(getCSS(element, "overflow")) === true && /(BODY)/i.test(element.nodeName) === false){
2161 stack.clip = (stack.clip) ? clipBounds(stack.clip, bounds) : bounds;
2167 function getBackgroundBounds(borders, bounds, clip) {
2168 var backgroundBounds = {
2169 left: bounds.left + borders[3].width,
2170 top: bounds.top + borders[0].width,
2171 width: bounds.width - (borders[1].width + borders[3].width),
2172 height: bounds.height - (borders[0].width + borders[2].width)
2176 backgroundBounds = clipBounds(backgroundBounds, clip);
2179 return backgroundBounds;
2182 function getBounds(element, transform) {
2183 var bounds = (transform.matrix) ? Util.OffsetBounds(element) : Util.Bounds(element);
2184 transform.origin[0] += bounds.left;
2185 transform.origin[1] += bounds.top;
2189 function renderElement(element, parentStack, ignoreBackground) {
2190 var transform = getTransform(element, parentStack),
2191 bounds = getBounds(element, transform),
2193 stack = createStack(element, parentStack, bounds, transform),
2194 borders = stack.borders,
2196 backgroundBounds = getBackgroundBounds(borders, bounds, stack.clip),
2197 borderData = parseBorders(element, bounds, borders),
2198 backgroundColor = (ignoreElementsRegExp.test(element.nodeName)) ? "#efefef" : getCSS(element, "backgroundColor");
2201 createShape(ctx, borderData.clip);
2206 if (backgroundBounds.height > 0 && backgroundBounds.width > 0 && !ignoreBackground) {
2207 renderBackgroundColor(ctx, bounds, backgroundColor);
2208 renderBackgroundImage(element, backgroundBounds, ctx);
2209 } else if (ignoreBackground) {
2210 stack.backgroundColor = backgroundColor;
2215 borderData.borders.forEach(function(border) {
2216 renderBorders(ctx, border.args, border.color);
2219 switch(element.nodeName){
2221 if ((image = loadImage(element.getAttribute('src')))) {
2222 renderImage(ctx, element, image, bounds, borders);
2224 Util.log("html2canvas: Error loading <img>:" + element.getAttribute('src'));
2228 // TODO add all relevant type's, i.e. HTML5 new stuff
2229 // todo add support for placeholder attribute for browsers which support it
2230 if (/^(text|url|email|submit|button|reset)$/.test(element.type) && (element.value || element.placeholder || "").length > 0){
2231 renderFormValue(element, bounds, stack);
2235 if ((element.value || element.placeholder || "").length > 0){
2236 renderFormValue(element, bounds, stack);
2240 if ((element.options||element.placeholder || "").length > 0){
2241 renderFormValue(element, bounds, stack);
2245 renderListItem(element, stack, backgroundBounds);
2248 renderImage(ctx, element, element, bounds, borders);
2255 function isElementVisible(element) {
2256 return (getCSS(element, 'display') !== "none" && getCSS(element, 'visibility') !== "hidden" && !element.hasAttribute("data-html2canvas-ignore"));
2259 function parseElement (element, stack, cb) {
2263 if (isElementVisible(element)) {
2264 stack = renderElement(element, stack, false) || stack;
2265 if (!ignoreElementsRegExp.test(element.nodeName)) {
2266 return parseChildren(element, stack, cb);
2272 function parseChildren(element, stack, cb) {
2273 var children = Util.Children(element);
2274 // After all nodes have processed, finished() will call the cb.
2275 // We add one and kick it off so this will still work when children.length === 0.
2276 // Note that unless async is true, this will happen synchronously, just will callbacks.
2277 var jobs = children.length + 1;
2280 if (options.async) {
2281 children.forEach(function(node) {
2282 // Don't block the page from rendering
2283 setTimeout(function(){ parseNode(node); }, 0);
2286 children.forEach(parseNode);
2289 function parseNode(node) {
2290 if (node.nodeType === node.ELEMENT_NODE) {
2291 parseElement(node, stack, finished);
2292 } else if (node.nodeType === node.TEXT_NODE) {
2293 renderText(element, node, stack);
2299 function finished(el) {
2301 Util.log("finished rendering " + children.length + " children.");
2307 _html2canvas.Preload = function( options ) {
2310 numLoaded: 0, // also failed are counted here
2316 Util = _html2canvas.Util,
2320 element = options.elements[0] || document.body,
2321 doc = element.ownerDocument,
2322 domImages = element.getElementsByTagName('img'), // Fetch images of the present element only
2323 imgLen = domImages.length,
2324 link = doc.createElement("a"),
2325 supportCORS = (function( img ){
2326 return (img.crossOrigin !== undefined);
2330 link.href = window.location.href;
2331 pageOrigin = link.protocol + link.host;
2333 function isSameOrigin(url){
2335 link.href = link.href; // YES, BELIEVE IT OR NOT, that is required for IE9 - http://jsfiddle.net/niklasvh/2e48b/
2336 var origin = link.protocol + link.host;
2337 return (origin === pageOrigin);
2341 Util.log("html2canvas: start: images: " + images.numLoaded + " / " + images.numTotal + " (failed: " + images.numFailed + ")");
2342 if (!images.firstRun && images.numLoaded >= images.numTotal){
2343 Util.log("Finished loading images: # " + images.numTotal + " (failed: " + images.numFailed + ")");
2345 if (typeof options.complete === "function"){
2346 options.complete(images);
2352 // TODO modify proxy to serve images with CORS enabled, where available
2353 function proxyGetImage(url, img, imageObj){
2355 scriptUrl = options.proxy,
2359 url = link.href; // work around for pages with base href="" set - WARNING: this may change the url
2361 callback_name = 'html2canvas_' + (count++);
2362 imageObj.callbackname = callback_name;
2364 if (scriptUrl.indexOf("?") > -1) {
2369 scriptUrl += 'url=' + encodeURIComponent(url) + '&callback=' + callback_name;
2370 script = doc.createElement("script");
2372 window[callback_name] = function(a){
2373 if (a.substring(0,6) === "error:"){
2374 imageObj.succeeded = false;
2379 setImageLoadHandlers(img, imageObj);
2382 window[callback_name] = undefined; // to work with IE<9 // NOTE: that the undefined callback property-name still exists on the window object (for IE<9)
2384 delete window[callback_name]; // for all browser that support this
2386 script.parentNode.removeChild(script);
2388 delete imageObj.script;
2389 delete imageObj.callbackname;
2392 script.setAttribute("type", "text/javascript");
2393 script.setAttribute("src", scriptUrl);
2394 imageObj.script = script;
2395 window.document.body.appendChild(script);
2399 function loadPseudoElement(element, type) {
2400 var style = window.getComputedStyle(element, type),
2401 content = style.content;
2402 if (content.substr(0, 3) === 'url') {
2403 methods.loadImage(_html2canvas.Util.parseBackgroundImage(content)[0].args[0]);
2405 loadBackgroundImages(style.backgroundImage, element);
2408 function loadPseudoElementImages(element) {
2409 loadPseudoElement(element, ":before");
2410 loadPseudoElement(element, ":after");
2413 function loadGradientImage(backgroundImage, bounds) {
2414 var img = _html2canvas.Generate.Gradient(backgroundImage, bounds);
2416 if (img !== undefined){
2417 images[backgroundImage] = {
2427 function invalidBackgrounds(background_image) {
2428 return (background_image && background_image.method && background_image.args && background_image.args.length > 0 );
2431 function loadBackgroundImages(background_image, el) {
2434 _html2canvas.Util.parseBackgroundImage(background_image).filter(invalidBackgrounds).forEach(function(background_image) {
2435 if (background_image.method === 'url') {
2436 methods.loadImage(background_image.args[0]);
2437 } else if(background_image.method.match(/\-?gradient$/)) {
2438 if(bounds === undefined) {
2439 bounds = _html2canvas.Util.Bounds(el);
2441 loadGradientImage(background_image.value, bounds);
2446 function getImages (el) {
2447 var elNodeType = false;
2449 // Firefox fails with permission denied on pages with iframes
2451 Util.Children(el).forEach(getImages);
2456 elNodeType = el.nodeType;
2459 Util.log("html2canvas: failed to access some element's nodeType - Exception: " + ex.message);
2462 if (elNodeType === 1 || elNodeType === undefined) {
2463 loadPseudoElementImages(el);
2465 loadBackgroundImages(Util.getCSS(el, 'backgroundImage'), el);
2467 Util.log("html2canvas: failed to get background-image - Exception: " + e.message);
2469 loadBackgroundImages(el);
2473 function setImageLoadHandlers(img, imageObj) {
2474 img.onload = function() {
2475 if ( imageObj.timer !== undefined ) {
2477 window.clearTimeout( imageObj.timer );
2481 imageObj.succeeded = true;
2482 img.onerror = img.onload = null;
2485 img.onerror = function() {
2486 if (img.crossOrigin === "anonymous") {
2488 window.clearTimeout( imageObj.timer );
2490 // let's try with proxy instead
2491 if ( options.proxy ) {
2497 proxyGetImage( img.src, img, imageObj );
2504 imageObj.succeeded = false;
2505 img.onerror = img.onload = null;
2511 loadImage: function( src ) {
2513 if ( src && images[src] === undefined ) {
2515 if ( src.match(/data:image\/.*;base64,/i) ) {
2516 img.src = src.replace(/url\(['"]{0,}|['"]{0,}\)$/ig, '');
2517 imageObj = images[src] = {
2521 setImageLoadHandlers(img, imageObj);
2522 } else if ( isSameOrigin( src ) || options.allowTaint === true ) {
2523 imageObj = images[src] = {
2527 setImageLoadHandlers(img, imageObj);
2529 } else if ( supportCORS && !options.allowTaint && options.useCORS ) {
2530 // attempt to load with CORS
2532 img.crossOrigin = "anonymous";
2533 imageObj = images[src] = {
2537 setImageLoadHandlers(img, imageObj);
2539 } else if ( options.proxy ) {
2540 imageObj = images[src] = {
2544 proxyGetImage( src, img, imageObj );
2549 cleanupDOM: function(cause) {
2551 if (!images.cleanupDone) {
2552 if (cause && typeof cause === "string") {
2553 Util.log("html2canvas: Cleanup because: " + cause);
2555 Util.log("html2canvas: Cleanup after timeout: " + options.timeout + " ms.");
2558 for (src in images) {
2559 if (images.hasOwnProperty(src)) {
2561 if (typeof img === "object" && img.callbackname && img.succeeded === undefined) {
2562 // cancel proxy image request
2563 window[img.callbackname] = undefined; // to work with IE<9 // NOTE: that the undefined callback property-name still exists on the window object (for IE<9)
2565 delete window[img.callbackname]; // for all browser that support this
2567 if (img.script && img.script.parentNode) {
2568 img.script.setAttribute("src", "about:blank"); // try to cancel running request
2569 img.script.parentNode.removeChild(img.script);
2573 Util.log("html2canvas: Cleaned up failed img: '" + src + "' Steps: " + images.numLoaded + " / " + images.numTotal);
2578 // cancel any pending requests
2579 if(window.stop !== undefined) {
2581 } else if(document.execCommand !== undefined) {
2582 document.execCommand("Stop", false);
2584 if (document.close !== undefined) {
2587 images.cleanupDone = true;
2588 if (!(cause && typeof cause === "string")) {
2594 renderingDone: function() {
2596 window.clearTimeout(timeoutTimer);
2601 if (options.timeout > 0) {
2602 timeoutTimer = window.setTimeout(methods.cleanupDOM, options.timeout);
2605 Util.log('html2canvas: Preload starts: finding background-images');
2606 images.firstRun = true;
2610 Util.log('html2canvas: Preload: Finding images');
2611 // load <img> images
2612 for (i = 0; i < imgLen; i+=1){
2613 methods.loadImage( domImages[i].getAttribute( "src" ) );
2616 images.firstRun = false;
2617 Util.log('html2canvas: Preload: Done.');
2618 if (images.numTotal === images.numLoaded) {
2625 _html2canvas.Renderer = function(parseQueue, options){
2626 function sortZindex(a, b) {
2627 if (a === 'children') {
2629 } else if (b === 'children') {
2636 // http://www.w3.org/TR/CSS21/zindex.html
2637 function createRenderQueue(parseQueue) {
2641 rootContext = (function buildStackingContext(rootNode) {
2642 var rootContext = {};
2643 function insert(context, node, specialParent) {
2644 var zi = (node.zIndex.zindex === 'auto') ? 0 : Number(node.zIndex.zindex),
2645 contextForChildren = context, // the stacking context for children
2646 isPositioned = node.zIndex.isPositioned,
2647 isFloated = node.zIndex.isFloated,
2648 stub = {node: node},
2649 childrenDest = specialParent; // where children without z-index should be pushed into
2651 if (node.zIndex.ownStacking) {
2652 contextForChildren = stub.context = {
2653 children: [{node:node, children: []}]
2655 childrenDest = undefined;
2656 } else if (isPositioned || isFloated) {
2657 childrenDest = stub.children = [];
2660 if (zi === 0 && specialParent) {
2661 specialParent.push(stub);
2663 if (!context[zi]) { context[zi] = []; }
2664 context[zi].push(stub);
2667 node.zIndex.children.forEach(function(childNode) {
2668 insert(contextForChildren, childNode, childrenDest);
2671 insert(rootContext, rootNode);
2675 function sortZ(context) {
2676 Object.keys(context).sort(sortZindex).forEach(function(zi) {
2677 var nonPositioned = [],
2682 // positioned after static
2683 context[zi].forEach(function(v) {
2684 if (v.node.zIndex.isPositioned || v.node.zIndex.opacity < 1) {
2685 // http://www.w3.org/TR/css3-color/#transparency
2686 // non-positioned element with opactiy < 1 should be stacked as if it were a positioned element with ‘z-index: 0’ and ‘opacity: 1’.
2688 } else if (v.node.zIndex.isFloated) {
2691 nonPositioned.push(v);
2695 (function walk(arr) {
2696 arr.forEach(function(v) {
2698 if (v.children) { walk(v.children); }
2700 })(nonPositioned.concat(floated, positioned));
2702 list.forEach(function(v) {
2717 function getRenderer(rendererName) {
2720 if (typeof options.renderer === "string" && _html2canvas.Renderer[rendererName] !== undefined) {
2721 renderer = _html2canvas.Renderer[rendererName](options);
2722 } else if (typeof rendererName === "function") {
2723 renderer = rendererName(options);
2725 throw new Error("Unknown renderer");
2728 if ( typeof renderer !== "function" ) {
2729 throw new Error("Invalid renderer defined");
2734 return getRenderer(options.renderer)(parseQueue, options, document, createRenderQueue(parseQueue.stack), _html2canvas);
2737 _html2canvas.Util.Support = function (options, doc) {
2739 function supportSVGRendering() {
2740 var img = new Image(),
2741 canvas = doc.createElement("canvas"),
2742 ctx = (canvas.getContext === undefined) ? false : canvas.getContext("2d");
2743 if (ctx === false) {
2746 canvas.width = canvas.height = 10;
2748 "data:image/svg+xml,",
2749 "<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'>",
2750 "<foreignObject width='10' height='10'>",
2751 "<div xmlns='http://www.w3.org/1999/xhtml' style='width:10;height:10;'>",
2758 ctx.drawImage(img, 0, 0);
2763 _html2canvas.Util.log('html2canvas: Parse: SVG powered rendering available');
2767 // Test whether we can use ranges to measure bounding boxes
2768 // Opera doesn't provide valid bounds.height/bottom even though it supports the method.
2770 function supportRangeBounds() {
2771 var r, testElement, rangeBounds, rangeHeight, support = false;
2773 if (doc.createRange) {
2774 r = doc.createRange();
2775 if (r.getBoundingClientRect) {
2776 testElement = doc.createElement('boundtest');
2777 testElement.style.height = "123px";
2778 testElement.style.display = "block";
2779 doc.body.appendChild(testElement);
2781 r.selectNode(testElement);
2782 rangeBounds = r.getBoundingClientRect();
2783 rangeHeight = rangeBounds.height;
2785 if (rangeHeight === 123) {
2788 doc.body.removeChild(testElement);
2796 rangeBounds: supportRangeBounds(),
2797 svgRendering: options.svgRendering && supportSVGRendering()
2800 window.html2canvas = function(elements, opts) {
2801 elements = (elements.length) ? elements : [elements];
2812 timeout: 0, // no timeout
2813 useCORS: false, // try to load images as CORS (where available), before falling back to proxy
2814 allowTaint: false, // whether to allow images to taint the canvas, won't need proxy if set to true
2817 svgRendering: false, // use svg powered rendering where available (FF11+)
2818 ignoreElements: "IFRAME|OBJECT|PARAM",
2820 letterRendering: false,
2822 async: false, // If true, parsing will not block, but if the user scrolls during parse the image can get weird
2827 taintTest: true, // do a taint test with all images before applying to canvas
2831 options = _html2canvas.Util.Extend(opts, options);
2833 _html2canvas.logging = options.logging;
2834 options.complete = function( images ) {
2836 if (typeof options.onpreloaded === "function") {
2837 if ( options.onpreloaded( images ) === false ) {
2841 _html2canvas.Parse( images, options, function(queue) {
2842 if (typeof options.onparsed === "function") {
2843 if ( options.onparsed( queue ) === false ) {
2848 canvas = _html2canvas.Renderer( queue, options );
2850 if (typeof options.onrendered === "function") {
2851 options.onrendered( canvas );
2856 // for pages without images, we still want this to be async, i.e. return methods before executing
2857 window.setTimeout( function(){
2858 _html2canvas.Preload( options );
2862 render: function( queue, opts ) {
2863 return _html2canvas.Renderer( queue, _html2canvas.Util.Extend(opts, options) );
2865 parse: function( images, opts ) {
2866 return _html2canvas.Parse( images, _html2canvas.Util.Extend(opts, options) );
2868 preload: function( opts ) {
2869 return _html2canvas.Preload( _html2canvas.Util.Extend(opts, options) );
2871 log: _html2canvas.Util.log
2875 window.html2canvas.log = _html2canvas.Util.log; // for renderers
2876 window.html2canvas.Renderer = {
2877 Canvas: undefined // We are assuming this will be used
2879 _html2canvas.Renderer.Canvas = function(options) {
2880 options = options || {};
2884 testCanvas = document.createElement("canvas"),
2885 testctx = testCanvas.getContext("2d"),
2886 Util = _html2canvas.Util,
2887 canvas = options.canvas || doc.createElement('canvas');
2889 function createShape(ctx, args) {
2891 args.forEach(function(arg) {
2892 ctx[arg.name].apply(ctx, arg['arguments']);
2897 function safeImage(item) {
2898 if (safeImages.indexOf(item['arguments'][0].src) === -1) {
2899 testctx.drawImage(item['arguments'][0], 0, 0);
2901 testctx.getImageData(0, 0, 1, 1);
2903 testCanvas = doc.createElement("canvas");
2904 testctx = testCanvas.getContext("2d");
2907 safeImages.push(item['arguments'][0].src);
2912 function renderItem(ctx, item) {
2915 ctx[item.name] = item['arguments'];
2919 case "createPattern":
2920 if (item['arguments'][0].width > 0 && item['arguments'][0].height > 0) {
2922 ctx.fillStyle = ctx.createPattern(item['arguments'][0], "repeat");
2924 Util.log("html2canvas: Renderer: Error creating pattern", e.message);
2929 createShape(ctx, item['arguments']);
2932 if (item['arguments'][8] > 0 && item['arguments'][7] > 0) {
2933 if (!options.taintTest || (options.taintTest && safeImage(item))) {
2934 ctx.drawImage.apply( ctx, item['arguments'] );
2939 ctx[item.name].apply(ctx, item['arguments']);
2945 return function(parsedData, options, document, queue, _html2canvas) {
2946 var ctx = canvas.getContext("2d"),
2950 zStack = parsedData.stack;
2952 canvas.width = canvas.style.width = options.width || zStack.ctx.width;
2953 canvas.height = canvas.style.height = options.height || zStack.ctx.height;
2955 fstyle = ctx.fillStyle;
2956 ctx.fillStyle = (Util.isTransparent(parsedData.backgroundColor) && options.background !== undefined) ? options.background : parsedData.backgroundColor;
2957 ctx.fillRect(0, 0, canvas.width, canvas.height);
2958 ctx.fillStyle = fstyle;
2959 queue.forEach(function(storageContext) {
2960 // set common settings for canvas
2961 ctx.textBaseline = "bottom";
2964 if (storageContext.transform.matrix) {
2965 ctx.translate(storageContext.transform.origin[0], storageContext.transform.origin[1]);
2966 ctx.transform.apply(ctx, storageContext.transform.matrix);
2967 ctx.translate(-storageContext.transform.origin[0], -storageContext.transform.origin[1]);
2970 if (storageContext.clip){
2972 ctx.rect(storageContext.clip.left, storageContext.clip.top, storageContext.clip.width, storageContext.clip.height);
2976 if (storageContext.ctx.storage) {
2977 storageContext.ctx.storage.forEach(function(item) {
2978 renderItem(ctx, item);
2985 Util.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj");
2987 if (options.elements.length === 1) {
2988 if (typeof options.elements[0] === "object" && options.elements[0].nodeName !== "BODY") {
2989 // crop image to the bounds of selected (single) element
2990 bounds = _html2canvas.Util.Bounds(options.elements[0]);
2991 newCanvas = document.createElement('canvas');
2994 newCanvas.width = Math.ceil(bounds.width);
2995 newCanvas.height = Math.ceil(bounds.height);
2997 ctx = newCanvas.getContext("2d");
2998 ctx.drawImage(canvas, bounds.left, bounds.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
3010 })(window,document);