three.html.js 5.7 KB

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