HTMLMesh.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. import {
  2. CanvasTexture,
  3. LinearFilter,
  4. Mesh,
  5. MeshBasicMaterial,
  6. PlaneGeometry,
  7. SRGBColorSpace,
  8. Color
  9. } from 'three';
  10. /**
  11. * This class can be used to render a DOM element onto a canvas and use it as a texture
  12. * for a plane mesh.
  13. *
  14. * A typical use case for this class is to render the GUI of `lil-gui` as a texture so it
  15. * is compatible for VR.
  16. *
  17. * ```js
  18. * const gui = new GUI( { width: 300 } ); // create lil-gui instance
  19. *
  20. * const mesh = new HTMLMesh( gui.domElement );
  21. * scene.add( mesh );
  22. * ```
  23. *
  24. * @augments Mesh
  25. * @three_import import { HTMLMesh } from 'three/addons/interactive/HTMLMesh.js';
  26. */
  27. class HTMLMesh extends Mesh {
  28. /**
  29. * Constructs a new HTML mesh.
  30. *
  31. * @param {HTMLElement} dom - The DOM element to display as a plane mesh.
  32. */
  33. constructor( dom ) {
  34. const texture = new HTMLTexture( dom );
  35. const geometry = new PlaneGeometry( texture.image.width * 0.001, texture.image.height * 0.001 );
  36. const material = new MeshBasicMaterial( { map: texture, toneMapped: false, transparent: true } );
  37. super( geometry, material );
  38. function onEvent( event ) {
  39. material.map.dispatchDOMEvent( event );
  40. }
  41. this.addEventListener( 'mousedown', onEvent );
  42. this.addEventListener( 'mousemove', onEvent );
  43. this.addEventListener( 'mouseup', onEvent );
  44. this.addEventListener( 'click', onEvent );
  45. /**
  46. * Frees the GPU-related resources allocated by this instance and removes all event listeners.
  47. * Call this method whenever this instance is no longer used in your app.
  48. */
  49. this.dispose = function () {
  50. geometry.dispose();
  51. material.dispose();
  52. material.map.dispose();
  53. canvases.delete( dom );
  54. this.removeEventListener( 'mousedown', onEvent );
  55. this.removeEventListener( 'mousemove', onEvent );
  56. this.removeEventListener( 'mouseup', onEvent );
  57. this.removeEventListener( 'click', onEvent );
  58. };
  59. }
  60. }
  61. class HTMLTexture extends CanvasTexture {
  62. constructor( dom ) {
  63. super( html2canvas( dom ) );
  64. this.dom = dom;
  65. this.anisotropy = 16;
  66. this.colorSpace = SRGBColorSpace;
  67. this.minFilter = LinearFilter;
  68. this.magFilter = LinearFilter;
  69. this.generateMipmaps = false;
  70. // Create an observer on the DOM, and run html2canvas update in the next loop
  71. const observer = new MutationObserver( () => {
  72. if ( ! this.scheduleUpdate ) {
  73. // ideally should use xr.requestAnimationFrame, here setTimeout to avoid passing the renderer
  74. this.scheduleUpdate = setTimeout( () => this.update(), 16 );
  75. }
  76. } );
  77. const config = { attributes: true, childList: true, subtree: true, characterData: true };
  78. observer.observe( dom, config );
  79. this.observer = observer;
  80. }
  81. dispatchDOMEvent( event ) {
  82. if ( event.data ) {
  83. htmlevent( this.dom, event.type, event.data.x, event.data.y );
  84. }
  85. }
  86. update() {
  87. this.image = html2canvas( this.dom );
  88. this.needsUpdate = true;
  89. this.scheduleUpdate = null;
  90. }
  91. dispose() {
  92. if ( this.observer ) {
  93. this.observer.disconnect();
  94. }
  95. this.scheduleUpdate = clearTimeout( this.scheduleUpdate );
  96. super.dispose();
  97. }
  98. }
  99. //
  100. const canvases = new WeakMap();
  101. function html2canvas( element ) {
  102. const range = document.createRange();
  103. const color = new Color();
  104. function Clipper( context ) {
  105. const clips = [];
  106. let isClipping = false;
  107. function doClip() {
  108. if ( isClipping ) {
  109. isClipping = false;
  110. context.restore();
  111. }
  112. if ( clips.length === 0 ) return;
  113. let minX = - Infinity, minY = - Infinity;
  114. let maxX = Infinity, maxY = Infinity;
  115. for ( let i = 0; i < clips.length; i ++ ) {
  116. const clip = clips[ i ];
  117. minX = Math.max( minX, clip.x );
  118. minY = Math.max( minY, clip.y );
  119. maxX = Math.min( maxX, clip.x + clip.width );
  120. maxY = Math.min( maxY, clip.y + clip.height );
  121. }
  122. context.save();
  123. context.beginPath();
  124. context.rect( minX, minY, maxX - minX, maxY - minY );
  125. context.clip();
  126. isClipping = true;
  127. }
  128. return {
  129. add: function ( clip ) {
  130. clips.push( clip );
  131. doClip();
  132. },
  133. remove: function () {
  134. clips.pop();
  135. doClip();
  136. }
  137. };
  138. }
  139. function drawText( style, x, y, string ) {
  140. if ( string !== '' ) {
  141. if ( style.textTransform === 'uppercase' ) {
  142. string = string.toUpperCase();
  143. }
  144. context.font = style.fontWeight + ' ' + style.fontSize + ' ' + style.fontFamily;
  145. context.textBaseline = 'top';
  146. context.fillStyle = style.color;
  147. context.fillText( string, x, y + parseFloat( style.fontSize ) * 0.1 );
  148. }
  149. }
  150. function buildRectPath( x, y, w, h, r ) {
  151. if ( w < 2 * r ) r = w / 2;
  152. if ( h < 2 * r ) r = h / 2;
  153. context.beginPath();
  154. context.moveTo( x + r, y );
  155. context.arcTo( x + w, y, x + w, y + h, r );
  156. context.arcTo( x + w, y + h, x, y + h, r );
  157. context.arcTo( x, y + h, x, y, r );
  158. context.arcTo( x, y, x + w, y, r );
  159. context.closePath();
  160. }
  161. function drawBorder( style, which, x, y, width, height ) {
  162. const borderWidth = style[ which + 'Width' ];
  163. const borderStyle = style[ which + 'Style' ];
  164. const borderColor = style[ which + 'Color' ];
  165. if ( borderWidth !== '0px' && borderStyle !== 'none' && borderColor !== 'transparent' && borderColor !== 'rgba(0, 0, 0, 0)' ) {
  166. context.strokeStyle = borderColor;
  167. context.lineWidth = parseFloat( borderWidth );
  168. context.beginPath();
  169. context.moveTo( x, y );
  170. context.lineTo( x + width, y + height );
  171. context.stroke();
  172. }
  173. }
  174. function drawElement( element, style ) {
  175. // Do not render invisible elements, comments and scripts.
  176. if ( element.nodeType === Node.COMMENT_NODE || element.nodeName === 'SCRIPT' || ( element.style && element.style.display === 'none' ) ) {
  177. return;
  178. }
  179. let x = 0, y = 0, width = 0, height = 0;
  180. if ( element.nodeType === Node.TEXT_NODE ) {
  181. // text
  182. range.selectNode( element );
  183. const rect = range.getBoundingClientRect();
  184. x = rect.left - offset.left - 0.5;
  185. y = rect.top - offset.top - 0.5;
  186. width = rect.width;
  187. height = rect.height;
  188. drawText( style, x, y, element.nodeValue.trim() );
  189. } else if ( element instanceof HTMLCanvasElement ) {
  190. // Canvas element
  191. const rect = element.getBoundingClientRect();
  192. x = rect.left - offset.left - 0.5;
  193. y = rect.top - offset.top - 0.5;
  194. context.save();
  195. const dpr = window.devicePixelRatio;
  196. context.scale( 1 / dpr, 1 / dpr );
  197. context.drawImage( element, x, y );
  198. context.restore();
  199. } else if ( element instanceof HTMLImageElement ) {
  200. const rect = element.getBoundingClientRect();
  201. x = rect.left - offset.left - 0.5;
  202. y = rect.top - offset.top - 0.5;
  203. width = rect.width;
  204. height = rect.height;
  205. context.drawImage( element, x, y, width, height );
  206. } else {
  207. const rect = element.getBoundingClientRect();
  208. x = rect.left - offset.left - 0.5;
  209. y = rect.top - offset.top - 0.5;
  210. width = rect.width;
  211. height = rect.height;
  212. style = window.getComputedStyle( element );
  213. // Get the border of the element used for fill and border
  214. buildRectPath( x, y, width, height, parseFloat( style.borderRadius ) );
  215. const backgroundColor = style.backgroundColor;
  216. if ( backgroundColor !== 'transparent' && backgroundColor !== 'rgba(0, 0, 0, 0)' ) {
  217. context.fillStyle = backgroundColor;
  218. context.fill();
  219. }
  220. // If all the borders match then stroke the round rectangle
  221. const borders = [ 'borderTop', 'borderLeft', 'borderBottom', 'borderRight' ];
  222. let match = true;
  223. let prevBorder = null;
  224. for ( const border of borders ) {
  225. if ( prevBorder !== null ) {
  226. match = ( style[ border + 'Width' ] === style[ prevBorder + 'Width' ] ) &&
  227. ( style[ border + 'Color' ] === style[ prevBorder + 'Color' ] ) &&
  228. ( style[ border + 'Style' ] === style[ prevBorder + 'Style' ] );
  229. }
  230. if ( match === false ) break;
  231. prevBorder = border;
  232. }
  233. if ( match === true ) {
  234. // They all match so stroke the rectangle from before allows for border-radius
  235. const width = parseFloat( style.borderTopWidth );
  236. if ( style.borderTopWidth !== '0px' && style.borderTopStyle !== 'none' && style.borderTopColor !== 'transparent' && style.borderTopColor !== 'rgba(0, 0, 0, 0)' ) {
  237. context.strokeStyle = style.borderTopColor;
  238. context.lineWidth = width;
  239. context.stroke();
  240. }
  241. } else {
  242. // Otherwise draw individual borders
  243. drawBorder( style, 'borderTop', x, y, width, 0 );
  244. drawBorder( style, 'borderLeft', x, y, 0, height );
  245. drawBorder( style, 'borderBottom', x, y + height, width, 0 );
  246. drawBorder( style, 'borderRight', x + width, y, 0, height );
  247. }
  248. if ( element instanceof HTMLInputElement ) {
  249. let accentColor = style.accentColor;
  250. if ( accentColor === undefined || accentColor === 'auto' ) accentColor = style.color;
  251. color.set( accentColor );
  252. const luminance = Math.sqrt( 0.299 * ( color.r ** 2 ) + 0.587 * ( color.g ** 2 ) + 0.114 * ( color.b ** 2 ) );
  253. const accentTextColor = luminance < 0.5 ? 'white' : '#111111';
  254. if ( element.type === 'radio' ) {
  255. buildRectPath( x, y, width, height, height );
  256. context.fillStyle = 'white';
  257. context.strokeStyle = accentColor;
  258. context.lineWidth = 1;
  259. context.fill();
  260. context.stroke();
  261. if ( element.checked ) {
  262. buildRectPath( x + 2, y + 2, width - 4, height - 4, height );
  263. context.fillStyle = accentColor;
  264. context.strokeStyle = accentTextColor;
  265. context.lineWidth = 2;
  266. context.fill();
  267. context.stroke();
  268. }
  269. }
  270. if ( element.type === 'checkbox' ) {
  271. buildRectPath( x, y, width, height, 2 );
  272. context.fillStyle = element.checked ? accentColor : 'white';
  273. context.strokeStyle = element.checked ? accentTextColor : accentColor;
  274. context.lineWidth = 1;
  275. context.stroke();
  276. context.fill();
  277. if ( element.checked ) {
  278. const currentTextAlign = context.textAlign;
  279. context.textAlign = 'center';
  280. const properties = {
  281. color: accentTextColor,
  282. fontFamily: style.fontFamily,
  283. fontSize: height + 'px',
  284. fontWeight: 'bold'
  285. };
  286. drawText( properties, x + ( width / 2 ), y, '✔' );
  287. context.textAlign = currentTextAlign;
  288. }
  289. }
  290. if ( element.type === 'range' ) {
  291. const [ min, max, value ] = [ 'min', 'max', 'value' ].map( property => parseFloat( element[ property ] ) );
  292. const position = ( ( value - min ) / ( max - min ) ) * ( width - height );
  293. buildRectPath( x, y + ( height / 4 ), width, height / 2, height / 4 );
  294. context.fillStyle = accentTextColor;
  295. context.strokeStyle = accentColor;
  296. context.lineWidth = 1;
  297. context.fill();
  298. context.stroke();
  299. buildRectPath( x, y + ( height / 4 ), position + ( height / 2 ), height / 2, height / 4 );
  300. context.fillStyle = accentColor;
  301. context.fill();
  302. buildRectPath( x + position, y, height, height, height / 2 );
  303. context.fillStyle = accentColor;
  304. context.fill();
  305. }
  306. if ( element.type === 'color' || element.type === 'text' || element.type === 'number' ) {
  307. clipper.add( { x: x, y: y, width: width, height: height } );
  308. drawText( style, x + parseInt( style.paddingLeft ), y + parseInt( style.paddingTop ), element.value );
  309. clipper.remove();
  310. }
  311. }
  312. }
  313. /*
  314. // debug
  315. context.strokeStyle = '#' + Math.random().toString( 16 ).slice( - 3 );
  316. context.strokeRect( x - 0.5, y - 0.5, width + 1, height + 1 );
  317. */
  318. const isClipping = style.overflow === 'auto' || style.overflow === 'hidden';
  319. if ( isClipping ) clipper.add( { x: x, y: y, width: width, height: height } );
  320. for ( let i = 0; i < element.childNodes.length; i ++ ) {
  321. drawElement( element.childNodes[ i ], style );
  322. }
  323. if ( isClipping ) clipper.remove();
  324. }
  325. const offset = element.getBoundingClientRect();
  326. let canvas = canvases.get( element );
  327. if ( canvas === undefined ) {
  328. canvas = document.createElement( 'canvas' );
  329. canvas.width = offset.width;
  330. canvas.height = offset.height;
  331. canvases.set( element, canvas );
  332. }
  333. const context = canvas.getContext( '2d'/*, { alpha: false }*/ );
  334. const clipper = new Clipper( context );
  335. // console.time( 'drawElement' );
  336. context.clearRect( 0, 0, canvas.width, canvas.height );
  337. drawElement( element );
  338. // console.timeEnd( 'drawElement' );
  339. return canvas;
  340. }
  341. function htmlevent( element, event, x, y ) {
  342. const mouseEventInit = {
  343. clientX: ( x * element.offsetWidth ) + element.offsetLeft,
  344. clientY: ( y * element.offsetHeight ) + element.offsetTop,
  345. view: element.ownerDocument.defaultView
  346. };
  347. window.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  348. const rect = element.getBoundingClientRect();
  349. x = x * rect.width + rect.left;
  350. y = y * rect.height + rect.top;
  351. function traverse( element ) {
  352. if ( element.nodeType !== Node.TEXT_NODE && element.nodeType !== Node.COMMENT_NODE ) {
  353. const rect = element.getBoundingClientRect();
  354. if ( x > rect.left && x < rect.right && y > rect.top && y < rect.bottom ) {
  355. element.dispatchEvent( new MouseEvent( event, mouseEventInit ) );
  356. if ( element instanceof HTMLInputElement && element.type === 'range' && ( event === 'mousedown' || event === 'click' ) ) {
  357. const [ min, max ] = [ 'min', 'max' ].map( property => parseFloat( element[ property ] ) );
  358. const width = rect.width;
  359. const offsetX = x - rect.x;
  360. const proportion = offsetX / width;
  361. element.value = min + ( max - min ) * proportion;
  362. element.dispatchEvent( new InputEvent( 'input', { bubbles: true } ) );
  363. }
  364. }
  365. for ( let i = 0; i < element.childNodes.length; i ++ ) {
  366. traverse( element.childNodes[ i ] );
  367. }
  368. }
  369. }
  370. traverse( element );
  371. }
  372. export { HTMLMesh };