index.html 7.8 KB

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