HTMLMesh.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. function html2canvas( element ) {
  44. var range = document.createRange();
  45. function Clipper( context ) {
  46. var clips = [];
  47. var isClipping = false;
  48. function doClip() {
  49. if ( isClipping ) {
  50. isClipping = false;
  51. context.restore();
  52. }
  53. if ( clips.length === 0 ) return;
  54. var minX = - Infinity, minY = - Infinity;
  55. var maxX = Infinity, maxY = Infinity;
  56. for ( var i = 0; i < clips.length; i ++ ) {
  57. var clip = clips[ i ];
  58. minX = Math.max( minX, clip.x );
  59. minY = Math.max( minY, clip.y );
  60. maxX = Math.min( maxX, clip.x + clip.width );
  61. maxY = Math.min( maxY, clip.y + clip.height );
  62. }
  63. context.save();
  64. context.beginPath();
  65. context.rect( minX, minY, maxX - minX, maxY - minY );
  66. context.clip();
  67. isClipping = true;
  68. }
  69. return {
  70. add: function ( clip ) {
  71. clips.push( clip );
  72. doClip();
  73. },
  74. remove: function () {
  75. clips.pop();
  76. doClip();
  77. }
  78. };
  79. }
  80. function drawText( style, x, y, string ) {
  81. if ( string !== '' ) {
  82. if ( style.textTransform === 'uppercase' ) {
  83. string = string.toUpperCase();
  84. }
  85. context.font = style.fontSize + ' ' + style.fontFamily;
  86. context.textBaseline = 'top';
  87. context.fillStyle = style.color;
  88. context.fillText( string, x, y );
  89. }
  90. }
  91. function drawBorder( style, which, x, y, width, height ) {
  92. var borderWidth = style[ which + 'Width' ];
  93. var borderStyle = style[ which + 'Style' ];
  94. var borderColor = style[ which + 'Color' ];
  95. if ( borderWidth !== '0px' && borderStyle !== 'none' && borderColor !== 'transparent' && borderColor !== 'rgba(0, 0, 0, 0)' ) {
  96. context.strokeStyle = borderColor;
  97. context.beginPath();
  98. context.moveTo( x, y );
  99. context.lineTo( x + width, y + height );
  100. context.stroke();
  101. }
  102. }
  103. function drawElement( element, style ) {
  104. var x = 0, y = 0, width = 0, height = 0;
  105. if ( element.nodeType === 3 ) {
  106. // text
  107. range.selectNode( element );
  108. var rect = range.getBoundingClientRect();
  109. x = rect.left - offset.left - 0.5;
  110. y = rect.top - offset.top - 0.5;
  111. width = rect.width;
  112. height = rect.height;
  113. drawText( style, x, y, element.nodeValue.trim() );
  114. } else {
  115. if ( element.style.display === 'none' ) return;
  116. var rect = element.getBoundingClientRect();
  117. x = rect.left - offset.left - 0.5;
  118. y = rect.top - offset.top - 0.5;
  119. width = rect.width;
  120. height = rect.height;
  121. style = window.getComputedStyle( element );
  122. var backgroundColor = style.backgroundColor;
  123. if ( backgroundColor !== 'transparent' && backgroundColor !== 'rgba(0, 0, 0, 0)' ) {
  124. context.fillStyle = backgroundColor;
  125. context.fillRect( x, y, width, height );
  126. }
  127. drawBorder( style, 'borderTop', x, y, width, 0 );
  128. drawBorder( style, 'borderLeft', x, y, 0, height );
  129. drawBorder( style, 'borderBottom', x, y + height, width, 0 );
  130. drawBorder( style, 'borderRight', x + width, y, 0, height );
  131. if ( element.type === 'color' || element.type === 'text' ) {
  132. clipper.add( { x: x, y: y, width: width, height: height } );
  133. drawText( style, x + parseInt( style.paddingLeft ), y + parseInt( style.paddingTop ), element.value );
  134. clipper.remove();
  135. }
  136. }
  137. /*
  138. // debug
  139. context.strokeStyle = '#' + Math.random().toString( 16 ).slice( - 3 );
  140. context.strokeRect( x - 0.5, y - 0.5, width + 1, height + 1 );
  141. */
  142. var isClipping = style.overflow === 'auto' || style.overflow === 'hidden';
  143. if ( isClipping ) clipper.add( { x: x, y: y, width: width, height: height } );
  144. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  145. drawElement( element.childNodes[ i ], style );
  146. }
  147. if ( isClipping ) clipper.remove();
  148. }
  149. var offset = element.getBoundingClientRect();
  150. var canvas = document.createElement( 'canvas' );
  151. canvas.width = offset.width;
  152. canvas.height = offset.height;
  153. var context = canvas.getContext( '2d'/*, { alpha: false }*/ );
  154. var clipper = new Clipper( context );
  155. // console.time( 'drawElement' );
  156. drawElement( element );
  157. // console.timeEnd( 'drawElement' );
  158. return canvas;
  159. }
  160. function htmlevent( element, event, x, y ) {
  161. const mouseEventInit = {
  162. clientX: ( x * element.offsetWidth ) + element.offsetLeft,
  163. clientY: ( y * element.offsetHeight ) + element.offsetTop,
  164. view: element.ownerDocument.defaultView
  165. };
  166. window.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  167. const rect = element.getBoundingClientRect();
  168. x = x * rect.width + rect.left;
  169. y = y * rect.height + rect.top;
  170. function traverse( element ) {
  171. if ( element.nodeType !== 3 ) {
  172. const rect = element.getBoundingClientRect();
  173. if ( x > rect.left && x < rect.right && y > rect.top && y < rect.bottom ) {
  174. element.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  175. }
  176. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  177. traverse( element.childNodes[ i ] );
  178. }
  179. }
  180. }
  181. traverse( element );
  182. }
  183. export { HTMLMesh };