2
0

index.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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="clearSearchButton"></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 clearSearchButton = document.getElementById( 'clearSearchButton' );
  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 sectionLink = document.querySelector( '#sections > a' );
  45. const sectionDefaultHref = sectionLink.href;
  46. const links = {};
  47. const validRedirects = new Map();
  48. const container = document.createElement( 'div' );
  49. let selected = null;
  50. init();
  51. async function init() {
  52. content.appendChild( container );
  53. viewSrcButton.style.display = 'none';
  54. const files = await ( await fetch( 'files.json' ) ).json();
  55. const tags = await ( await fetch( 'tags.json' ) ).json();
  56. for ( const key in files ) {
  57. const category = 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 < category.length; i ++ ) {
  63. const file = category[ i ];
  64. const link = createLink( file, tags[ 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. content.querySelector( `a[href="${ file }.html"]` ).scrollIntoView();
  75. selectFile( file );
  76. viewer.src = validRedirects.get( file );
  77. viewer.style.display = 'unset';
  78. }
  79. }
  80. if ( viewer.src === '' ) {
  81. viewer.srcdoc = document.getElementById( 'PlaceholderHTML' ).innerHTML;
  82. viewer.style.display = 'unset';
  83. }
  84. filterInput.value = extractQuery();
  85. if ( filterInput.value !== '' ) {
  86. panel.classList.add( 'searchFocused' );
  87. updateFilter( files, tags );
  88. } else {
  89. updateLink( '' );
  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. clearSearchButton.onclick = function ( ) {
  101. filterInput.value = '';
  102. updateFilter( files, tags );
  103. filterInput.focus();
  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, tags ) {
  128. const external = Array.isArray( tags ) && tags.includes( 'external' ) ? ' <span class="tag">external</span>' : '';
  129. const template = `
  130. <div class="card">
  131. <a href="${ file }.html" target="viewer">
  132. <div class="cover">
  133. <img src="screenshots/${ file }.jpg" loading="lazy" width="400" />
  134. </div>
  135. <div class="title">${ getName( file ) }${ external }</div>
  136. </a>
  137. </div>
  138. `;
  139. const link = createElementFromHTML( template );
  140. link.querySelector( 'a[target="viewer"]' ).addEventListener( 'click', function ( event ) {
  141. if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
  142. selectFile( file );
  143. } );
  144. return link;
  145. }
  146. function selectFile( file ) {
  147. if ( selected !== null ) links[ selected ].classList.remove( 'selected' );
  148. links[ file ].classList.add( 'selected' );
  149. window.location.hash = file;
  150. viewer.focus();
  151. viewer.style.display = 'unset';
  152. panel.classList.remove( 'open' );
  153. selected = file;
  154. // Reveal "View source" button and set attributes to this example
  155. viewSrcButton.style.display = '';
  156. viewSrcButton.href = 'https://github.com/mrdoob/three.js/blob/master/examples/' + selected + '.html';
  157. viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitHub';
  158. }
  159. function escapeRegExp( string ) {
  160. string = string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); // https://stackoverflow.com/a/6969486/5250847
  161. return '(?=.*' + string.split( ' ' ).join( ')(?=.*' ) + ')'; // match all words, in any order
  162. }
  163. function updateFilter( files, tags ) {
  164. let v = filterInput.value.trim();
  165. v = v.replace( /\s+/gi, ' ' ); // replace multiple whitespaces with a single one
  166. if ( v !== '' ) {
  167. window.history.replaceState( {}, '', '?q=' + v + window.location.hash );
  168. } else {
  169. window.history.replaceState( {}, '', window.location.pathname + window.location.hash );
  170. }
  171. const exp = new RegExp( escapeRegExp( v ), 'gi' );
  172. for ( const key in files ) {
  173. const section = files[ key ];
  174. for ( let i = 0; i < section.length; i ++ ) {
  175. filterExample( section[ i ], exp, tags );
  176. }
  177. }
  178. layoutList( files );
  179. updateLink( v );
  180. }
  181. function updateLink( search ) {
  182. // update docs link
  183. if ( search ) {
  184. const link = sectionLink.href.split( /[?#]/ )[ 0 ];
  185. sectionLink.href = `${link}?q=${search}`;
  186. } else {
  187. sectionLink.href = sectionDefaultHref;
  188. }
  189. }
  190. function filterExample( file, exp, tags ) {
  191. const link = links[ file ];
  192. if ( file in tags ) file += ' ' + tags[ file ].join( ' ' );
  193. const res = file.replace( /_+/g, ' ' ).match( exp );
  194. if ( res && res.length > 0 ) {
  195. link.classList.remove( 'hidden' );
  196. } else {
  197. link.classList.add( 'hidden' );
  198. }
  199. }
  200. function getName( file ) {
  201. const name = file.split( '_' );
  202. name.shift();
  203. return name.join( ' / ' );
  204. }
  205. function layoutList( files ) {
  206. for ( const key in files ) {
  207. let collapsed = true;
  208. const section = files[ key ];
  209. for ( let i = 0; i < section.length; i ++ ) {
  210. const file = section[ i ];
  211. if ( links[ file ].classList.contains( 'hidden' ) === false ) {
  212. collapsed = false;
  213. break;
  214. }
  215. }
  216. const element = document.querySelector( 'h2[data-category="' + key + '"]' );
  217. if ( collapsed ) {
  218. element.classList.add( 'hidden' );
  219. } else {
  220. element.classList.remove( 'hidden' );
  221. }
  222. }
  223. }
  224. function extractQuery() {
  225. const search = window.location.search;
  226. if ( search.indexOf( '?q=' ) !== - 1 ) {
  227. return decodeURI( search.slice( 3 ) );
  228. }
  229. return '';
  230. }
  231. function createElementFromHTML( htmlString ) {
  232. const div = document.createElement( 'div' );
  233. div.innerHTML = htmlString.trim();
  234. return div.firstChild;
  235. }
  236. </script>
  237. <template id="PlaceholderHTML">
  238. <!DOCTYPE html>
  239. <html lang="en">
  240. <head>
  241. <meta charset="utf-8">
  242. <title>three.js examples</title>
  243. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  244. <link rel="stylesheet" type="text/css" href="../files/main.css">
  245. <style>
  246. html, body {
  247. height: 100%;
  248. }
  249. body {
  250. height: 100%;
  251. display: flex;
  252. align-items: center;
  253. justify-content: center;
  254. user-select: none;
  255. }
  256. </style>
  257. </head>
  258. <body>
  259. Select an example from the sidebar
  260. </body>
  261. </html>
  262. </template>
  263. </body>
  264. </html>