three.html.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import * as THREE from '../../../build/three.module.js';
  2. class HTMLMesh extends THREE.Mesh {
  3. constructor( dom ) {
  4. const texture = new HTMLTexture( dom );
  5. const geometry = new THREE.PlaneGeometry( texture.image.width * 0.002, texture.image.height * 0.002 );
  6. const material = new THREE.MeshBasicMaterial( { map: texture, toneMapped: false } );
  7. super( geometry, material );
  8. }
  9. }
  10. class HTMLTexture extends THREE.CanvasTexture {
  11. constructor( dom ) {
  12. super( html2canvas( dom ) );
  13. this.dom = dom;
  14. this.anisotropy = 16;
  15. this.encoding = THREE.sRGBEncoding;
  16. this.minFilter = THREE.LinearFilter;
  17. this.magFilter = THREE.LinearFilter;
  18. }
  19. click( x, y ) {
  20. htmlclick( this.dom, x, y );
  21. this.update();
  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. if ( style.textTransform === 'uppercase' ) {
  69. string = string.toUpperCase();
  70. }
  71. context.font = style.fontSize + ' ' + style.fontFamily;
  72. context.textBaseline = 'top';
  73. context.fillStyle = style.color;
  74. context.fillText( string, x, y );
  75. }
  76. }
  77. function drawBorder( style, which, x, y, width, height ) {
  78. var borderWidth = style[ which + 'Width' ];
  79. var borderStyle = style[ which + 'Style' ];
  80. var borderColor = style[ which + 'Color' ];
  81. if ( borderWidth !== '0px' && borderStyle !== 'none' && borderColor !== 'transparent' && borderColor !== 'rgba(0, 0, 0, 0)' ) {
  82. context.strokeStyle = borderColor;
  83. context.beginPath();
  84. context.moveTo( x, y );
  85. context.lineTo( x + width, y + height );
  86. context.stroke();
  87. }
  88. }
  89. function drawElement( element, style ) {
  90. var x = 0, y = 0, width = 0, height = 0;
  91. if ( element.nodeType === 3 ) {
  92. // text
  93. range.selectNode( element );
  94. var rect = range.getBoundingClientRect();
  95. x = rect.left - offset.left - 0.5;
  96. y = rect.top - offset.top - 0.5;
  97. width = rect.width;
  98. height = rect.height;
  99. drawText( style, x, y, element.nodeValue.trim() );
  100. } else {
  101. if ( element.style.display === 'none' ) return;
  102. var rect = element.getBoundingClientRect();
  103. x = rect.left - offset.left - 0.5;
  104. y = rect.top - offset.top - 0.5;
  105. width = rect.width;
  106. height = rect.height;
  107. style = window.getComputedStyle( element );
  108. var backgroundColor = style.backgroundColor;
  109. if ( backgroundColor !== 'transparent' && backgroundColor !== 'rgba(0, 0, 0, 0)' ) {
  110. context.fillStyle = backgroundColor;
  111. context.fillRect( x, y, width, height );
  112. }
  113. drawBorder( style, 'borderTop', x, y, width, 0 );
  114. drawBorder( style, 'borderLeft', x, y, 0, height );
  115. drawBorder( style, 'borderBottom', x, y + height, width, 0 );
  116. drawBorder( style, 'borderRight', x + width, y, 0, height );
  117. if ( element.type === 'color' || element.type === 'text' ) {
  118. clipper.add( { x: x, y: y, width: width, height: height } );
  119. drawText( style, x + parseInt( style.paddingLeft ), y + parseInt( style.paddingTop ), element.value );
  120. clipper.remove();
  121. }
  122. }
  123. /*
  124. // debug
  125. context.strokeStyle = '#' + Math.random().toString( 16 ).slice( - 3 );
  126. context.strokeRect( x - 0.5, y - 0.5, width + 1, height + 1 );
  127. */
  128. var isClipping = style.overflow === 'auto' || style.overflow === 'hidden';
  129. if ( isClipping ) clipper.add( { x: x, y: y, width: width, height: height } );
  130. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  131. drawElement( element.childNodes[ i ], style );
  132. }
  133. if ( isClipping ) clipper.remove();
  134. }
  135. var offset = element.getBoundingClientRect();
  136. var canvas = document.createElement( 'canvas' );
  137. canvas.width = offset.width;
  138. canvas.height = offset.height;
  139. var context = canvas.getContext( '2d'/*, { alpha: false }*/ );
  140. var clipper = new Clipper( context );
  141. console.time( 'drawElement' );
  142. drawElement( element );
  143. console.timeEnd( 'drawElement' );
  144. return canvas;
  145. }
  146. function htmlclick( element, x, y ) {
  147. /*
  148. const mouseEventInit = {
  149. clientX: ( x * element.offsetWidth ) + element.offsetLeft,
  150. clientY: ( y * element.offsetHeight ) + element.offsetTop,
  151. view: element.ownerDocument.defaultView
  152. };
  153. element.dispatchEvent( new MouseEvent( 'click', mouseEventInit ) );
  154. */
  155. const rect = element.getBoundingClientRect();
  156. x = x * rect.width + rect.left;
  157. y = y * rect.height + rect.top;
  158. function traverse( element ) {
  159. if ( element.nodeType !== 3 ) {
  160. const rect = element.getBoundingClientRect();
  161. if ( x > rect.left && x < rect.right && y > rect.top && y < rect.bottom ) {
  162. element.click();
  163. }
  164. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  165. traverse( element.childNodes[ i ] );
  166. }
  167. }
  168. }
  169. traverse( element );
  170. }
  171. export { HTMLMesh, HTMLTexture };