index.html 7.6 KB

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