index.html 8.2 KB

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