index.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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" allowfullscreen allowvr onmousewheel=""></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. }
  74. }
  75. filterInput.value = extractQuery();
  76. if ( filterInput.value !== '' ) {
  77. panel.classList.add( 'searchFocused' );
  78. }
  79. updateFilter( files, tags );
  80. // Events
  81. filterInput.onfocus = function ( ) {
  82. panel.classList.add( 'searchFocused' );
  83. };
  84. filterInput.onblur = function ( ) {
  85. if ( filterInput.value === '' ) {
  86. panel.classList.remove( 'searchFocused' );
  87. }
  88. };
  89. exitSearchButton.onclick = function ( ) {
  90. filterInput.value = '';
  91. updateFilter( files, tags );
  92. panel.classList.remove( 'searchFocused' );
  93. };
  94. filterInput.addEventListener( 'input', function () {
  95. updateFilter( files, tags );
  96. } );
  97. expandButton.addEventListener( 'click', function ( event ) {
  98. event.preventDefault();
  99. panel.classList.toggle( 'open' );
  100. } );
  101. panelScrim.onclick = function ( event ) {
  102. event.preventDefault();
  103. panel.classList.toggle( 'open' );
  104. };
  105. previewsToggler.onclick = function ( event ) {
  106. event.preventDefault();
  107. content.classList.toggle( 'minimal' );
  108. };
  109. // iOS iframe auto-resize workaround
  110. if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {
  111. viewer.style.width = getComputedStyle( viewer ).width;
  112. viewer.style.height = getComputedStyle( viewer ).height;
  113. viewer.setAttribute( 'scrolling', 'no' );
  114. }
  115. }
  116. function createLink( file ) {
  117. const template = `
  118. <div class="card">
  119. <a href="${file}.html" target="viewer">
  120. <div class="cover">
  121. <img src="screenshots/${ file }.jpg" loading="lazy" width="400" />
  122. </div>
  123. <div class="title">${getName( file )}</div>
  124. </a>
  125. </div>
  126. `;
  127. const link = createElementFromHTML( template );
  128. link.querySelector( 'a[target="viewer"]' ).addEventListener( 'click', function ( event ) {
  129. if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
  130. selectFile( file );
  131. } );
  132. return link;
  133. }
  134. function selectFile( file ) {
  135. if ( selected !== null ) links[ selected ].classList.remove( 'selected' );
  136. links[ file ].classList.add( 'selected' );
  137. window.location.hash = file;
  138. viewer.focus();
  139. panel.classList.remove( 'open' );
  140. selected = file;
  141. // Reveal "View source" button and set attributes to this example
  142. viewSrcButton.style.display = '';
  143. viewSrcButton.href = 'https://github.com/mrdoob/three.js/blob/master/examples/' + selected + '.html';
  144. viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitHub';
  145. }
  146. function updateFilter( files, tags ) {
  147. let v = filterInput.value.trim();
  148. v = v.replace( /\s+/gi, ' ' ); // replace multiple whitespaces with a single one
  149. if ( v !== '' ) {
  150. window.history.replaceState( {}, '', '?q=' + v + window.location.hash );
  151. } else {
  152. window.history.replaceState( {}, '', window.location.pathname + window.location.hash );
  153. }
  154. const exp = new RegExp( v, 'gi' );
  155. for ( const key in files ) {
  156. const section = files[ key ];
  157. for ( let i = 0; i < section.length; i ++ ) {
  158. filterExample( section[ i ], exp, tags );
  159. }
  160. }
  161. layoutList( files );
  162. }
  163. function filterExample( file, exp, tags ) {
  164. const link = links[ file ];
  165. const name = getName( file );
  166. if ( file in tags ) file += ' ' + tags[ file ].join( ' ' );
  167. const res = file.match( exp );
  168. let text;
  169. if ( res && res.length > 0 ) {
  170. link.classList.remove( 'hidden' );
  171. for ( let i = 0; i < res.length; i ++ ) {
  172. text = name.replace( res[ i ], '<b>' + res[ i ] + '</b>' );
  173. }
  174. link.querySelector( '.title' ).innerHTML = text;
  175. } else {
  176. link.classList.add( 'hidden' );
  177. link.querySelector( '.title' ).innerHTML = name;
  178. }
  179. }
  180. function getName( file ) {
  181. const name = file.split( '_' );
  182. name.shift();
  183. return name.join( ' / ' );
  184. }
  185. function layoutList( files ) {
  186. for ( const key in files ) {
  187. let collapsed = true;
  188. const section = files[ key ];
  189. for ( let i = 0; i < section.length; i ++ ) {
  190. const file = section[ i ];
  191. if ( links[ file ].classList.contains( 'hidden' ) === false ) {
  192. collapsed = false;
  193. break;
  194. }
  195. }
  196. const element = document.querySelector( 'h2[data-category="' + key + '"]' );
  197. if ( collapsed ) {
  198. element.classList.add( 'hidden' );
  199. } else {
  200. element.classList.remove( 'hidden' );
  201. }
  202. }
  203. }
  204. function extractQuery() {
  205. const p = window.location.search.indexOf( '?q=' );
  206. if ( p !== - 1 ) {
  207. return decodeURI( window.location.search.substr( 3 ) );
  208. }
  209. return '';
  210. }
  211. function createElementFromHTML( htmlString ) {
  212. const div = document.createElement( 'div' );
  213. div.innerHTML = htmlString.trim();
  214. return div.firstChild;
  215. }
  216. </script>
  217. </body>
  218. </html>