three.html.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {
  2. CanvasTexture,
  3. Mesh,
  4. MeshBasicMaterial,
  5. PlaneGeometry,
  6. sRGBEncoding
  7. } from '../../../build/three.module.js';
  8. class HTMLMesh extends Mesh {
  9. constructor( dom ) {
  10. const texture = new HTMLTexture( dom );
  11. const geometry = new PlaneGeometry( texture.image.width * 0.001, texture.image.height * 0.001 );
  12. const material = new MeshBasicMaterial( { map: texture, toneMapped: false } );
  13. super( geometry, material );
  14. }
  15. }
  16. class HTMLTexture extends CanvasTexture {
  17. constructor( dom ) {
  18. super( html2canvas( dom ) );
  19. this.dom = dom;
  20. this.encoding = sRGBEncoding;
  21. this.anisotropy = 16;
  22. }
  23. update() {
  24. this.image = html2canvas( this.dom );
  25. this.needsUpdate = true;
  26. }
  27. }
  28. //
  29. function html2canvas( element ) {
  30. var range = document.createRange();
  31. function Clipper( context ) {
  32. var clips = [];
  33. var isClipping = false;
  34. function doClip() {
  35. if ( isClipping ) {
  36. isClipping = false;
  37. context.restore();
  38. }
  39. if ( clips.length === 0 ) return;
  40. var minX = - Infinity, minY = - Infinity;
  41. var maxX = Infinity, maxY = Infinity;
  42. for ( var i = 0; i < clips.length; i ++ ) {
  43. var clip = clips[ i ];
  44. minX = Math.max( minX, clip.x );
  45. minY = Math.max( minY, clip.y );
  46. maxX = Math.min( maxX, clip.x + clip.width );
  47. maxY = Math.min( maxY, clip.y + clip.height );
  48. }
  49. context.save();
  50. context.beginPath();
  51. context.rect( minX, minY, maxX - minX, maxY - minY );
  52. context.clip();
  53. isClipping = true;
  54. }
  55. return {
  56. add: function ( clip ) {
  57. clips.push( clip );
  58. doClip();
  59. },
  60. remove: function () {
  61. clips.pop();
  62. doClip();
  63. }
  64. };
  65. }
  66. function drawText( style, x, y, string ) {
  67. if ( string !== '' ) {
  68. context.font = style.fontSize + ' ' + style.fontFamily;
  69. context.textBaseline = 'top';
  70. context.fillStyle = style.color;
  71. context.fillText( string, x, y );
  72. }
  73. }
  74. function drawBorder( style, which, x, y, width, height ) {
  75. var borderWidth = style[ which + 'Width' ];
  76. var borderStyle = style[ which + 'Style' ];
  77. var borderColor = style[ which + 'Color' ];
  78. if ( borderWidth !== '0px' && borderStyle !== 'none' && borderColor !== 'transparent' && borderColor !== 'rgba(0, 0, 0, 0)' ) {
  79. context.strokeStyle = borderColor;
  80. context.beginPath();
  81. context.moveTo( x, y );
  82. context.lineTo( x + width, y + height );
  83. context.stroke();
  84. }
  85. }
  86. function drawElement( element, style ) {
  87. var x = 0, y = 0, width = 0, height = 0;
  88. if ( element.nodeType === 3 ) {
  89. // text
  90. range.selectNode( element );
  91. var rect = range.getBoundingClientRect();
  92. x = rect.left - offset.left - 0.5;
  93. y = rect.top - offset.top - 0.5;
  94. width = rect.width;
  95. height = rect.height;
  96. drawText( style, x, y, element.nodeValue.trim() );
  97. } else {
  98. if ( element.style.display === 'none' ) return;
  99. var rect = element.getBoundingClientRect();
  100. x = rect.left - offset.left - 0.5;
  101. y = rect.top - offset.top - 0.5;
  102. width = rect.width;
  103. height = rect.height;
  104. style = window.getComputedStyle( element );
  105. var backgroundColor = style.backgroundColor;
  106. if ( backgroundColor !== 'transparent' && backgroundColor !== 'rgba(0, 0, 0, 0)' ) {
  107. context.fillStyle = backgroundColor;
  108. context.fillRect( x, y, width, height );
  109. }
  110. drawBorder( style, 'borderTop', x, y, width, 0 );
  111. drawBorder( style, 'borderLeft', x, y, 0, height );
  112. drawBorder( style, 'borderBottom', x, y + height, width, 0 );
  113. drawBorder( style, 'borderRight', x + width, y, 0, height );
  114. if ( element.type === 'color' || element.type === 'text' ) {
  115. clipper.add( { x: x, y: y, width: width, height: height } );
  116. drawText( style, x + parseInt( style.paddingLeft ), y + parseInt( style.paddingTop ), element.value );
  117. clipper.remove();
  118. }
  119. }
  120. /*
  121. // debug
  122. context.strokeStyle = '#' + Math.random().toString( 16 ).slice( - 3 );
  123. context.strokeRect( x - 0.5, y - 0.5, width + 1, height + 1 );
  124. */
  125. var isClipping = style.overflow === 'auto' || style.overflow === 'hidden';
  126. if ( isClipping ) clipper.add( { x: x, y: y, width: width, height: height } );
  127. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  128. drawElement( element.childNodes[ i ], style );
  129. }
  130. if ( isClipping ) clipper.remove();
  131. }
  132. var offset = element.getBoundingClientRect();
  133. var canvas = document.createElement( 'canvas' );
  134. canvas.width = offset.width;
  135. canvas.height = offset.height;
  136. var context = canvas.getContext( '2d'/*, { alpha: false }*/ );
  137. var clipper = new Clipper( context );
  138. console.time( 'drawElement' );
  139. drawElement( element );
  140. console.timeEnd( 'drawElement' );
  141. return canvas;
  142. }
  143. export { HTMLMesh, HTMLTexture };