HTMLMesh.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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; // Create an observer on the DOM, and run html2canvas update in the next loop
  37. const observer = new MutationObserver( () => {
  38. if ( ! this.scheduleUpdate ) {
  39. // ideally should use xr.requestAnimationFrame, here setTimeout to avoid passing the renderer
  40. this.scheduleUpdate = setTimeout( () => this.update(), 16 );
  41. }
  42. } );
  43. const config = {
  44. attributes: true,
  45. childList: true,
  46. subtree: true,
  47. characterData: true
  48. };
  49. observer.observe( dom, config );
  50. this.observer = observer;
  51. }
  52. dispatchDOMEvent( event ) {
  53. if ( event.data ) {
  54. htmlevent( this.dom, event.type, event.data.x, event.data.y );
  55. }
  56. }
  57. update() {
  58. this.image = html2canvas( this.dom );
  59. this.needsUpdate = true;
  60. this.scheduleUpdate = null;
  61. }
  62. dispose() {
  63. if ( this.observer ) {
  64. this.observer.disconnect();
  65. }
  66. this.scheduleUpdate = clearTimeout( this.scheduleUpdate );
  67. super.dispose();
  68. }
  69. } //
  70. const canvases = new WeakMap();
  71. function html2canvas( element ) {
  72. const range = document.createRange();
  73. function Clipper( context ) {
  74. const clips = [];
  75. let isClipping = false;
  76. function doClip() {
  77. if ( isClipping ) {
  78. isClipping = false;
  79. context.restore();
  80. }
  81. if ( clips.length === 0 ) return;
  82. let minX = - Infinity,
  83. minY = - Infinity;
  84. let maxX = Infinity,
  85. maxY = Infinity;
  86. for ( let i = 0; i < clips.length; i ++ ) {
  87. const clip = clips[ i ];
  88. minX = Math.max( minX, clip.x );
  89. minY = Math.max( minY, clip.y );
  90. maxX = Math.min( maxX, clip.x + clip.width );
  91. maxY = Math.min( maxY, clip.y + clip.height );
  92. }
  93. context.save();
  94. context.beginPath();
  95. context.rect( minX, minY, maxX - minX, maxY - minY );
  96. context.clip();
  97. isClipping = true;
  98. }
  99. return {
  100. add: function ( clip ) {
  101. clips.push( clip );
  102. doClip();
  103. },
  104. remove: function () {
  105. clips.pop();
  106. doClip();
  107. }
  108. };
  109. }
  110. function drawText( style, x, y, string ) {
  111. if ( string !== '' ) {
  112. if ( style.textTransform === 'uppercase' ) {
  113. string = string.toUpperCase();
  114. }
  115. context.font = style.fontSize + ' ' + style.fontFamily;
  116. context.textBaseline = 'top';
  117. context.fillStyle = style.color;
  118. context.fillText( string, x, y );
  119. }
  120. }
  121. function drawBorder( style, which, x, y, width, height ) {
  122. const borderWidth = style[ which + 'Width' ];
  123. const borderStyle = style[ which + 'Style' ];
  124. const borderColor = style[ which + 'Color' ];
  125. if ( borderWidth !== '0px' && borderStyle !== 'none' && borderColor !== 'transparent' && borderColor !== 'rgba(0, 0, 0, 0)' ) {
  126. context.strokeStyle = borderColor;
  127. context.beginPath();
  128. context.moveTo( x, y );
  129. context.lineTo( x + width, y + height );
  130. context.stroke();
  131. }
  132. }
  133. function drawElement( element, style ) {
  134. let x = 0,
  135. y = 0,
  136. width = 0,
  137. height = 0;
  138. if ( element.nodeType === Node.TEXT_NODE ) {
  139. // text
  140. range.selectNode( element );
  141. const rect = range.getBoundingClientRect();
  142. x = rect.left - offset.left - 0.5;
  143. y = rect.top - offset.top - 0.5;
  144. width = rect.width;
  145. height = rect.height;
  146. drawText( style, x, y, element.nodeValue.trim() );
  147. } else if ( element.nodeType === Node.COMMENT_NODE ) {
  148. return;
  149. } else if ( element instanceof HTMLCanvasElement ) {
  150. // Canvas element
  151. if ( element.style.display === 'none' ) return;
  152. context.save();
  153. const dpr = window.devicePixelRatio;
  154. context.scale( 1 / dpr, 1 / dpr );
  155. context.drawImage( element, 0, 0 );
  156. context.restore();
  157. } else {
  158. if ( element.style.display === 'none' ) return;
  159. const rect = element.getBoundingClientRect();
  160. x = rect.left - offset.left - 0.5;
  161. y = rect.top - offset.top - 0.5;
  162. width = rect.width;
  163. height = rect.height;
  164. style = window.getComputedStyle( element );
  165. const backgroundColor = style.backgroundColor;
  166. if ( backgroundColor !== 'transparent' && backgroundColor !== 'rgba(0, 0, 0, 0)' ) {
  167. context.fillStyle = backgroundColor;
  168. context.fillRect( x, y, width, height );
  169. }
  170. drawBorder( style, 'borderTop', x, y, width, 0 );
  171. drawBorder( style, 'borderLeft', x, y, 0, height );
  172. drawBorder( style, 'borderBottom', x, y + height, width, 0 );
  173. drawBorder( style, 'borderRight', x + width, y, 0, height );
  174. if ( element.type === 'color' || element.type === 'text' || element.type === 'number' ) {
  175. clipper.add( {
  176. x: x,
  177. y: y,
  178. width: width,
  179. height: height
  180. } );
  181. drawText( style, x + parseInt( style.paddingLeft ), y + parseInt( style.paddingTop ), element.value );
  182. clipper.remove();
  183. }
  184. }
  185. /*
  186. // debug
  187. context.strokeStyle = '#' + Math.random().toString( 16 ).slice( - 3 );
  188. context.strokeRect( x - 0.5, y - 0.5, width + 1, height + 1 );
  189. */
  190. const isClipping = style.overflow === 'auto' || style.overflow === 'hidden';
  191. if ( isClipping ) clipper.add( {
  192. x: x,
  193. y: y,
  194. width: width,
  195. height: height
  196. } );
  197. for ( let i = 0; i < element.childNodes.length; i ++ ) {
  198. drawElement( element.childNodes[ i ], style );
  199. }
  200. if ( isClipping ) clipper.remove();
  201. }
  202. const offset = element.getBoundingClientRect();
  203. let canvas;
  204. if ( canvases.has( element ) ) {
  205. canvas = canvases.get( element );
  206. } else {
  207. canvas = document.createElement( 'canvas' );
  208. canvas.width = offset.width;
  209. canvas.height = offset.height;
  210. }
  211. const context = canvas.getContext( '2d'
  212. /*, { alpha: false }*/
  213. );
  214. const clipper = new Clipper( context ); // console.time( 'drawElement' );
  215. drawElement( element ); // console.timeEnd( 'drawElement' );
  216. return canvas;
  217. }
  218. function htmlevent( element, event, x, y ) {
  219. const mouseEventInit = {
  220. clientX: x * element.offsetWidth + element.offsetLeft,
  221. clientY: y * element.offsetHeight + element.offsetTop,
  222. view: element.ownerDocument.defaultView
  223. };
  224. window.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  225. const rect = element.getBoundingClientRect();
  226. x = x * rect.width + rect.left;
  227. y = y * rect.height + rect.top;
  228. function traverse( element ) {
  229. if ( element.nodeType !== Node.TEXT_NODE && element.nodeType !== Node.COMMENT_NODE ) {
  230. const rect = element.getBoundingClientRect();
  231. if ( x > rect.left && x < rect.right && y > rect.top && y < rect.bottom ) {
  232. element.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  233. }
  234. for ( let i = 0; i < element.childNodes.length; i ++ ) {
  235. traverse( element.childNodes[ i ] );
  236. }
  237. }
  238. }
  239. traverse( element );
  240. }
  241. THREE.HTMLMesh = HTMLMesh;
  242. } )();