HTMLMesh.js 6.1 KB

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