HTMLMesh.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import {
  2. CanvasTexture,
  3. LinearFilter,
  4. Mesh,
  5. MeshBasicMaterial,
  6. PlaneGeometry,
  7. sRGBEncoding
  8. } from 'three';
  9. class HTMLMesh extends Mesh {
  10. constructor( dom ) {
  11. const texture = new HTMLTexture( dom );
  12. const geometry = new PlaneGeometry( texture.image.width * 0.001, texture.image.height * 0.001 );
  13. const material = new MeshBasicMaterial( { map: texture, toneMapped: false } );
  14. super( geometry, material );
  15. function onEvent( event ) {
  16. material.map.dispatchDOMEvent( event );
  17. }
  18. this.addEventListener( 'mousedown', onEvent );
  19. this.addEventListener( 'mousemove', onEvent );
  20. this.addEventListener( 'mouseup', onEvent );
  21. this.addEventListener( 'click', onEvent );
  22. this.dispose = function () {
  23. geometry.dispose();
  24. material.dispose();
  25. material.map.dispose();
  26. this.removeEventListener( 'mousedown', onEvent );
  27. this.removeEventListener( 'mousemove', onEvent );
  28. this.removeEventListener( 'mouseup', onEvent );
  29. this.removeEventListener( 'click', onEvent );
  30. };
  31. }
  32. }
  33. class HTMLTexture extends CanvasTexture {
  34. constructor( dom ) {
  35. super( html2canvas( dom ) );
  36. this.dom = dom;
  37. this.anisotropy = 16;
  38. this.encoding = sRGBEncoding;
  39. this.minFilter = LinearFilter;
  40. this.magFilter = LinearFilter;
  41. // Create an observer on the DOM, and run html2canvas update in the next loop
  42. const observer = new MutationObserver( () => {
  43. if ( ! this.scheduleUpdate ) {
  44. // ideally should use xr.requestAnimationFrame, here setTimeout to avoid passing the renderer
  45. this.scheduleUpdate = setTimeout( () => this.update(), 16 );
  46. }
  47. } );
  48. const config = { attributes: true, childList: true, subtree: true, characterData: true };
  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. //
  71. const canvases = new WeakMap();
  72. function html2canvas( element ) {
  73. const range = document.createRange();
  74. function Clipper( context ) {
  75. const clips = [];
  76. let isClipping = false;
  77. function doClip() {
  78. if ( isClipping ) {
  79. isClipping = false;
  80. context.restore();
  81. }
  82. if ( clips.length === 0 ) return;
  83. let minX = - Infinity, minY = - Infinity;
  84. let maxX = Infinity, maxY = Infinity;
  85. for ( let i = 0; i < clips.length; i ++ ) {
  86. const clip = clips[ i ];
  87. minX = Math.max( minX, clip.x );
  88. minY = Math.max( minY, clip.y );
  89. maxX = Math.min( maxX, clip.x + clip.width );
  90. maxY = Math.min( maxY, clip.y + clip.height );
  91. }
  92. context.save();
  93. context.beginPath();
  94. context.rect( minX, minY, maxX - minX, maxY - minY );
  95. context.clip();
  96. isClipping = true;
  97. }
  98. return {
  99. add: function ( clip ) {
  100. clips.push( clip );
  101. doClip();
  102. },
  103. remove: function () {
  104. clips.pop();
  105. doClip();
  106. }
  107. };
  108. }
  109. function drawText( style, x, y, string ) {
  110. if ( string !== '' ) {
  111. if ( style.textTransform === 'uppercase' ) {
  112. string = string.toUpperCase();
  113. }
  114. context.font = style.fontSize + ' ' + style.fontFamily;
  115. context.textBaseline = 'top';
  116. context.fillStyle = style.color;
  117. context.fillText( string, x, y );
  118. }
  119. }
  120. function drawBorder( style, which, x, y, width, height ) {
  121. const borderWidth = style[ which + 'Width' ];
  122. const borderStyle = style[ which + 'Style' ];
  123. const borderColor = style[ which + 'Color' ];
  124. if ( borderWidth !== '0px' && borderStyle !== 'none' && borderColor !== 'transparent' && borderColor !== 'rgba(0, 0, 0, 0)' ) {
  125. context.strokeStyle = borderColor;
  126. context.beginPath();
  127. context.moveTo( x, y );
  128. context.lineTo( x + width, y + height );
  129. context.stroke();
  130. }
  131. }
  132. function drawElement( element, style ) {
  133. let x = 0, y = 0, width = 0, height = 0;
  134. if ( element.nodeType === Node.TEXT_NODE ) {
  135. // text
  136. range.selectNode( element );
  137. const rect = range.getBoundingClientRect();
  138. x = rect.left - offset.left - 0.5;
  139. y = rect.top - offset.top - 0.5;
  140. width = rect.width;
  141. height = rect.height;
  142. drawText( style, x, y, element.nodeValue.trim() );
  143. } else if ( element.nodeType === Node.COMMENT_NODE ) {
  144. return;
  145. } else if ( element instanceof HTMLCanvasElement ) {
  146. // Canvas element
  147. if ( element.style.display === 'none' ) return;
  148. context.save();
  149. const dpr = window.devicePixelRatio;
  150. context.scale(1/dpr, 1/dpr);
  151. context.drawImage(element, 0, 0 );
  152. context.restore();
  153. } else {
  154. if ( element.style.display === 'none' ) return;
  155. const rect = element.getBoundingClientRect();
  156. x = rect.left - offset.left - 0.5;
  157. y = rect.top - offset.top - 0.5;
  158. width = rect.width;
  159. height = rect.height;
  160. style = window.getComputedStyle( element );
  161. const backgroundColor = style.backgroundColor;
  162. if ( backgroundColor !== 'transparent' && backgroundColor !== 'rgba(0, 0, 0, 0)' ) {
  163. context.fillStyle = backgroundColor;
  164. context.fillRect( x, y, width, height );
  165. }
  166. drawBorder( style, 'borderTop', x, y, width, 0 );
  167. drawBorder( style, 'borderLeft', x, y, 0, height );
  168. drawBorder( style, 'borderBottom', x, y + height, width, 0 );
  169. drawBorder( style, 'borderRight', x + width, y, 0, height );
  170. if ( element.type === 'color' || element.type === 'text' || element.type === 'number' ) {
  171. clipper.add( { x: x, y: y, width: width, height: height } );
  172. drawText( style, x + parseInt( style.paddingLeft ), y + parseInt( style.paddingTop ), element.value );
  173. clipper.remove();
  174. }
  175. }
  176. /*
  177. // debug
  178. context.strokeStyle = '#' + Math.random().toString( 16 ).slice( - 3 );
  179. context.strokeRect( x - 0.5, y - 0.5, width + 1, height + 1 );
  180. */
  181. const isClipping = style.overflow === 'auto' || style.overflow === 'hidden';
  182. if ( isClipping ) clipper.add( { x: x, y: y, width: width, height: height } );
  183. for ( let i = 0; i < element.childNodes.length; i ++ ) {
  184. drawElement( element.childNodes[ i ], style );
  185. }
  186. if ( isClipping ) clipper.remove();
  187. }
  188. const offset = element.getBoundingClientRect();
  189. let canvas;
  190. if ( canvases.has( element ) ) {
  191. canvas = canvases.get( element );
  192. } else {
  193. canvas = document.createElement( 'canvas' );
  194. canvas.width = offset.width;
  195. canvas.height = offset.height;
  196. }
  197. const context = canvas.getContext( '2d'/*, { alpha: false }*/ );
  198. const clipper = new Clipper( context );
  199. // console.time( 'drawElement' );
  200. drawElement( element );
  201. // console.timeEnd( 'drawElement' );
  202. return canvas;
  203. }
  204. function htmlevent( element, event, x, y ) {
  205. const mouseEventInit = {
  206. clientX: ( x * element.offsetWidth ) + element.offsetLeft,
  207. clientY: ( y * element.offsetHeight ) + element.offsetTop,
  208. view: element.ownerDocument.defaultView
  209. };
  210. window.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  211. const rect = element.getBoundingClientRect();
  212. x = x * rect.width + rect.left;
  213. y = y * rect.height + rect.top;
  214. function traverse( element ) {
  215. if ( element.nodeType !== Node.TEXT_NODE && element.nodeType !== Node.COMMENT_NODE ) {
  216. const rect = element.getBoundingClientRect();
  217. if ( x > rect.left && x < rect.right && y > rect.top && y < rect.bottom ) {
  218. element.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  219. }
  220. for ( let i = 0; i < element.childNodes.length; i ++ ) {
  221. traverse( element.childNodes[ i ] );
  222. }
  223. }
  224. }
  225. traverse( element );
  226. }
  227. export { HTMLMesh };