three.html.js 5.4 KB

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