HTMLMesh.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. }
  42. dispatchDOMEvent( event ) {
  43. htmlevent( this.dom, event.type, event.data.x, event.data.y );
  44. this.update();
  45. }
  46. update() {
  47. this.image = html2canvas( this.dom );
  48. this.needsUpdate = true;
  49. }
  50. }
  51. //
  52. const canvases = new WeakMap();
  53. function html2canvas( element ) {
  54. var range = document.createRange();
  55. function Clipper( context ) {
  56. var clips = [];
  57. var isClipping = false;
  58. function doClip() {
  59. if ( isClipping ) {
  60. isClipping = false;
  61. context.restore();
  62. }
  63. if ( clips.length === 0 ) return;
  64. var minX = - Infinity, minY = - Infinity;
  65. var maxX = Infinity, maxY = Infinity;
  66. for ( var i = 0; i < clips.length; i ++ ) {
  67. var clip = clips[ i ];
  68. minX = Math.max( minX, clip.x );
  69. minY = Math.max( minY, clip.y );
  70. maxX = Math.min( maxX, clip.x + clip.width );
  71. maxY = Math.min( maxY, clip.y + clip.height );
  72. }
  73. context.save();
  74. context.beginPath();
  75. context.rect( minX, minY, maxX - minX, maxY - minY );
  76. context.clip();
  77. isClipping = true;
  78. }
  79. return {
  80. add: function ( clip ) {
  81. clips.push( clip );
  82. doClip();
  83. },
  84. remove: function () {
  85. clips.pop();
  86. doClip();
  87. }
  88. };
  89. }
  90. function drawText( style, x, y, string ) {
  91. if ( string !== '' ) {
  92. if ( style.textTransform === 'uppercase' ) {
  93. string = string.toUpperCase();
  94. }
  95. context.font = style.fontSize + ' ' + style.fontFamily;
  96. context.textBaseline = 'top';
  97. context.fillStyle = style.color;
  98. context.fillText( string, x, y );
  99. }
  100. }
  101. function drawBorder( style, which, x, y, width, height ) {
  102. var borderWidth = style[ which + 'Width' ];
  103. var borderStyle = style[ which + 'Style' ];
  104. var borderColor = style[ which + 'Color' ];
  105. if ( borderWidth !== '0px' && borderStyle !== 'none' && borderColor !== 'transparent' && borderColor !== 'rgba(0, 0, 0, 0)' ) {
  106. context.strokeStyle = borderColor;
  107. context.beginPath();
  108. context.moveTo( x, y );
  109. context.lineTo( x + width, y + height );
  110. context.stroke();
  111. }
  112. }
  113. function drawElement( element, style ) {
  114. var x = 0, y = 0, width = 0, 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' || element.type === 'number' ) {
  142. clipper.add( { x: x, y: y, width: width, height: height } );
  143. drawText( style, x + parseInt( style.paddingLeft ), y + parseInt( style.paddingTop ), element.value );
  144. clipper.remove();
  145. }
  146. }
  147. /*
  148. // debug
  149. context.strokeStyle = '#' + Math.random().toString( 16 ).slice( - 3 );
  150. context.strokeRect( x - 0.5, y - 0.5, width + 1, height + 1 );
  151. */
  152. var isClipping = style.overflow === 'auto' || style.overflow === 'hidden';
  153. if ( isClipping ) clipper.add( { x: x, y: y, width: width, height: height } );
  154. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  155. drawElement( element.childNodes[ i ], style );
  156. }
  157. if ( isClipping ) clipper.remove();
  158. }
  159. const offset = element.getBoundingClientRect();
  160. let canvas;
  161. if ( canvases.has( element ) ) {
  162. canvas = canvases.get( element );
  163. } else {
  164. canvas = document.createElement( 'canvas' );
  165. canvas.width = offset.width;
  166. canvas.height = offset.height;
  167. }
  168. const context = canvas.getContext( '2d'/*, { alpha: false }*/ );
  169. const clipper = new Clipper( context );
  170. // console.time( 'drawElement' );
  171. drawElement( element );
  172. // console.timeEnd( 'drawElement' );
  173. return canvas;
  174. }
  175. function htmlevent( element, event, x, y ) {
  176. const mouseEventInit = {
  177. clientX: ( x * element.offsetWidth ) + element.offsetLeft,
  178. clientY: ( y * element.offsetHeight ) + element.offsetTop,
  179. view: element.ownerDocument.defaultView
  180. };
  181. window.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  182. const rect = element.getBoundingClientRect();
  183. x = x * rect.width + rect.left;
  184. y = y * rect.height + rect.top;
  185. function traverse( element ) {
  186. if ( element.nodeType !== 3 ) {
  187. const rect = element.getBoundingClientRect();
  188. if ( x > rect.left && x < rect.right && y > rect.top && y < rect.bottom ) {
  189. element.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  190. }
  191. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  192. traverse( element.childNodes[ i ] );
  193. }
  194. }
  195. }
  196. traverse( element );
  197. }
  198. export { HTMLMesh };