2
html2canvas 0.4.0 <http://html2canvas.hertzen.com>
3
Copyright (c) 2013 Niklas von Hertzen (@niklasvh)
5
Released under MIT License
8
(function(window, document, undefined){
12
var _html2canvas = {},
18
if (_html2canvas.logging && window.console && window.console.log) {
19
window.console.log(a);
23
_html2canvas.Util = {};
25
_html2canvas.Util.trimText = (function(isNative){
26
return function(input){
27
if(isNative) { return isNative.apply( input ); }
28
else { return ((input || '') + '').replace( /^\s+|\s+$/g , '' ); }
30
})( String.prototype.trim );
32
_html2canvas.Util.parseBackgroundImage = function (value) {
33
var whitespace = ' \r\n\t',
34
method, definition, prefix, prefix_i, block, results = [],
35
c, mode = 0, numParen = 0, quote, args;
37
var appendResult = function(){
39
if(definition.substr( 0, 1 ) === '"') {
40
definition = definition.substr( 1, definition.length - 2 );
43
args.push(definition);
45
if(method.substr( 0, 1 ) === '-' &&
46
(prefix_i = method.indexOf( '-', 1 ) + 1) > 0) {
47
prefix = method.substr( 0, prefix_i);
48
method = method.substr( prefix_i );
52
method: method.toLowerCase(),
57
args = []; //for some odd reason, setting .length = 0 didn't work in safari
65
for(var i = 0, ii = value.length; i<ii; i++) {
67
if(mode === 0 && whitespace.indexOf( c ) > -1){
75
else if(quote === c) {
107
else if(mode === 0) {
111
else if (mode === 1) {
112
if(numParen === 0 && !method.match(/^url$/i)) {
113
args.push(definition);
123
if(mode === 0) { method += c; }
124
else { definition += c; }
131
_html2canvas.Util.Bounds = function getBounds (el) {
135
if (el.getBoundingClientRect){
136
clientRect = el.getBoundingClientRect();
139
// TODO add scroll position to bounds, so no scrolling of window necessary
140
bounds.top = clientRect.top;
141
bounds.bottom = clientRect.bottom || (clientRect.top + clientRect.height);
142
bounds.left = clientRect.left;
144
// older IE doesn't have width/height, but top/bottom instead
145
bounds.width = clientRect.width || (clientRect.right - clientRect.left);
146
bounds.height = clientRect.height || (clientRect.bottom - clientRect.top);
153
_html2canvas.Util.getCSS = function (el, attribute, index) {
154
// return $(el).css(attribute);
157
isBackgroundSizePosition = attribute.match( /^background(Size|Position)$/ );
159
function toPX( attribute, val ) {
160
var rsLeft = el.runtimeStyle && el.runtimeStyle[ attribute ],
164
// Check if we are not dealing with pixels, (Opera has issues with this)
165
// Ported from jQuery css.js
166
// From the awesome hack by Dean Edwards
167
// http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
169
// If we're not dealing with a regular pixel number
170
// but a number that has a weird ending, we need to convert it to pixels
172
if ( !/^-?[0-9]+\.?[0-9]*(?:px)?$/i.test( val ) && /^-?\d/.test( val ) ) {
174
// Remember the original values
177
// Put in the new values to get a computed value out
179
el.runtimeStyle.left = el.currentStyle.left;
181
style.left = attribute === "fontSize" ? "1em" : (val || 0);
182
val = style.pixelLeft + "px";
184
// Revert the changed values
187
el.runtimeStyle.left = rsLeft;
192
if (!/^(thin|medium|thick)$/i.test( val )) {
193
return Math.round(parseFloat( val )) + "px";
199
if (previousElement !== el) {
200
computedCSS = document.defaultView.getComputedStyle(el, null);
202
val = computedCSS[attribute];
204
if (isBackgroundSizePosition) {
205
val = (val || '').split( ',' );
206
val = val[index || 0] || val[0] || 'auto';
207
val = _html2canvas.Util.trimText(val).split(' ');
209
if(attribute === 'backgroundSize' && (!val[ 0 ] || val[ 0 ].match( /cover|contain|auto/ ))) {
210
//these values will be handled in the parent function
213
val[ 0 ] = ( val[ 0 ].indexOf( "%" ) === -1 ) ? toPX( attribute + "X", val[ 0 ] ) : val[ 0 ];
214
if(val[ 1 ] === undefined) {
215
if(attribute === 'backgroundSize') {
220
// IE 9 doesn't return double digit always
224
val[ 1 ] = ( val[ 1 ].indexOf( "%" ) === -1 ) ? toPX( attribute + "Y", val[ 1 ] ) : val[ 1 ];
226
} else if ( /border(Top|Bottom)(Left|Right)Radius/.test( attribute) ) {
227
var arr = val.split(" ");
228
if ( arr.length <= 1 ) {
231
arr[ 0 ] = parseInt( arr[ 0 ], 10 );
232
arr[ 1 ] = parseInt( arr[ 1 ], 10 );
239
_html2canvas.Util.resizeBounds = function( current_width, current_height, target_width, target_height, stretch_mode ){
240
var target_ratio = target_width / target_height,
241
current_ratio = current_width / current_height,
242
output_width, output_height;
244
if(!stretch_mode || stretch_mode === 'auto') {
245
output_width = target_width;
246
output_height = target_height;
249
if(target_ratio < current_ratio ^ stretch_mode === 'contain') {
250
output_height = target_height;
251
output_width = target_height * current_ratio;
253
output_width = target_width;
254
output_height = target_width / current_ratio;
258
return { width: output_width, height: output_height };
261
function backgroundBoundsFactory( prop, el, bounds, image, imageIndex, backgroundSize ) {
262
var bgposition = _html2canvas.Util.getCSS( el, prop, imageIndex ) ,
268
if (bgposition.length === 1){
277
if (bgposition[0].toString().indexOf("%") !== -1){
278
percentage = (parseFloat(bgposition[0])/100);
279
left = bounds.width * percentage;
280
if(prop !== 'backgroundSize') {
281
left -= (backgroundSize || image).width*percentage;
285
if(prop === 'backgroundSize') {
286
if(bgposition[0] === 'auto') {
290
if(bgposition[0].match(/contain|cover/)) {
291
var resized = _html2canvas.Util.resizeBounds( image.width, image.height, bounds.width, bounds.height, bgposition[0] );
292
left = resized.width;
293
topPos = resized.height;
295
left = parseInt (bgposition[0], 10 );
300
left = parseInt( bgposition[0], 10 );
305
if(bgposition[1] === 'auto') {
306
topPos = left / image.width * image.height;
307
} else if (bgposition[1].toString().indexOf("%") !== -1){
308
percentage = (parseFloat(bgposition[1])/100);
309
topPos = bounds.height * percentage;
310
if(prop !== 'backgroundSize') {
311
topPos -= (backgroundSize || image).height * percentage;
315
topPos = parseInt(bgposition[1],10);
318
return [left, topPos];
321
_html2canvas.Util.BackgroundPosition = function( el, bounds, image, imageIndex, backgroundSize ) {
322
var result = backgroundBoundsFactory( 'backgroundPosition', el, bounds, image, imageIndex, backgroundSize );
323
return { left: result[0], top: result[1] };
325
_html2canvas.Util.BackgroundSize = function( el, bounds, image, imageIndex ) {
326
var result = backgroundBoundsFactory( 'backgroundSize', el, bounds, image, imageIndex );
327
return { width: result[0], height: result[1] };
330
_html2canvas.Util.Extend = function (options, defaults) {
331
for (var key in options) {
332
if (options.hasOwnProperty(key)) {
333
defaults[key] = options[key];
341
* Derived from jQuery.contents()
342
* Copyright 2010, John Resig
343
* Dual licensed under the MIT or GPL Version 2 licenses.
344
* http://jquery.org/license
346
_html2canvas.Util.Children = function( elem ) {
352
children = (elem.nodeName && elem.nodeName.toUpperCase() === "IFRAME") ?
353
elem.contentDocument || elem.contentWindow.document : (function( array ){
356
if ( array !== null ) {
358
(function( first, second ) {
359
var i = first.length,
362
if ( typeof second.length === "number" ) {
363
for ( var l = second.length; j < l; j++ ) {
364
first[ i++ ] = second[ j ];
368
while ( second[j] !== undefined ) {
369
first[ i++ ] = second[ j++ ];
381
})( elem.childNodes );
384
h2clog("html2canvas.Util.Children failed with exception: " + ex.message);
390
_html2canvas.Util.Font = (function () {
394
return function(font, fontSize, doc) {
395
if (fontData[font + "-" + fontSize] !== undefined) {
396
return fontData[font + "-" + fontSize];
399
var container = doc.createElement('div'),
400
img = doc.createElement('img'),
401
span = doc.createElement('span'),
402
sampleText = 'Hidden Text',
407
container.style.visibility = "hidden";
408
container.style.fontFamily = font;
409
container.style.fontSize = fontSize;
410
container.style.margin = 0;
411
container.style.padding = 0;
413
doc.body.appendChild(container);
415
// http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever (handtinywhite.gif)
416
img.src = "data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=";
420
img.style.margin = 0;
421
img.style.padding = 0;
422
img.style.verticalAlign = "baseline";
424
span.style.fontFamily = font;
425
span.style.fontSize = fontSize;
426
span.style.margin = 0;
427
span.style.padding = 0;
429
span.appendChild(doc.createTextNode(sampleText));
430
container.appendChild(span);
431
container.appendChild(img);
432
baseline = (img.offsetTop - span.offsetTop) + 1;
434
container.removeChild(span);
435
container.appendChild(doc.createTextNode(sampleText));
437
container.style.lineHeight = "normal";
438
img.style.verticalAlign = "super";
440
middle = (img.offsetTop-container.offsetTop) + 1;
447
fontData[font + "-" + fontSize] = metricsObj;
449
doc.body.removeChild(container);
457
_html2canvas.Generate = {};
460
/^(-webkit-linear-gradient)\(([a-z\s]+)([\w\d\.\s,%\(\)]+)\)$/,
461
/^(-o-linear-gradient)\(([a-z\s]+)([\w\d\.\s,%\(\)]+)\)$/,
462
/^(-webkit-gradient)\((linear|radial),\s((?:\d{1,3}%?)\s(?:\d{1,3}%?),\s(?:\d{1,3}%?)\s(?:\d{1,3}%?))([\w\d\.\s,%\(\)\-]+)\)$/,
463
/^(-moz-linear-gradient)\(((?:\d{1,3}%?)\s(?:\d{1,3}%?))([\w\d\.\s,%\(\)]+)\)$/,
464
/^(-webkit-radial-gradient)\(((?:\d{1,3}%?)\s(?:\d{1,3}%?)),\s(\w+)\s([a-z\-]+)([\w\d\.\s,%\(\)]+)\)$/,
465
/^(-moz-radial-gradient)\(((?:\d{1,3}%?)\s(?:\d{1,3}%?)),\s(\w+)\s?([a-z\-]*)([\w\d\.\s,%\(\)]+)\)$/,
466
/^(-o-radial-gradient)\(((?:\d{1,3}%?)\s(?:\d{1,3}%?)),\s(\w+)\s([a-z\-]+)([\w\d\.\s,%\(\)]+)\)$/
470
* TODO: Add IE10 vendor prefix (-ms) support
471
* TODO: Add W3C gradient (linear-gradient) support
472
* TODO: Add old Webkit -webkit-gradient(radial, ...) support
473
* TODO: Maybe some RegExp optimizations are possible ;o)
475
_html2canvas.Generate.parseGradient = function(css, bounds) {
476
var gradient, i, len = reGradients.length, m1, stop, m2, m2Len, step, m3, tl,tr,br,bl;
478
for(i = 0; i < len; i+=1){
479
m1 = css.match(reGradients[i]);
487
case '-webkit-linear-gradient':
488
case '-o-linear-gradient':
500
m2 = m1[2].match(/\w+/g);
503
for(i = 0; i < m2Len; i+=1){
507
gradient.y1 = bounds.height;
511
gradient.x0 = bounds.width;
516
gradient.y0 = bounds.height;
522
gradient.x1 = bounds.width;
527
if(gradient.x0 === null && gradient.x1 === null){ // center
528
gradient.x0 = gradient.x1 = bounds.width / 2;
530
if(gradient.y0 === null && gradient.y1 === null){ // center
531
gradient.y0 = gradient.y1 = bounds.height / 2;
534
// get colors and stops
535
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);
538
step = 1 / Math.max(m2Len - 1, 1);
539
for(i = 0; i < m2Len; i+=1){
540
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)?/);
542
stop = parseFloat(m3[2]);
545
} else { // px - stupid opera
546
stop /= bounds.width;
551
gradient.colorStops.push({
559
case '-webkit-gradient':
562
type: m1[2] === 'radial' ? 'circle' : m1[2], // TODO: Add radial gradient support for older mozilla definitions
571
m2 = m1[3].match(/(\d{1,3})%?\s(\d{1,3})%?,\s(\d{1,3})%?\s(\d{1,3})%?/);
573
gradient.x0 = (m2[1] * bounds.width) / 100;
574
gradient.y0 = (m2[2] * bounds.height) / 100;
575
gradient.x1 = (m2[3] * bounds.width) / 100;
576
gradient.y1 = (m2[4] * bounds.height) / 100;
579
// get colors and stops
580
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);
583
for(i = 0; i < m2Len; i+=1){
584
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\.]+)?\))\)/);
585
stop = parseFloat(m3[2]);
586
if(m3[1] === 'from') {
592
gradient.colorStops.push({
600
case '-moz-linear-gradient':
612
m2 = m1[2].match(/(\d{1,3})%?\s(\d{1,3})%?/);
614
// m2[1] == 0% -> left
615
// m2[1] == 50% -> center
616
// m2[1] == 100% -> right
618
// m2[2] == 0% -> top
619
// m2[2] == 50% -> center
620
// m2[2] == 100% -> bottom
623
gradient.x0 = (m2[1] * bounds.width) / 100;
624
gradient.y0 = (m2[2] * bounds.height) / 100;
625
gradient.x1 = bounds.width - gradient.x0;
626
gradient.y1 = bounds.height - gradient.y0;
629
// get colors and stops
630
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);
633
step = 1 / Math.max(m2Len - 1, 1);
634
for(i = 0; i < m2Len; i+=1){
635
m3 = m2[i].match(/((?:rgb|rgba)\(\d{1,3},\s\d{1,3},\s\d{1,3}(?:,\s[0-9\.]+)?\))\s*(\d{1,3})?(%)?/);
637
stop = parseFloat(m3[2]);
638
if(m3[3]){ // percentage
644
gradient.colorStops.push({
652
case '-webkit-radial-gradient':
653
case '-moz-radial-gradient':
654
case '-o-radial-gradient':
670
m2 = m1[2].match(/(\d{1,3})%?\s(\d{1,3})%?/);
672
gradient.cx = (m2[1] * bounds.width) / 100;
673
gradient.cy = (m2[2] * bounds.height) / 100;
677
m2 = m1[3].match(/\w+/);
678
m3 = m1[4].match(/[a-z\-]*/);
681
case 'farthest-corner':
682
case 'cover': // is equivalent to farthest-corner
683
case '': // mozilla removes "cover" from definition :(
684
tl = Math.sqrt(Math.pow(gradient.cx, 2) + Math.pow(gradient.cy, 2));
685
tr = Math.sqrt(Math.pow(gradient.cx, 2) + Math.pow(gradient.y1 - gradient.cy, 2));
686
br = Math.sqrt(Math.pow(gradient.x1 - gradient.cx, 2) + Math.pow(gradient.y1 - gradient.cy, 2));
687
bl = Math.sqrt(Math.pow(gradient.x1 - gradient.cx, 2) + Math.pow(gradient.cy, 2));
688
gradient.rx = gradient.ry = Math.max(tl, tr, br, bl);
690
case 'closest-corner':
691
tl = Math.sqrt(Math.pow(gradient.cx, 2) + Math.pow(gradient.cy, 2));
692
tr = Math.sqrt(Math.pow(gradient.cx, 2) + Math.pow(gradient.y1 - gradient.cy, 2));
693
br = Math.sqrt(Math.pow(gradient.x1 - gradient.cx, 2) + Math.pow(gradient.y1 - gradient.cy, 2));
694
bl = Math.sqrt(Math.pow(gradient.x1 - gradient.cx, 2) + Math.pow(gradient.cy, 2));
695
gradient.rx = gradient.ry = Math.min(tl, tr, br, bl);
697
case 'farthest-side':
698
if(m2[0] === 'circle'){
699
gradient.rx = gradient.ry = Math.max(
702
gradient.x1 - gradient.cx,
703
gradient.y1 - gradient.cy
707
gradient.type = m2[0];
709
gradient.rx = Math.max(
711
gradient.x1 - gradient.cx
713
gradient.ry = Math.max(
715
gradient.y1 - gradient.cy
720
case 'contain': // is equivalent to closest-side
721
if(m2[0] === 'circle'){
722
gradient.rx = gradient.ry = Math.min(
725
gradient.x1 - gradient.cx,
726
gradient.y1 - gradient.cy
730
gradient.type = m2[0];
732
gradient.rx = Math.min(
734
gradient.x1 - gradient.cx
736
gradient.ry = Math.min(
738
gradient.y1 - gradient.cy
743
// TODO: add support for "30px 40px" sizes (webkit only)
748
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);
751
step = 1 / Math.max(m2Len - 1, 1);
752
for(i = 0; i < m2Len; i+=1){
753
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)?/);
755
stop = parseFloat(m3[2]);
758
} else { // px - stupid opera
759
stop /= bounds.width;
764
gradient.colorStops.push({
777
_html2canvas.Generate.Gradient = function(src, bounds) {
778
if(bounds.width === 0 || bounds.height === 0) {
782
var canvas = document.createElement('canvas'),
783
ctx = canvas.getContext('2d'),
784
gradient, grad, i, len;
786
canvas.width = bounds.width;
787
canvas.height = bounds.height;
789
// TODO: add support for multi defined background gradients
790
gradient = _html2canvas.Generate.parseGradient(src, bounds);
793
if(gradient.type === 'linear') {
794
grad = ctx.createLinearGradient(gradient.x0, gradient.y0, gradient.x1, gradient.y1);
796
for (i = 0, len = gradient.colorStops.length; i < len; i+=1) {
798
grad.addColorStop(gradient.colorStops[i].stop, gradient.colorStops[i].color);
801
h2clog(['failed to add color stop: ', e, '; tried to add: ', gradient.colorStops[i], '; stop: ', i, '; in: ', src]);
805
ctx.fillStyle = grad;
806
ctx.fillRect(0, 0, bounds.width, bounds.height);
808
} else if(gradient.type === 'circle') {
810
grad = ctx.createRadialGradient(gradient.cx, gradient.cy, 0, gradient.cx, gradient.cy, gradient.rx);
812
for (i = 0, len = gradient.colorStops.length; i < len; i+=1) {
814
grad.addColorStop(gradient.colorStops[i].stop, gradient.colorStops[i].color);
817
h2clog(['failed to add color stop: ', e, '; tried to add: ', gradient.colorStops[i], '; stop: ', i, '; in: ', src]);
821
ctx.fillStyle = grad;
822
ctx.fillRect(0, 0, bounds.width, bounds.height);
824
} else if(gradient.type === 'ellipse') {
827
var canvasRadial = document.createElement('canvas'),
828
ctxRadial = canvasRadial.getContext('2d'),
829
ri = Math.max(gradient.rx, gradient.ry),
830
di = ri * 2, imgRadial;
832
canvasRadial.width = canvasRadial.height = di;
834
grad = ctxRadial.createRadialGradient(gradient.rx, gradient.ry, 0, gradient.rx, gradient.ry, ri);
836
for (i = 0, len = gradient.colorStops.length; i < len; i+=1) {
838
grad.addColorStop(gradient.colorStops[i].stop, gradient.colorStops[i].color);
841
h2clog(['failed to add color stop: ', e, '; tried to add: ', gradient.colorStops[i], '; stop: ', i, '; in: ', src]);
845
ctxRadial.fillStyle = grad;
846
ctxRadial.fillRect(0, 0, di, di);
848
ctx.fillStyle = gradient.colorStops[i - 1].color;
849
ctx.fillRect(0, 0, canvas.width, canvas.height);
850
ctx.drawImage(canvasRadial, gradient.cx - gradient.rx, gradient.cy - gradient.ry, 2 * gradient.rx, 2 * gradient.ry);
858
_html2canvas.Generate.ListAlpha = function(number) {
863
modulus = number % 26;
864
tmp = String.fromCharCode((modulus) + 64) + tmp;
865
number = number / 26;
866
}while((number*26) > 26);
871
_html2canvas.Generate.ListRoman = function(number) {
872
var romanArray = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"],
873
decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1],
876
len = romanArray.length;
878
if (number <= 0 || number >= 4000) {
882
for (v=0; v < len; v+=1) {
883
while (number >= decimal[v]) {
884
number -= decimal[v];
885
roman += romanArray[v];
894
_html2canvas.Parse = function (images, options) {
897
var element = (( options.elements === undefined ) ? document.body : options.elements[0]), // select body by default
899
doc = element.ownerDocument,
900
support = _html2canvas.Util.Support(options, doc),
901
ignoreElementsRegExp = new RegExp("(" + options.ignoreElements + ")"),
903
getCSS = _html2canvas.Util.getCSS,
904
pseudoHide = "___html2canvas___pseudoelement",
905
hidePseudoElements = doc.createElement('style');
907
hidePseudoElements.innerHTML = '.' + pseudoHide + '-before:before { content: "" !important; display: none !important; }' +
908
'.' + pseudoHide + '-after:after { content: "" !important; display: none !important; }';
910
body.appendChild(hidePseudoElements);
912
images = images || {};
914
function documentWidth () {
916
Math.max(doc.body.scrollWidth, doc.documentElement.scrollWidth),
917
Math.max(doc.body.offsetWidth, doc.documentElement.offsetWidth),
918
Math.max(doc.body.clientWidth, doc.documentElement.clientWidth)
922
function documentHeight () {
924
Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight),
925
Math.max(doc.body.offsetHeight, doc.documentElement.offsetHeight),
926
Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
930
function getCSSInt(element, attribute) {
931
var val = parseInt(getCSS(element, attribute), 10);
932
return (isNaN(val)) ? 0 : val; // borders in old IE are throwing 'medium' for demo.html
935
function renderRect (ctx, x, y, w, h, bgcolor) {
936
if (bgcolor !== "transparent"){
937
ctx.setVariable("fillStyle", bgcolor);
938
ctx.fillRect(x, y, w, h);
943
function textTransform (text, transform) {
946
return text.toLowerCase();
948
return text.replace( /(^|\s|:|-|\(|\))([a-z])/g , function (m, p1, p2) {
950
return p1 + p2.toUpperCase();
954
return text.toUpperCase();
960
function noLetterSpacing(letter_spacing) {
961
return (/^(normal|none|0px)$/.test(letter_spacing));
964
function drawText(currentText, x, y, ctx){
965
if (currentText !== null && _html2canvas.Util.trimText(currentText).length > 0) {
966
ctx.fillText(currentText, x, y);
971
function setTextVariables(ctx, el, text_decoration, color) {
973
bold = getCSS(el, "fontWeight"),
974
family = getCSS(el, "fontFamily"),
975
size = getCSS(el, "fontSize");
977
switch(parseInt(bold, 10)){
986
ctx.setVariable("fillStyle", color);
987
ctx.setVariable("font", [getCSS(el, "fontStyle"), getCSS(el, "fontVariant"), bold, size, family].join(" "));
988
ctx.setVariable("textAlign", (align) ? "right" : "left");
990
if (text_decoration !== "none"){
991
return _html2canvas.Util.Font(family, size, doc);
995
function renderTextDecoration(ctx, text_decoration, bounds, metrics, color) {
996
switch(text_decoration) {
998
// Draws a line at the baseline of the font
999
// 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
1000
renderRect(ctx, bounds.left, Math.round(bounds.top + metrics.baseline + metrics.lineWidth), bounds.width, 1, color);
1003
renderRect(ctx, bounds.left, Math.round(bounds.top), bounds.width, 1, color);
1005
case "line-through":
1006
// TODO try and find exact position for line-through
1007
renderRect(ctx, bounds.left, Math.ceil(bounds.top + metrics.middle + metrics.lineWidth), bounds.width, 1, color);
1012
function getTextBounds(state, text, textDecoration, isLast) {
1014
if (support.rangeBounds) {
1015
if (textDecoration !== "none" || _html2canvas.Util.trimText(text).length !== 0) {
1016
bounds = textRangeBounds(text, state.node, state.textOffset);
1018
state.textOffset += text.length;
1019
} else if (state.node && typeof state.node.nodeValue === "string" ){
1020
var newTextNode = (isLast) ? state.node.splitText(text.length) : null;
1021
bounds = textWrapperBounds(state.node);
1022
state.node = newTextNode;
1027
function textRangeBounds(text, textNode, textOffset) {
1028
var range = doc.createRange();
1029
range.setStart(textNode, textOffset);
1030
range.setEnd(textNode, textOffset + text.length);
1031
return range.getBoundingClientRect();
1034
function textWrapperBounds(oldTextNode) {
1035
var parent = oldTextNode.parentNode,
1036
wrapElement = doc.createElement('wrapper'),
1037
backupText = oldTextNode.cloneNode(true);
1039
wrapElement.appendChild(oldTextNode.cloneNode(true));
1040
parent.replaceChild(wrapElement, oldTextNode);
1042
var bounds = _html2canvas.Util.Bounds(wrapElement);
1043
parent.replaceChild(backupText, wrapElement);
1047
function renderText(el, textNode, stack) {
1048
var ctx = stack.ctx,
1049
color = getCSS(el, "color"),
1050
textDecoration = getCSS(el, "textDecoration"),
1051
textAlign = getCSS(el, "textAlign"),
1059
if (_html2canvas.Util.trimText(textNode.nodeValue).length > 0) {
1060
textNode.nodeValue = textTransform(textNode.nodeValue, getCSS(el, "textTransform"));
1061
textAlign = textAlign.replace(["-webkit-auto"],["auto"]);
1063
textList = (!options.letterRendering && /^(left|right|justify|auto)$/.test(textAlign) && noLetterSpacing(getCSS(el, "letterSpacing"))) ?
1064
textNode.nodeValue.split(/(\b| )/)
1065
: textNode.nodeValue.split("");
1067
metrics = setTextVariables(ctx, el, textDecoration, color);
1069
if (options.chinese) {
1070
textList.forEach(function(word, index) {
1071
if (/.*[\u4E00-\u9FA5].*$/.test(word)) {
1072
word = word.split("");
1073
word.unshift(index, 1);
1074
textList.splice.apply(textList, word);
1079
textList.forEach(function(text, index) {
1080
var bounds = getTextBounds(state, text, textDecoration, (index < textList.length - 1));
1082
drawText(text, bounds.left, bounds.bottom, ctx);
1083
renderTextDecoration(ctx, textDecoration, bounds, metrics, color);
1089
function listPosition (element, val) {
1090
var boundElement = doc.createElement( "boundelement" ),
1094
boundElement.style.display = "inline";
1096
originalType = element.style.listStyleType;
1097
element.style.listStyleType = "none";
1099
boundElement.appendChild(doc.createTextNode(val));
1101
element.insertBefore(boundElement, element.firstChild);
1103
bounds = _html2canvas.Util.Bounds(boundElement);
1104
element.removeChild(boundElement);
1105
element.style.listStyleType = originalType;
1109
function elementIndex( el ) {
1112
childs = el.parentNode.childNodes;
1114
if (el.parentNode) {
1115
while( childs[ ++i ] !== el ) {
1116
if ( childs[ i ].nodeType === 1 ) {
1126
function listItemText(element, type) {
1127
var currentIndex = elementIndex(element),
1131
text = currentIndex;
1133
case "decimal-leading-zero":
1134
text = (currentIndex.toString().length === 1) ? currentIndex = "0" + currentIndex.toString() : currentIndex.toString();
1137
text = _html2canvas.Generate.ListRoman( currentIndex );
1140
text = _html2canvas.Generate.ListRoman( currentIndex ).toLowerCase();
1143
text = _html2canvas.Generate.ListAlpha( currentIndex ).toLowerCase();
1146
text = _html2canvas.Generate.ListAlpha( currentIndex );
1154
function renderListItem(element, stack, elBounds) {
1158
type = getCSS(element, "listStyleType"),
1161
if (/^(decimal|decimal-leading-zero|upper-alpha|upper-latin|upper-roman|lower-alpha|lower-greek|lower-latin|lower-roman)$/i.test(type)) {
1162
text = listItemText(element, type);
1163
listBounds = listPosition(element, text);
1164
setTextVariables(ctx, element, "none", getCSS(element, "color"));
1166
if (getCSS(element, "listStylePosition") === "inside") {
1167
ctx.setVariable("textAlign", "left");
1173
drawText(text, x, listBounds.bottom, ctx);
1177
function loadImage (src){
1178
var img = images[src];
1179
if (img && img.succeeded === true) {
1186
function clipBounds(src, dst){
1187
var x = Math.max(src.left, dst.left),
1188
y = Math.max(src.top, dst.top),
1189
x2 = Math.min((src.left + src.width), (dst.left + dst.width)),
1190
y2 = Math.min((src.top + src.height), (dst.top + dst.height));
1200
function setZ(zIndex, parentZ){
1201
// TODO fix static elements overlapping relative/absolute elements under same stack, if they are defined after them
1204
newContext = h2czContext(0);
1208
if (zIndex !== "auto"){
1209
newContext = h2czContext(zIndex);
1210
parentZ.children.push(newContext);
1218
function renderImage(ctx, element, image, bounds, borders) {
1220
var paddingLeft = getCSSInt(element, 'paddingLeft'),
1221
paddingTop = getCSSInt(element, 'paddingTop'),
1222
paddingRight = getCSSInt(element, 'paddingRight'),
1223
paddingBottom = getCSSInt(element, 'paddingBottom');
1232
bounds.left + paddingLeft + borders[3].width, //dx
1233
bounds.top + paddingTop + borders[0].width, // dy
1234
bounds.width - (borders[1].width + borders[3].width + paddingLeft + paddingRight), //dw
1235
bounds.height - (borders[0].width + borders[2].width + paddingTop + paddingBottom) //dh
1239
function getBorderData(element) {
1240
return ["Top", "Right", "Bottom", "Left"].map(function(side) {
1242
width: getCSSInt(element, 'border' + side + 'Width'),
1243
color: getCSS(element, 'border' + side + 'Color')
1248
function getBorderRadiusData(element) {
1249
return ["TopLeft", "TopRight", "BottomRight", "BottomLeft"].map(function(side) {
1250
return getCSS(element, 'border' + side + 'Radius');
1254
var getCurvePoints = (function(kappa) {
1256
return function(x, y, r1, r2) {
1257
var ox = (r1) * kappa, // control point offset horizontal
1258
oy = (r2) * kappa, // control point offset vertical
1259
xm = x + r1, // x-middle
1260
ym = y + r2; // y-middle
1262
topLeft: bezierCurve({
1275
topRight: bezierCurve({
1288
bottomRight: bezierCurve({
1301
bottomLeft: bezierCurve({
1316
})(4 * ((Math.sqrt(2) - 1) / 3));
1318
function bezierCurve(start, startControl, endControl, end) {
1320
var lerp = function (a, b, t) {
1322
x:a.x + (b.x - a.x) * t,
1323
y:a.y + (b.y - a.y) * t
1329
startControl: startControl,
1330
endControl: endControl,
1332
subdivide: function(t) {
1333
var ab = lerp(start, startControl, t),
1334
bc = lerp(startControl, endControl, t),
1335
cd = lerp(endControl, end, t),
1336
abbc = lerp(ab, bc, t),
1337
bccd = lerp(bc, cd, t),
1338
dest = lerp(abbc, bccd, t);
1339
return [bezierCurve(start, ab, abbc, dest), bezierCurve(dest, bccd, cd, end)];
1341
curveTo: function(borderArgs) {
1342
borderArgs.push(["bezierCurve", startControl.x, startControl.y, endControl.x, endControl.y, end.x, end.y]);
1344
curveToReversed: function(borderArgs) {
1345
borderArgs.push(["bezierCurve", endControl.x, endControl.y, startControl.x, startControl.y, start.x, start.y]);
1350
function parseCorner(borderArgs, radius1, radius2, corner1, corner2, x, y) {
1351
if (radius1[0] > 0 || radius1[1] > 0) {
1352
borderArgs.push(["line", corner1[0].start.x, corner1[0].start.y]);
1353
corner1[0].curveTo(borderArgs);
1354
corner1[1].curveTo(borderArgs);
1356
borderArgs.push(["line", x, y]);
1359
if (radius2[0] > 0 || radius2[1] > 0) {
1360
borderArgs.push(["line", corner2[0].start.x, corner2[0].start.y]);
1364
function drawSide(borderData, radius1, radius2, outer1, inner1, outer2, inner2) {
1365
var borderArgs = [];
1367
if (radius1[0] > 0 || radius1[1] > 0) {
1368
borderArgs.push(["line", outer1[1].start.x, outer1[1].start.y]);
1369
outer1[1].curveTo(borderArgs);
1371
borderArgs.push([ "line", borderData.c1[0], borderData.c1[1]]);
1374
if (radius2[0] > 0 || radius2[1] > 0) {
1375
borderArgs.push(["line", outer2[0].start.x, outer2[0].start.y]);
1376
outer2[0].curveTo(borderArgs);
1377
borderArgs.push(["line", inner2[0].end.x, inner2[0].end.y]);
1378
inner2[0].curveToReversed(borderArgs);
1380
borderArgs.push([ "line", borderData.c2[0], borderData.c2[1]]);
1381
borderArgs.push([ "line", borderData.c3[0], borderData.c3[1]]);
1384
if (radius1[0] > 0 || radius1[1] > 0) {
1385
borderArgs.push(["line", inner1[1].end.x, inner1[1].end.y]);
1386
inner1[1].curveToReversed(borderArgs);
1388
borderArgs.push([ "line", borderData.c4[0], borderData.c4[1]]);
1394
function calculateCurvePoints(bounds, borderRadius, borders) {
1396
var x = bounds.left,
1398
width = bounds.width,
1399
height = bounds.height,
1401
tlh = borderRadius[0][0],
1402
tlv = borderRadius[0][1],
1403
trh = borderRadius[1][0],
1404
trv = borderRadius[1][1],
1405
brv = borderRadius[2][0],
1406
brh = borderRadius[2][1],
1407
blh = borderRadius[3][0],
1408
blv = borderRadius[3][1],
1410
topWidth = width - trh,
1411
rightHeight = height - brv,
1412
bottomWidth = width - brh,
1413
leftHeight = height - blv;
1416
topLeftOuter: getCurvePoints(
1421
).topLeft.subdivide(0.5),
1423
topLeftInner: getCurvePoints(
1424
x + borders[3].width,
1425
y + borders[0].width,
1426
Math.max(0, tlh - borders[3].width),
1427
Math.max(0, tlv - borders[0].width)
1428
).topLeft.subdivide(0.5),
1430
topRightOuter: getCurvePoints(
1435
).topRight.subdivide(0.5),
1437
topRightInner: getCurvePoints(
1438
x + Math.min(topWidth, width + borders[3].width),
1439
y + borders[0].width,
1440
(topWidth > width + borders[3].width) ? 0 :trh - borders[3].width,
1441
trv - borders[0].width
1442
).topRight.subdivide(0.5),
1444
bottomRightOuter: getCurvePoints(
1449
).bottomRight.subdivide(0.5),
1451
bottomRightInner: getCurvePoints(
1452
x + Math.min(bottomWidth, width + borders[3].width),
1453
y + Math.min(rightHeight, height + borders[0].width),
1454
Math.max(0, brh - borders[1].width),
1455
Math.max(0, brv - borders[2].width)
1456
).bottomRight.subdivide(0.5),
1458
bottomLeftOuter: getCurvePoints(
1463
).bottomLeft.subdivide(0.5),
1465
bottomLeftInner: getCurvePoints(
1466
x + borders[3].width,
1468
Math.max(0, blh - borders[3].width),
1469
Math.max(0, blv - borders[2].width)
1470
).bottomLeft.subdivide(0.5)
1474
function getBorderClip(element, borderPoints, borders, radius, bounds) {
1475
var backgroundClip = getCSS(element, 'backgroundClip'),
1478
switch(backgroundClip) {
1481
parseCorner(borderArgs, radius[0], radius[1], borderPoints.topLeftInner, borderPoints.topRightInner, bounds.left + borders[3].width, bounds.top + borders[0].width);
1482
parseCorner(borderArgs, radius[1], radius[2], borderPoints.topRightInner, borderPoints.bottomRightInner, bounds.left + bounds.width - borders[1].width, bounds.top + borders[0].width);
1483
parseCorner(borderArgs, radius[2], radius[3], borderPoints.bottomRightInner, borderPoints.bottomLeftInner, bounds.left + bounds.width - borders[1].width, bounds.top + bounds.height - borders[2].width);
1484
parseCorner(borderArgs, radius[3], radius[0], borderPoints.bottomLeftInner, borderPoints.topLeftInner, bounds.left + borders[3].width, bounds.top + bounds.height - borders[2].width);
1488
parseCorner(borderArgs, radius[0], radius[1], borderPoints.topLeftOuter, borderPoints.topRightOuter, bounds.left, bounds.top);
1489
parseCorner(borderArgs, radius[1], radius[2], borderPoints.topRightOuter, borderPoints.bottomRightOuter, bounds.left + bounds.width, bounds.top);
1490
parseCorner(borderArgs, radius[2], radius[3], borderPoints.bottomRightOuter, borderPoints.bottomLeftOuter, bounds.left + bounds.width, bounds.top + bounds.height);
1491
parseCorner(borderArgs, radius[3], radius[0], borderPoints.bottomLeftOuter, borderPoints.topLeftOuter, bounds.left, bounds.top + bounds.height);
1498
function parseBorders(element, bounds, borders){
1499
var x = bounds.left,
1501
width = bounds.width,
1502
height = bounds.height,
1509
// http://www.w3.org/TR/css3-background/#the-border-radius
1510
borderRadius = getBorderRadiusData(element),
1511
borderPoints = calculateCurvePoints(bounds, borderRadius, borders),
1513
clip: getBorderClip(element, borderPoints, borders, borderRadius, bounds),
1517
for (borderSide = 0; borderSide < 4; borderSide++) {
1519
if (borders[borderSide].width > 0) {
1523
bh = height - (borders[2].width);
1525
switch(borderSide) {
1528
bh = borders[0].width;
1530
borderArgs = drawSide({
1533
c3: [bx + bw - borders[1].width, by + bh],
1534
c4: [bx + borders[3].width, by + bh]
1535
}, borderRadius[0], borderRadius[1],
1536
borderPoints.topLeftOuter, borderPoints.topLeftInner, borderPoints.topRightOuter, borderPoints.topRightInner);
1540
bx = x + width - (borders[1].width);
1541
bw = borders[1].width;
1543
borderArgs = drawSide({
1545
c2: [bx + bw, by + bh + borders[2].width],
1547
c4: [bx, by + borders[0].width]
1548
}, borderRadius[1], borderRadius[2],
1549
borderPoints.topRightOuter, borderPoints.topRightInner, borderPoints.bottomRightOuter, borderPoints.bottomRightInner);
1553
by = (by + height) - (borders[2].width);
1554
bh = borders[2].width;
1556
borderArgs = drawSide({
1557
c1: [bx + bw, by + bh],
1559
c3: [bx + borders[3].width, by],
1560
c4: [bx + bw - borders[2].width, by]
1561
}, borderRadius[2], borderRadius[3],
1562
borderPoints.bottomRightOuter, borderPoints.bottomRightInner, borderPoints.bottomLeftOuter, borderPoints.bottomLeftInner);
1566
bw = borders[3].width;
1568
borderArgs = drawSide({
1569
c1: [bx, by + bh + borders[2].width],
1571
c3: [bx + bw, by + borders[0].width],
1572
c4: [bx + bw, by + bh]
1573
}, borderRadius[3], borderRadius[0],
1574
borderPoints.bottomLeftOuter, borderPoints.bottomLeftInner, borderPoints.topLeftOuter, borderPoints.topLeftInner);
1578
borderData.borders.push({
1580
color: borders[borderSide].color
1589
function createShape(ctx, args) {
1590
var shape = ctx.drawShape();
1591
args.forEach(function(border, index) {
1592
shape[(index === 0) ? "moveTo" : border[0] + "To" ].apply(null, border.slice(1));
1597
function renderBorders(ctx, borderArgs, color) {
1598
if (color !== "transparent") {
1599
ctx.setVariable( "fillStyle", color);
1600
createShape(ctx, borderArgs);
1606
function renderFormValue (el, bounds, stack){
1608
var valueWrap = doc.createElement('valuewrap'),
1609
cssPropertyArray = ['lineHeight','textAlign','fontFamily','color','fontSize','paddingLeft','paddingTop','width','height','border','borderLeftWidth','borderTopWidth'],
1613
cssPropertyArray.forEach(function(property) {
1615
valueWrap.style[property] = getCSS(el, property);
1617
// Older IE has issues with "border"
1618
h2clog("html2canvas: Parse: Exception caught in renderFormValue: " + e.message);
1622
valueWrap.style.borderColor = "black";
1623
valueWrap.style.borderStyle = "solid";
1624
valueWrap.style.display = "block";
1625
valueWrap.style.position = "absolute";
1627
if (/^(submit|reset|button|text|password)$/.test(el.type) || el.nodeName === "SELECT"){
1628
valueWrap.style.lineHeight = getCSS(el, "height");
1631
valueWrap.style.top = bounds.top + "px";
1632
valueWrap.style.left = bounds.left + "px";
1634
textValue = (el.nodeName === "SELECT") ? (el.options[el.selectedIndex] || 0).text : el.value;
1636
textValue = el.placeholder;
1639
textNode = doc.createTextNode(textValue);
1641
valueWrap.appendChild(textNode);
1642
body.appendChild(valueWrap);
1644
renderText(el, textNode, stack);
1645
body.removeChild(valueWrap);
1648
function drawImage (ctx) {
1649
ctx.drawImage.apply(ctx, Array.prototype.slice.call(arguments, 1));
1653
function getPseudoElement(el, which) {
1654
var elStyle = window.getComputedStyle(el, which);
1655
if(!elStyle || !elStyle.content || elStyle.content === "none" || elStyle.content === "-moz-alt-content") {
1658
var content = elStyle.content + '',
1659
first = content.substr( 0, 1 );
1661
if(first === content.substr( content.length - 1 ) && first.match(/'|"/)) {
1662
content = content.substr( 1, content.length - 2 );
1665
var isImage = content.substr( 0, 3 ) === 'url',
1666
elps = document.createElement( isImage ? 'img' : 'span' );
1668
elps.className = pseudoHide + "-before " + pseudoHide + "-after";
1670
Object.keys(elStyle).filter(indexedProperty).forEach(function(prop) {
1671
elps.style[prop] = elStyle[prop];
1675
elps.src = _html2canvas.Util.parseBackgroundImage(content)[0].args[0];
1677
elps.innerHTML = content;
1682
function indexedProperty(property) {
1683
return (isNaN(window.parseInt(property, 10)));
1686
function injectPseudoElements(el, stack) {
1687
var before = getPseudoElement(el, ':before'),
1688
after = getPseudoElement(el, ':after');
1689
if(!before && !after) {
1694
el.className += " " + pseudoHide + "-before";
1695
el.parentNode.insertBefore(before, el);
1696
parseElement(before, stack, true);
1697
el.parentNode.removeChild(before);
1698
el.className = el.className.replace(pseudoHide + "-before", "").trim();
1702
el.className += " " + pseudoHide + "-after";
1703
el.appendChild(after);
1704
parseElement(after, stack, true);
1705
el.removeChild(after);
1706
el.className = el.className.replace(pseudoHide + "-after", "").trim();
1711
function renderBackgroundRepeat(ctx, image, backgroundPosition, bounds) {
1712
var offsetX = Math.round(bounds.left + backgroundPosition.left),
1713
offsetY = Math.round(bounds.top + backgroundPosition.top);
1715
ctx.createPattern(image);
1716
ctx.translate(offsetX, offsetY);
1718
ctx.translate(-offsetX, -offsetY);
1721
function backgroundRepeatShape(ctx, image, backgroundPosition, bounds, left, top, width, height) {
1723
args.push(["line", Math.round(left), Math.round(top)]);
1724
args.push(["line", Math.round(left + width), Math.round(top)]);
1725
args.push(["line", Math.round(left + width), Math.round(height + top)]);
1726
args.push(["line", Math.round(left), Math.round(height + top)]);
1727
createShape(ctx, args);
1730
renderBackgroundRepeat(ctx, image, backgroundPosition, bounds);
1734
function renderBackgroundColor(ctx, backgroundBounds, bgcolor) {
1737
backgroundBounds.left,
1738
backgroundBounds.top,
1739
backgroundBounds.width,
1740
backgroundBounds.height,
1745
function renderBackgroundRepeating(el, bounds, ctx, image, imageIndex) {
1746
var backgroundSize = _html2canvas.Util.BackgroundSize(el, bounds, image, imageIndex),
1747
backgroundPosition = _html2canvas.Util.BackgroundPosition(el, bounds, image, imageIndex, backgroundSize),
1748
backgroundRepeat = getCSS(el, "backgroundRepeat").split(",").map(function(value) {
1749
return value.trim();
1752
image = resizeImage(image, backgroundSize);
1754
backgroundRepeat = backgroundRepeat[imageIndex] || backgroundRepeat[0];
1756
switch (backgroundRepeat) {
1758
backgroundRepeatShape(ctx, image, backgroundPosition, bounds,
1759
bounds.left, bounds.top + backgroundPosition.top, 99999, image.height);
1763
backgroundRepeatShape(ctx, image, backgroundPosition, bounds,
1764
bounds.left + backgroundPosition.left, bounds.top, image.width, 99999);
1768
backgroundRepeatShape(ctx, image, backgroundPosition, bounds,
1769
bounds.left + backgroundPosition.left, bounds.top + backgroundPosition.top, image.width, image.height);
1773
renderBackgroundRepeat(ctx, image, backgroundPosition, {
1777
height: image.height
1783
function renderBackgroundImage(element, bounds, ctx) {
1784
var backgroundImage = getCSS(element, "backgroundImage"),
1785
backgroundImages = _html2canvas.Util.parseBackgroundImage(backgroundImage),
1787
imageIndex = backgroundImages.length;
1789
while(imageIndex--) {
1790
backgroundImage = backgroundImages[imageIndex];
1792
if (!backgroundImage.args || backgroundImage.args.length === 0) {
1796
var key = backgroundImage.method === 'url' ?
1797
backgroundImage.args[0] :
1798
backgroundImage.value;
1800
image = loadImage(key);
1802
// TODO add support for background-origin
1804
renderBackgroundRepeating(element, bounds, ctx, image, imageIndex);
1806
h2clog("html2canvas: Error loading background:", backgroundImage);
1811
function resizeImage(image, bounds) {
1812
if(image.width === bounds.width && image.height === bounds.height) {
1816
var ctx, canvas = doc.createElement('canvas');
1817
canvas.width = bounds.width;
1818
canvas.height = bounds.height;
1819
ctx = canvas.getContext("2d");
1820
drawImage(ctx, image, 0, 0, image.width, image.height, 0, 0, bounds.width, bounds.height );
1824
function setOpacity(ctx, element, parentStack) {
1825
var opacity = getCSS(element, "opacity") * ((parentStack) ? parentStack.opacity : 1);
1826
ctx.setVariable("globalAlpha", opacity);
1830
function createStack(element, parentStack, bounds) {
1832
var ctx = h2cRenderContext((!parentStack) ? documentWidth() : bounds.width , (!parentStack) ? documentHeight() : bounds.height),
1835
zIndex: setZ(getCSS(element, "zIndex"), (parentStack) ? parentStack.zIndex : null),
1836
opacity: setOpacity(ctx, element, parentStack),
1837
cssPosition: getCSS(element, "position"),
1838
borders: getBorderData(element),
1839
clip: (parentStack && parentStack.clip) ? _html2canvas.Util.Extend( {}, parentStack.clip ) : null
1842
// TODO correct overflow for absolute content residing under a static position
1843
if (options.useOverflow === true && /(hidden|scroll|auto)/.test(getCSS(element, "overflow")) === true && /(BODY)/i.test(element.nodeName) === false){
1844
stack.clip = (stack.clip) ? clipBounds(stack.clip, bounds) : bounds;
1847
stack.zIndex.children.push(stack);
1852
function getBackgroundBounds(borders, bounds, clip) {
1853
var backgroundBounds = {
1854
left: bounds.left + borders[3].width,
1855
top: bounds.top + borders[0].width,
1856
width: bounds.width - (borders[1].width + borders[3].width),
1857
height: bounds.height - (borders[0].width + borders[2].width)
1861
backgroundBounds = clipBounds(backgroundBounds, clip);
1864
return backgroundBounds;
1867
function renderElement(element, parentStack, pseudoElement){
1868
var bounds = _html2canvas.Util.Bounds(element),
1870
bgcolor = (ignoreElementsRegExp.test(element.nodeName)) ? "#efefef" : getCSS(element, "backgroundColor"),
1871
stack = createStack(element, parentStack, bounds),
1872
borders = stack.borders,
1874
backgroundBounds = getBackgroundBounds(borders, bounds, stack.clip),
1875
borderData = parseBorders(element, bounds, borders);
1877
createShape(ctx, borderData.clip);
1882
if (backgroundBounds.height > 0 && backgroundBounds.width > 0){
1883
renderBackgroundColor(ctx, bounds, bgcolor);
1884
renderBackgroundImage(element, backgroundBounds, ctx);
1889
borderData.borders.forEach(function(border) {
1890
renderBorders(ctx, border.args, border.color);
1893
if (!pseudoElement) {
1894
injectPseudoElements(element, stack);
1897
switch(element.nodeName){
1899
if ((image = loadImage(element.getAttribute('src')))) {
1900
renderImage(ctx, element, image, bounds, borders);
1902
h2clog("html2canvas: Error loading <img>:" + element.getAttribute('src'));
1906
// TODO add all relevant type's, i.e. HTML5 new stuff
1907
// todo add support for placeholder attribute for browsers which support it
1908
if (/^(text|url|email|submit|button|reset)$/.test(element.type) && (element.value || element.placeholder).length > 0){
1909
renderFormValue(element, bounds, stack);
1913
if ((element.value || element.placeholder || "").length > 0){
1914
renderFormValue(element, bounds, stack);
1918
if ((element.options||element.placeholder || "").length > 0){
1919
renderFormValue(element, bounds, stack);
1923
renderListItem(element, stack, backgroundBounds);
1926
renderImage(ctx, element, element, bounds, borders);
1933
function isElementVisible(element) {
1934
return (getCSS(element, 'display') !== "none" && getCSS(element, 'visibility') !== "hidden" && !element.hasAttribute("data-html2canvas-ignore"));
1937
function parseElement (el, stack, pseudoElement) {
1939
if (isElementVisible(el)) {
1940
stack = renderElement(el, stack, pseudoElement) || stack;
1941
if (!ignoreElementsRegExp.test(el.nodeName)) {
1942
_html2canvas.Util.Children(el).forEach(function(node) {
1943
if (node.nodeType === 1) {
1944
parseElement(node, stack, pseudoElement);
1945
} else if (node.nodeType === 3) {
1946
renderText(el, node, stack);
1953
function svgDOMRender(body, stack) {
1954
var img = new Image(),
1955
docWidth = documentWidth(),
1956
docHeight = documentHeight(),
1959
function parseDOM(el) {
1960
var children = _html2canvas.Util.Children( el ),
1961
len = children.length,
1967
for ( i = 0; i < len; i+=1 ) {
1968
elm = children[ i ];
1969
if ( elm.nodeType === 3 ) {
1971
html += elm.nodeValue.replace(/</g,"<").replace(/>/g,">");
1972
} else if ( elm.nodeType === 1 ) {
1974
if ( !/^(script|meta|title)$/.test(elm.nodeName.toLowerCase()) ) {
1976
html += "<" + elm.nodeName.toLowerCase();
1979
if ( elm.hasAttributes() ) {
1980
attr = elm.attributes;
1982
for ( a = 0; a < alen; a+=1 ) {
1983
html += " " + attr[ a ].name + '="' + attr[ a ].value + '"';
1993
html += "</" + elm.nodeName.toLowerCase() + ">";
2003
"data:image/svg+xml,",
2004
"<svg xmlns='http://www.w3.org/2000/svg' version='1.1' width='" + docWidth + "' height='" + docHeight + "'>",
2005
"<foreignObject width='" + docWidth + "' height='" + docHeight + "'>",
2006
"<html xmlns='http://www.w3.org/1999/xhtml' style='margin:0;'>",
2007
html.replace(/\#/g,"%23"),
2013
img.onload = function() {
2014
stack.svgRender = img;
2020
var stack = renderElement(element, null);
2022
if (support.svgRendering) {
2023
svgDOMRender(document.documentElement, stack);
2026
Array.prototype.slice.call(element.children, 0).forEach(function(childElement) {
2027
parseElement(childElement, stack);
2030
stack.backgroundColor = getCSS(document.documentElement, "backgroundColor");
2031
body.removeChild(hidePseudoElements);
2038
function h2czContext(zindex) {
2044
_html2canvas.Preload = function( options ) {
2047
numLoaded: 0, // also failed are counted here
2056
element = options.elements[0] || document.body,
2057
doc = element.ownerDocument,
2058
domImages = doc.images, // TODO probably should limit it to images present in the element only
2059
imgLen = domImages.length,
2060
link = doc.createElement("a"),
2061
supportCORS = (function( img ){
2062
return (img.crossOrigin !== undefined);
2066
link.href = window.location.href;
2067
pageOrigin = link.protocol + link.host;
2069
function isSameOrigin(url){
2071
link.href = link.href; // YES, BELIEVE IT OR NOT, that is required for IE9 - http://jsfiddle.net/niklasvh/2e48b/
2072
var origin = link.protocol + link.host;
2073
return (origin === pageOrigin);
2077
h2clog("html2canvas: start: images: " + images.numLoaded + " / " + images.numTotal + " (failed: " + images.numFailed + ")");
2078
if (!images.firstRun && images.numLoaded >= images.numTotal){
2079
h2clog("Finished loading images: # " + images.numTotal + " (failed: " + images.numFailed + ")");
2081
if (typeof options.complete === "function"){
2082
options.complete(images);
2088
// TODO modify proxy to serve images with CORS enabled, where available
2089
function proxyGetImage(url, img, imageObj){
2091
scriptUrl = options.proxy,
2095
url = link.href; // work around for pages with base href="" set - WARNING: this may change the url
2097
callback_name = 'html2canvas_' + (count++);
2098
imageObj.callbackname = callback_name;
2100
if (scriptUrl.indexOf("?") > -1) {
2105
scriptUrl += 'url=' + encodeURIComponent(url) + '&callback=' + callback_name;
2106
script = doc.createElement("script");
2108
window[callback_name] = function(a){
2109
if (a.substring(0,6) === "error:"){
2110
imageObj.succeeded = false;
2115
setImageLoadHandlers(img, imageObj);
2118
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)
2120
delete window[callback_name]; // for all browser that support this
2122
script.parentNode.removeChild(script);
2124
delete imageObj.script;
2125
delete imageObj.callbackname;
2128
script.setAttribute("type", "text/javascript");
2129
script.setAttribute("src", scriptUrl);
2130
imageObj.script = script;
2131
window.document.body.appendChild(script);
2135
function loadPseudoElement(element, type) {
2136
var style = window.getComputedStyle(element, type),
2137
content = style.content;
2138
if (content.substr(0, 3) === 'url') {
2139
methods.loadImage(_html2canvas.Util.parseBackgroundImage(content)[0].args[0]);
2141
loadBackgroundImages(style.backgroundImage, element);
2144
function loadPseudoElementImages(element) {
2145
loadPseudoElement(element, ":before");
2146
loadPseudoElement(element, ":after");
2149
function loadGradientImage(backgroundImage, bounds) {
2150
var img = _html2canvas.Generate.Gradient(backgroundImage, bounds);
2152
if (img !== undefined){
2153
images[backgroundImage] = {
2163
function invalidBackgrounds(background_image) {
2164
return (background_image && background_image.method && background_image.args && background_image.args.length > 0 );
2167
function loadBackgroundImages(background_image, el) {
2170
_html2canvas.Util.parseBackgroundImage(background_image).filter(invalidBackgrounds).forEach(function(background_image) {
2171
if (background_image.method === 'url') {
2172
methods.loadImage(background_image.args[0]);
2173
} else if(background_image.method.match(/\-?gradient$/)) {
2174
if(bounds === undefined) {
2175
bounds = _html2canvas.Util.Bounds(el);
2177
loadGradientImage(background_image.value, bounds);
2182
function getImages (el) {
2183
var elNodeType = false;
2185
// Firefox fails with permission denied on pages with iframes
2187
_html2canvas.Util.Children(el).forEach(function(img) {
2194
elNodeType = el.nodeType;
2197
h2clog("html2canvas: failed to access some element's nodeType - Exception: " + ex.message);
2200
if (elNodeType === 1 || elNodeType === undefined) {
2201
loadPseudoElementImages(el);
2203
loadBackgroundImages(_html2canvas.Util.getCSS(el, 'backgroundImage'), el);
2205
h2clog("html2canvas: failed to get background-image - Exception: " + e.message);
2207
loadBackgroundImages(el);
2211
function setImageLoadHandlers(img, imageObj) {
2212
img.onload = function() {
2213
if ( imageObj.timer !== undefined ) {
2215
window.clearTimeout( imageObj.timer );
2219
imageObj.succeeded = true;
2220
img.onerror = img.onload = null;
2223
img.onerror = function() {
2224
if (img.crossOrigin === "anonymous") {
2226
window.clearTimeout( imageObj.timer );
2228
// let's try with proxy instead
2229
if ( options.proxy ) {
2235
proxyGetImage( img.src, img, imageObj );
2242
imageObj.succeeded = false;
2243
img.onerror = img.onload = null;
2249
loadImage: function( src ) {
2251
if ( src && images[src] === undefined ) {
2253
if ( src.match(/data:image\/.*;base64,/i) ) {
2254
img.src = src.replace(/url\(['"]{0,}|['"]{0,}\)$/ig, '');
2255
imageObj = images[src] = {
2259
setImageLoadHandlers(img, imageObj);
2260
} else if ( isSameOrigin( src ) || options.allowTaint === true ) {
2261
imageObj = images[src] = {
2265
setImageLoadHandlers(img, imageObj);
2267
} else if ( supportCORS && !options.allowTaint && options.useCORS ) {
2268
// attempt to load with CORS
2270
img.crossOrigin = "anonymous";
2271
imageObj = images[src] = {
2275
setImageLoadHandlers(img, imageObj);
2278
// work around for https://bugs.webkit.org/show_bug.cgi?id=80028
2279
img.customComplete = function () {
2280
if (!this.img.complete) {
2281
this.timer = window.setTimeout(this.img.customComplete, 100);
2286
img.customComplete();
2288
} else if ( options.proxy ) {
2289
imageObj = images[src] = {
2293
proxyGetImage( src, img, imageObj );
2298
cleanupDOM: function(cause) {
2300
if (!images.cleanupDone) {
2301
if (cause && typeof cause === "string") {
2302
h2clog("html2canvas: Cleanup because: " + cause);
2304
h2clog("html2canvas: Cleanup after timeout: " + options.timeout + " ms.");
2307
for (src in images) {
2308
if (images.hasOwnProperty(src)) {
2310
if (typeof img === "object" && img.callbackname && img.succeeded === undefined) {
2311
// cancel proxy image request
2312
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)
2314
delete window[img.callbackname]; // for all browser that support this
2316
if (img.script && img.script.parentNode) {
2317
img.script.setAttribute("src", "about:blank"); // try to cancel running request
2318
img.script.parentNode.removeChild(img.script);
2322
h2clog("html2canvas: Cleaned up failed img: '" + src + "' Steps: " + images.numLoaded + " / " + images.numTotal);
2327
// cancel any pending requests
2328
if(window.stop !== undefined) {
2330
} else if(document.execCommand !== undefined) {
2331
document.execCommand("Stop", false);
2333
if (document.close !== undefined) {
2336
images.cleanupDone = true;
2337
if (!(cause && typeof cause === "string")) {
2343
renderingDone: function() {
2345
window.clearTimeout(timeoutTimer);
2350
if (options.timeout > 0) {
2351
timeoutTimer = window.setTimeout(methods.cleanupDOM, options.timeout);
2354
h2clog('html2canvas: Preload starts: finding background-images');
2355
images.firstRun = true;
2359
h2clog('html2canvas: Preload: Finding images');
2360
// load <img> images
2361
for (i = 0; i < imgLen; i+=1){
2362
methods.loadImage( domImages[i].getAttribute( "src" ) );
2365
images.firstRun = false;
2366
h2clog('html2canvas: Preload: Done.');
2367
if ( images.numTotal === images.numLoaded ) {
2374
function h2cRenderContext(width, height) {
2384
'arguments': arguments
2387
translate: function() {
2391
'arguments': arguments
2398
'arguments': arguments
2405
'arguments': arguments
2408
restore: function() {
2412
'arguments': arguments
2415
fillRect: function () {
2419
'arguments': arguments
2422
createPattern: function() {
2425
name: "createPattern",
2426
'arguments': arguments
2429
drawShape: function() {
2440
moveTo: function() {
2443
'arguments': arguments
2446
lineTo: function() {
2449
'arguments': arguments
2455
'arguments': arguments
2458
bezierCurveTo: function() {
2460
name: "bezierCurveTo",
2461
'arguments': arguments
2464
quadraticCurveTo: function() {
2466
name: "quadraticCurveTo",
2467
'arguments': arguments
2473
drawImage: function () {
2477
'arguments': arguments
2480
fillText: function () {
2484
'arguments': arguments
2487
setVariable: function (variable, value) {
2496
_html2canvas.Renderer = function(parseQueue, options){
2498
function createRenderQueue(parseQueue) {
2501
var sortZ = function(zStack){
2505
zStack.children.forEach(function(stackChild) {
2506
if (stackChild.children && stackChild.children.length > 0){
2507
subStacks.push(stackChild);
2508
stackValues.push(stackChild.zindex);
2510
queue.push(stackChild);
2514
stackValues.sort(function(a, b) {
2518
stackValues.forEach(function(zValue) {
2521
subStacks.some(function(stack, i){
2523
return (stack.zindex === zValue);
2525
sortZ(subStacks.splice(index, 1)[0]);
2530
sortZ(parseQueue.zIndex);
2535
function getRenderer(rendererName) {
2538
if (typeof options.renderer === "string" && _html2canvas.Renderer[rendererName] !== undefined) {
2539
renderer = _html2canvas.Renderer[rendererName](options);
2540
} else if (typeof rendererName === "function") {
2541
renderer = rendererName(options);
2543
throw new Error("Unknown renderer");
2546
if ( typeof renderer !== "function" ) {
2547
throw new Error("Invalid renderer defined");
2552
return getRenderer(options.renderer)(parseQueue, options, document, createRenderQueue(parseQueue), _html2canvas);
2555
_html2canvas.Util.Support = function (options, doc) {
2557
function supportSVGRendering() {
2558
var img = new Image(),
2559
canvas = doc.createElement("canvas"),
2560
ctx = (canvas.getContext === undefined) ? false : canvas.getContext("2d");
2561
if (ctx === false) {
2564
canvas.width = canvas.height = 10;
2566
"data:image/svg+xml,",
2567
"<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'>",
2568
"<foreignObject width='10' height='10'>",
2569
"<div xmlns='http://www.w3.org/1999/xhtml' style='width:10;height:10;'>",
2576
ctx.drawImage(img, 0, 0);
2581
h2clog('html2canvas: Parse: SVG powered rendering available');
2585
// Test whether we can use ranges to measure bounding boxes
2586
// Opera doesn't provide valid bounds.height/bottom even though it supports the method.
2588
function supportRangeBounds() {
2589
var r, testElement, rangeBounds, rangeHeight, support = false;
2591
if (doc.createRange) {
2592
r = doc.createRange();
2593
if (r.getBoundingClientRect) {
2594
testElement = doc.createElement('boundtest');
2595
testElement.style.height = "123px";
2596
testElement.style.display = "block";
2597
doc.body.appendChild(testElement);
2599
r.selectNode(testElement);
2600
rangeBounds = r.getBoundingClientRect();
2601
rangeHeight = rangeBounds.height;
2603
if (rangeHeight === 123) {
2606
doc.body.removeChild(testElement);
2614
rangeBounds: supportRangeBounds(),
2615
svgRendering: options.svgRendering && supportSVGRendering()
2618
window.html2canvas = function(elements, opts) {
2619
elements = (elements.length) ? elements : [elements];
2630
timeout: 0, // no timeout
2631
useCORS: false, // try to load images as CORS (where available), before falling back to proxy
2632
allowTaint: false, // whether to allow images to taint the canvas, won't need proxy if set to true
2635
svgRendering: false, // use svg powered rendering where available (FF11+)
2636
ignoreElements: "IFRAME|OBJECT|PARAM",
2638
letterRendering: false,
2645
taintTest: true, // do a taint test with all images before applying to canvas
2649
options = _html2canvas.Util.Extend(opts, options);
2651
_html2canvas.logging = options.logging;
2652
options.complete = function( images ) {
2654
if (typeof options.onpreloaded === "function") {
2655
if ( options.onpreloaded( images ) === false ) {
2659
queue = _html2canvas.Parse( images, options );
2661
if (typeof options.onparsed === "function") {
2662
if ( options.onparsed( queue ) === false ) {
2667
canvas = _html2canvas.Renderer( queue, options );
2669
if (typeof options.onrendered === "function") {
2670
options.onrendered( canvas );
2676
// for pages without images, we still want this to be async, i.e. return methods before executing
2677
window.setTimeout( function(){
2678
_html2canvas.Preload( options );
2682
render: function( queue, opts ) {
2683
return _html2canvas.Renderer( queue, _html2canvas.Util.Extend(opts, options) );
2685
parse: function( images, opts ) {
2686
return _html2canvas.Parse( images, _html2canvas.Util.Extend(opts, options) );
2688
preload: function( opts ) {
2689
return _html2canvas.Preload( _html2canvas.Util.Extend(opts, options) );
2695
window.html2canvas.log = h2clog; // for renderers
2696
window.html2canvas.Renderer = {
2697
Canvas: undefined // We are assuming this will be used
2699
_html2canvas.Renderer.Canvas = function(options) {
2701
options = options || {};
2705
testCanvas = document.createElement("canvas"),
2706
testctx = testCanvas.getContext("2d"),
2707
canvas = options.canvas || doc.createElement('canvas');
2710
function createShape(ctx, args) {
2712
args.forEach(function(arg) {
2713
ctx[arg.name].apply(ctx, arg['arguments']);
2718
function safeImage(item) {
2719
if (safeImages.indexOf(item['arguments'][0].src ) === -1) {
2720
testctx.drawImage(item['arguments'][0], 0, 0);
2722
testctx.getImageData(0, 0, 1, 1);
2724
testCanvas = doc.createElement("canvas");
2725
testctx = testCanvas.getContext("2d");
2728
safeImages.push(item['arguments'][0].src);
2733
function isTransparent(backgroundColor) {
2734
return (backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)");
2737
function renderItem(ctx, item) {
2740
ctx[item.name] = item['arguments'];
2743
if (item.name === "createPattern") {
2744
if (item['arguments'][0].width > 0 && item['arguments'][0].height > 0) {
2746
ctx.fillStyle = ctx.createPattern(item['arguments'][0], "repeat");
2749
h2clog("html2canvas: Renderer: Error creating pattern", e.message);
2752
} else if (item.name === "drawShape") {
2753
createShape(ctx, item['arguments']);
2754
} else if (item.name === "drawImage") {
2755
if (item['arguments'][8] > 0 && item['arguments'][7] > 0) {
2756
if (!options.taintTest || (options.taintTest && safeImage(item))) {
2757
ctx.drawImage.apply( ctx, item['arguments'] );
2761
ctx[item.name].apply(ctx, item['arguments']);
2767
return function(zStack, options, doc, queue, _html2canvas) {
2769
var ctx = canvas.getContext("2d"),
2777
canvas.width = canvas.style.width = options.width || zStack.ctx.width;
2778
canvas.height = canvas.style.height = options.height || zStack.ctx.height;
2780
fstyle = ctx.fillStyle;
2781
ctx.fillStyle = (isTransparent(zStack.backgroundColor) && options.background !== undefined) ? options.background : zStack.backgroundColor;
2782
ctx.fillRect(0, 0, canvas.width, canvas.height);
2783
ctx.fillStyle = fstyle;
2786
if ( options.svgRendering && zStack.svgRender !== undefined ) {
2787
// TODO: enable async rendering to support this
2788
ctx.drawImage( zStack.svgRender, 0, 0 );
2790
for ( i = 0, queueLen = queue.length; i < queueLen; i+=1 ) {
2791
storageContext = queue.splice(0, 1)[0];
2792
storageContext.canvasPosition = storageContext.canvasPosition || {};
2794
// set common settings for canvas
2795
ctx.textBaseline = "bottom";
2797
if (storageContext.clip){
2800
// console.log(storageContext);
2801
ctx.rect(storageContext.clip.left, storageContext.clip.top, storageContext.clip.width, storageContext.clip.height);
2805
if (storageContext.ctx.storage) {
2806
storageContext.ctx.storage.forEach(renderItem.bind(null, ctx));
2809
if (storageContext.clip){
2815
h2clog("html2canvas: Renderer: Canvas renderer done - returning canvas obj");
2817
queueLen = options.elements.length;
2819
if (queueLen === 1) {
2820
if (typeof options.elements[0] === "object" && options.elements[0].nodeName !== "BODY") {
2821
// crop image to the bounds of selected (single) element
2822
bounds = _html2canvas.Util.Bounds(options.elements[0]);
2823
newCanvas = doc.createElement('canvas');
2824
newCanvas.width = bounds.width;
2825
newCanvas.height = bounds.height;
2826
ctx = newCanvas.getContext("2d");
2828
ctx.drawImage(canvas, bounds.left, bounds.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
2837
})(window,document);