index.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. Promise.all( [
  48. fetch( 'files.json' ).then( response => response.json() ),
  49. fetch( 'tags.json' ).then( response => response.json() )
  50. ] ).then( ( json ) => {
  51. init( json[ 0 ], json[ 1 ] );
  52. } );
  53. function init( files, tags ) {
  54. content.appendChild( container );
  55. viewSrcButton.style.display = 'none';
  56. for ( const key in files ) {
  57. const section = files[ key ];
  58. const header = document.createElement( 'h2' );
  59. header.textContent = key;
  60. header.setAttribute( 'data-category', key );
  61. container.appendChild( header );
  62. for ( let i = 0; i < section.length; i ++ ) {
  63. const file = section[ i ];
  64. const link = createLink( file );
  65. container.appendChild( link );
  66. links[ file ] = link;
  67. validRedirects.set( file, file + '.html' );
  68. }
  69. }
  70. if ( window.location.hash !== '' ) {
  71. const file = window.location.hash.substring( 1 );
  72. // use a predefined map of redirects to avoid untrusted URL redirection due to user-provided value
  73. if ( validRedirects.has( file ) === true ) {
  74. selectFile( file );
  75. viewer.src = validRedirects.get( file );
  76. }
  77. }
  78. filterInput.value = extractQuery();
  79. if ( filterInput.value !== '' ) {
  80. panel.classList.add( 'searchFocused' );
  81. }
  82. updateFilter( files, tags );
  83. // Events
  84. filterInput.onfocus = function ( ) {
  85. panel.classList.add( 'searchFocused' );
  86. };
  87. filterInput.onblur = function ( ) {
  88. if ( filterInput.value === '' ) {
  89. panel.classList.remove( 'searchFocused' );
  90. }
  91. };
  92. exitSearchButton.onclick = function ( ) {
  93. filterInput.value = '';
  94. updateFilter( files, tags );
  95. panel.classList.remove( 'searchFocused' );
  96. };
  97. filterInput.addEventListener( 'input', function () {
  98. updateFilter( files, tags );
  99. } );
  100. expandButton.addEventListener( 'click', function ( event ) {
  101. event.preventDefault();
  102. panel.classList.toggle( 'open' );
  103. } );
  104. panelScrim.onclick = function ( event ) {
  105. event.preventDefault();
  106. panel.classList.toggle( 'open' );
  107. };
  108. previewsToggler.onclick = function ( event ) {
  109. event.preventDefault();
  110. content.classList.toggle( 'minimal' );
  111. };
  112. // iOS iframe auto-resize workaround
  113. if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {
  114. viewer.style.width = getComputedStyle( viewer ).width;
  115. viewer.style.height = getComputedStyle( viewer ).height;
  116. viewer.setAttribute( 'scrolling', 'no' );
  117. }
  118. }
  119. function createLink( file ) {
  120. const template = [
  121. '<div class="card">',
  122. ' <a href="' + file + '.html" target="viewer">',
  123. ' <div class="cover">',
  124. ' <img src="screenshots/' + file + '.jpg" loading="lazy" width="400" />',
  125. ' </div>',
  126. ' <div class="title">' + getName( file ) + '</div>',
  127. ' </a>',
  128. '</div>'
  129. ].join( "\n" );
  130. const link = createElementFromHTML( template );
  131. link.querySelector( 'a[target="viewer"]' ).addEventListener( 'click', function ( event ) {
  132. if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
  133. selectFile( file );
  134. } );
  135. return link;
  136. }
  137. function selectFile( file ) {
  138. if ( selected !== null ) links[ selected ].classList.remove( 'selected' );
  139. links[ file ].classList.add( 'selected' );
  140. window.location.hash = file;
  141. viewer.focus();
  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 p = window.location.search.indexOf( '?q=' );
  209. if ( p !== - 1 ) {
  210. return decodeURI( window.location.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号