HTMLMesh.js 6.2 KB

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