html2canvas.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. function html2canvas( element ) {
  5. var range = document.createRange();
  6. function Clipper( context ) {
  7. var clips = [];
  8. var isClipping = false;
  9. function doClip() {
  10. if ( isClipping ) {
  11. isClipping = false;
  12. context.restore();
  13. }
  14. if ( clips.length === 0 ) return;
  15. var minX = - Infinity, minY = - Infinity;
  16. var maxX = Infinity, maxY = Infinity;
  17. for ( var i = 0; i < clips.length; i ++ ) {
  18. var clip = clips[ i ];
  19. minX = Math.max( minX, clip.x );
  20. minY = Math.max( minY, clip.y );
  21. maxX = Math.min( maxX, clip.x + clip.width );
  22. maxY = Math.min( maxY, clip.y + clip.height );
  23. }
  24. context.save();
  25. context.beginPath();
  26. context.rect( minX, minY, maxX - minX, maxY - minY );
  27. context.clip();
  28. isClipping = true;
  29. }
  30. return {
  31. add: function ( clip ) {
  32. clips.push( clip );
  33. doClip();
  34. },
  35. remove: function () {
  36. clips.pop();
  37. doClip();
  38. }
  39. };
  40. }
  41. function drawText( style, x, y, string ) {
  42. if ( string !== '' ) {
  43. context.font = style.fontSize + ' ' + style.fontFamily;
  44. context.textBaseline = 'top';
  45. context.fillStyle = style.color;
  46. context.fillText( string, x, y );
  47. }
  48. }
  49. function drawBorder( style, which, x, y, width, height ) {
  50. var borderWidth = style[ which + 'Width' ];
  51. var borderStyle = style[ which + 'Style' ];
  52. var borderColor = style[ which + 'Color' ];
  53. if ( borderWidth !== '0px' && borderStyle !== 'none' && borderColor !== 'transparent' && borderColor !== 'rgba(0, 0, 0, 0)' ) {
  54. context.strokeStyle = borderColor;
  55. context.beginPath();
  56. context.moveTo( x, y );
  57. context.lineTo( x + width, y + height );
  58. context.stroke();
  59. }
  60. }
  61. function drawElement( element, style ) {
  62. var x = 0, y = 0, width = 0, height = 0;
  63. if ( element.nodeType === 3 ) {
  64. // text
  65. range.selectNode( element );
  66. var rect = range.getBoundingClientRect();
  67. x = rect.left - offset.left - 0.5;
  68. y = rect.top - offset.top - 0.5;
  69. width = rect.width;
  70. height = rect.height;
  71. drawText( style, x, y, element.nodeValue.trim() );
  72. } else {
  73. if ( element.style.display === 'none' ) return;
  74. var rect = element.getBoundingClientRect();
  75. x = rect.left - offset.left - 0.5;
  76. y = rect.top - offset.top - 0.5;
  77. width = rect.width;
  78. height = rect.height;
  79. style = window.getComputedStyle( element );
  80. var backgroundColor = style.backgroundColor;
  81. if ( backgroundColor !== 'transparent' && backgroundColor !== 'rgba(0, 0, 0, 0)' ) {
  82. context.fillStyle = backgroundColor;
  83. context.fillRect( x, y, width, height );
  84. }
  85. drawBorder( style, 'borderTop', x, y, width, 0 );
  86. drawBorder( style, 'borderLeft', x, y, 0, height );
  87. drawBorder( style, 'borderBottom', x, y + height, width, 0 );
  88. drawBorder( style, 'borderRight', x + width, y, 0, height );
  89. if ( element.type === 'color' || element.type === 'text' ) {
  90. clipper.add( { x: x, y: y, width: width, height: height } );
  91. drawText( style, x + parseInt( style.paddingLeft ), y + parseInt( style.paddingTop ), element.value );
  92. clipper.remove();
  93. }
  94. }
  95. /*
  96. // debug
  97. context.strokeStyle = '#' + Math.random().toString( 16 ).slice( - 3 );
  98. context.strokeRect( x - 0.5, y - 0.5, width + 1, height + 1 );
  99. */
  100. var isClipping = style.overflow === 'auto' || style.overflow === 'hidden';
  101. if ( isClipping ) clipper.add( { x: x, y: y, width: width, height: height } );
  102. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  103. drawElement( element.childNodes[ i ], style );
  104. }
  105. if ( isClipping ) clipper.remove();
  106. }
  107. var offset = element.getBoundingClientRect();
  108. var canvas = document.createElement( 'canvas' );
  109. canvas.width = offset.width;
  110. canvas.height = offset.height;
  111. var context = canvas.getContext( '2d'/*, { alpha: false }*/ );
  112. var clipper = new Clipper( context );
  113. console.time( 'drawElement' );
  114. drawElement( element );
  115. console.timeEnd( 'drawElement' );
  116. return canvas;
  117. }