index.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>three.js examples</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link rel="shortcut icon" href="../files/favicon.ico" />
  8. <link rel="stylesheet" type="text/css" href="../files/main.css">
  9. </head>
  10. <body>
  11. <div id="panel">
  12. <div id="header">
  13. <h1><a href="https://threejs.org">three.js</a></h1>
  14. <div id="sections">
  15. <a href="../docs/index.html#manual/introduction/Creating-a-scene">docs</a>
  16. <span class="selected">examples</span>
  17. </div>
  18. <div id="expandButton"></div>
  19. </div>
  20. <div id="panelScrim"></div>
  21. <div id="contentWrapper">
  22. <div id="inputWrapper">
  23. <input placeholder="" type="text" id="filterInput" autocorrect="off" autocapitalize="off" spellcheck="false" />
  24. <div id="exitSearchButton"></div>
  25. </div>
  26. <div id="content">
  27. <img id="previewsToggler" src="./files/thumbnails.svg" width="20" height="20" />
  28. </div>
  29. </div>
  30. </div>
  31. <iframe id="viewer" name="viewer" allow="fullscreen; xr-spatial-tracking;"></iframe>
  32. <a id="button" target="_blank"><img src="../files/ic_code_black_24dp.svg"></a>
  33. <script>
  34. const panel = document.getElementById( 'panel' );
  35. const content = document.getElementById( 'content' );
  36. const viewer = document.getElementById( 'viewer' );
  37. const filterInput = document.getElementById( 'filterInput' );
  38. const exitSearchButton = document.getElementById( 'exitSearchButton' );
  39. const expandButton = document.getElementById( 'expandButton' );
  40. const viewSrcButton = document.getElementById( 'button' );
  41. const panelScrim = document.getElementById( 'panelScrim' );
  42. const previewsToggler = document.getElementById( 'previewsToggler' );
  43. const links = {};
  44. const validRedirects = new Map();
  45. const container = document.createElement( 'div' );
  46. let selected = null;
  47. init();
  48. async function init() {
  49. content.appendChild( container );
  50. viewSrcButton.style.display = 'none';
  51. const files = await ( await fetch( 'files.json' ) ).json();
  52. const tags = await ( await fetch( 'tags.json' ) ).json();
  53. for ( const key in files ) {
  54. const section = files[ key ];
  55. const header = document.createElement( 'h2' );
  56. header.textContent = key;
  57. header.setAttribute( 'data-category', key );
  58. container.appendChild( header );
  59. for ( let i = 0; i < section.length; i ++ ) {
  60. const file = section[ i ];
  61. const link = createLink( file );
  62. container.appendChild( link );
  63. links[ file ] = link;
  64. validRedirects.set( file, file + '.html' );
  65. }
  66. }
  67. if ( window.location.hash !== '' ) {
  68. const file = window.location.hash.substring( 1 );
  69. // use a predefined map of redirects to avoid untrusted URL redirection due to user-provided value
  70. if ( validRedirects.has( file ) === true ) {
  71. selectFile( file );
  72. viewer.src = validRedirects.get( file );
  73. viewer.style.display = 'unset';
  74. }
  75. }
  76. filterInput.value = extractQuery();
  77. if ( filterInput.value !== '' ) {
  78. panel.classList.add( 'searchFocused' );
  79. updateFilter( files, tags );
  80. }
  81. // Events
  82. filterInput.onfocus = function ( ) {
  83. panel.classList.add( 'searchFocused' );
  84. };
  85. filterInput.onblur = function ( ) {
  86. if ( filterInput.value === '' ) {
  87. panel.classList.remove( 'searchFocused' );
  88. }
  89. };
  90. exitSearchButton.onclick = function ( ) {
  91. filterInput.value = '';
  92. updateFilter( files, tags );
  93. panel.classList.remove( 'searchFocused' );
  94. };
  95. filterInput.addEventListener( 'input', function () {
  96. updateFilter( files, tags );
  97. } );
  98. expandButton.addEventListener( 'click', function ( event ) {
  99. event.preventDefault();
  100. panel.classList.toggle( 'open' );
  101. } );
  102. panelScrim.onclick = function ( event ) {
  103. event.preventDefault();
  104. panel.classList.toggle( 'open' );
  105. };
  106. previewsToggler.onclick = function ( event ) {
  107. event.preventDefault();
  108. content.classList.toggle( 'minimal' );
  109. };
  110. // iOS iframe auto-resize workaround
  111. if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {
  112. viewer.style.width = getComputedStyle( viewer ).width;
  113. viewer.style.height = getComputedStyle( viewer ).height;
  114. viewer.setAttribute( 'scrolling', 'no' );
  115. }
  116. }
  117. function createLink( file ) {
  118. const template = `
  119. <div class="card">
  120. <a href="${file}.html" target="viewer">
  121. <div class="cover">
  122. <img src="screenshots/${ file }.jpg" loading="lazy" width="400" />
  123. </div>
  124. <div class="title">${getName( file )}</div>
  125. </a>
  126. </div>
  127. `;
  128. const link = createElementFromHTML( template );
  129. link.querySelector( 'a[target="viewer"]' ).addEventListener( 'click', function ( event ) {
  130. if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
  131. selectFile( file );
  132. } );
  133. return link;
  134. }
  135. function selectFile( file ) {
  136. if ( selected !== null ) links[ selected ].classList.remove( 'selected' );
  137. links[ file ].classList.add( 'selected' );
  138. window.location.hash = file;
  139. viewer.focus();
  140. viewer.style.display = 'unset';
  141. panel.classList.remove( 'open' );
  142. selected = file;
  143. // Reveal "View source" button and set attributes to this example
  144. viewSrcButton.style.display = '';
  145. viewSrcButton.href = 'https://github.com/mrdoob/three.js/blob/master/examples/' + selected + '.html';
  146. viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitHub';
  147. }
  148. function updateFilter( files, tags ) {
  149. let v = filterInput.value.trim();
  150. v = v.replace( /\s+/gi, ' ' ); // replace multiple whitespaces with a single one
  151. if ( v !== '' ) {
  152. window.history.replaceState( {}, '', '?q=' + v + window.location.hash );
  153. } else {
  154. window.history.replaceState( {}, '', window.location.pathname + window.location.hash );
  155. }
  156. const exp = new RegExp( v, 'gi' );
  157. for ( const key in files ) {
  158. const section = files[ key ];
  159. for ( let i = 0; i < section.length; i ++ ) {
  160. filterExample( section[ i ], exp, tags );
  161. }
  162. }
  163. layoutList( files );
  164. }
  165. function filterExample( file, exp, tags ) {
  166. const link = links[ file ];
  167. const name = getName( file );
  168. if ( file in tags ) file += ' ' + tags[ file ].join( ' ' );
  169. const res = file.match( exp );
  170. let text;
  171. if ( res && res.length > 0 ) {
  172. link.classList.remove( 'hidden' );
  173. for ( let i = 0; i < res.length; i ++ ) {
  174. text = name.replace( res[ i ], '<b>' + res[ i ] + '</b>' );
  175. }
  176. link.querySelector( '.title' ).innerHTML = text;
  177. } else {
  178. link.classList.add( 'hidden' );
  179. link.querySelector( '.title' ).innerHTML = name;
  180. }
  181. }
  182. function getName( file ) {
  183. const name = file.split( '_' );
  184. name.shift();
  185. return name.join( ' / ' );
  186. }
  187. function layoutList( files ) {
  188. for ( const key in files ) {
  189. let collapsed = true;
  190. const section = files[ key ];
  191. for ( let i = 0; i < section.length; i ++ ) {
  192. const file = section[ i ];
  193. if ( links[ file ].classList.contains( 'hidden' ) === false ) {
  194. collapsed = false;
  195. break;
  196. }
  197. }
  198. const element = document.querySelector( 'h2[data-category="' + key + '"]' );
  199. if ( collapsed ) {
  200. element.classList.add( 'hidden' );
  201. } else {
  202. element.classList.remove( 'hidden' );
  203. }
  204. }
  205. }
  206. function extractQuery() {
  207. const search = window.location.search;
  208. if ( search.indexOf( '?q=' ) !== - 1 ) {
  209. return decodeURI( search.substr( 3 ) );
  210. }
  211. return '';
  212. }
  213. function createElementFromHTML( htmlString ) {
  214. const div = document.createElement( 'div' );
  215. div.innerHTML = htmlString.trim();
  216. return div.firstChild;
  217. }
  218. </script>
  219. </body>
  220. </html>