HTMLMesh.js 6.6 KB

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