HTMLMesh.js 6.1 KB

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