index.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. <script async src="https://www.googletagmanager.com/gtag/js?id=G-JPPX9MZGZ4"></script>
  13. <script>
  14. window.dataLayer = window.dataLayer || [];
  15. function gtag(){dataLayer.push(arguments);}
  16. gtag('js', new Date());
  17. gtag('config', 'G-JPPX9MZGZ4');
  18. </script>
  19. <div id="panel">
  20. <div id="header">
  21. <h1><a href="https://threejs.org">three.js</a></h1>
  22. <div id="sections">
  23. <a href="../docs/index.html#manual/introduction/Creating-a-scene">docs</a>
  24. <span class="selected">examples</span>
  25. </div>
  26. <div id="expandButton"></div>
  27. </div>
  28. <div id="panelScrim"></div>
  29. <div id="contentWrapper">
  30. <div id="inputWrapper">
  31. <input placeholder="" type="text" id="filterInput" autocorrect="off" autocapitalize="off" spellcheck="false" />
  32. <div id="clearSearchButton"></div>
  33. </div>
  34. <div id="content">
  35. <img id="previewsToggler" src="./files/thumbnails.svg" width="20" height="20" />
  36. </div>
  37. </div>
  38. </div>
  39. <iframe id="viewer" name="viewer" allow="fullscreen; xr-spatial-tracking;"></iframe>
  40. <a id="button" target="_blank"><img src="../files/ic_code_black_24dp.svg"></a>
  41. <script>
  42. const panel = document.getElementById( 'panel' );
  43. const content = document.getElementById( 'content' );
  44. const viewer = document.getElementById( 'viewer' );
  45. const filterInput = document.getElementById( 'filterInput' );
  46. const clearSearchButton = document.getElementById( 'clearSearchButton' );
  47. const expandButton = document.getElementById( 'expandButton' );
  48. const viewSrcButton = document.getElementById( 'button' );
  49. const panelScrim = document.getElementById( 'panelScrim' );
  50. const previewsToggler = document.getElementById( 'previewsToggler' );
  51. const sectionLink = document.querySelector( '#sections > a' );
  52. const sectionDefaultHref = sectionLink.href;
  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 category = 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 < category.length; i ++ ) {
  70. const file = category[ i ];
  71. const link = createLink( file, tags[ 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. content.querySelector( `a[href="${ file }.html"]` ).scrollIntoView();
  82. selectFile( file );
  83. viewer.src = validRedirects.get( file );
  84. viewer.style.display = 'unset';
  85. }
  86. }
  87. if ( viewer.src === '' ) {
  88. viewer.srcdoc = document.getElementById( 'PlaceholderHTML' ).innerHTML;
  89. viewer.style.display = 'unset';
  90. }
  91. filterInput.value = extractQuery();
  92. if ( filterInput.value !== '' ) {
  93. panel.classList.add( 'searchFocused' );
  94. updateFilter( files, tags );
  95. } else {
  96. updateLink( '' );
  97. }
  98. // Events
  99. filterInput.onfocus = function ( ) {
  100. panel.classList.add( 'searchFocused' );
  101. };
  102. filterInput.onblur = function ( ) {
  103. if ( filterInput.value === '' ) {
  104. panel.classList.remove( 'searchFocused' );
  105. }
  106. };
  107. clearSearchButton.onclick = function ( ) {
  108. filterInput.value = '';
  109. updateFilter( files, tags );
  110. filterInput.focus();
  111. };
  112. filterInput.addEventListener( 'input', function () {
  113. updateFilter( files, tags );
  114. } );
  115. expandButton.addEventListener( 'click', function ( event ) {
  116. event.preventDefault();
  117. panel.classList.toggle( 'open' );
  118. } );
  119. panelScrim.onclick = function ( event ) {
  120. event.preventDefault();
  121. panel.classList.toggle( 'open' );
  122. };
  123. previewsToggler.onclick = function ( event ) {
  124. event.preventDefault();
  125. content.classList.toggle( 'minimal' );
  126. };
  127. // iOS iframe auto-resize workaround
  128. if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {
  129. viewer.style.width = getComputedStyle( viewer ).width;
  130. viewer.style.height = getComputedStyle( viewer ).height;
  131. viewer.setAttribute( 'scrolling', 'no' );
  132. }
  133. }
  134. function createLink( file, tags ) {
  135. const external = Array.isArray( tags ) && tags.includes( 'external' ) ? ' <span class="tag">external</span>' : '';
  136. const template = `
  137. <div class="card">
  138. <a href="${ file }.html" target="viewer">
  139. <div class="cover">
  140. <img src="screenshots/${ file }.jpg" loading="lazy" width="400" />
  141. </div>
  142. <div class="title">${ getName( file ) }${ external }</div>
  143. </a>
  144. </div>
  145. `;
  146. const link = createElementFromHTML( template );
  147. link.querySelector( 'a[target="viewer"]' ).addEventListener( 'click', function ( event ) {
  148. if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
  149. selectFile( file );
  150. } );
  151. return link;
  152. }
  153. function selectFile( file ) {
  154. if ( selected !== null ) links[ selected ].classList.remove( 'selected' );
  155. links[ file ].classList.add( 'selected' );
  156. window.location.hash = file;
  157. viewer.focus();
  158. viewer.style.display = 'unset';
  159. panel.classList.remove( 'open' );
  160. selected = file;
  161. // Reveal "View source" button and set attributes to this example
  162. viewSrcButton.style.display = '';
  163. viewSrcButton.href = 'https://github.com/mrdoob/three.js/blob/master/examples/' + selected + '.html';
  164. viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitHub';
  165. }
  166. function escapeRegExp( string ) {
  167. string = string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); // https://stackoverflow.com/a/6969486/5250847
  168. return '(?=.*' + string.split( ' ' ).join( ')(?=.*' ) + ')'; // match all words, in any order
  169. }
  170. function updateFilter( files, tags ) {
  171. let v = filterInput.value.trim();
  172. v = v.replace( /\s+/gi, ' ' ); // replace multiple whitespaces with a single one
  173. if ( v !== '' ) {
  174. window.history.replaceState( {}, '', '?q=' + v + window.location.hash );
  175. } else {
  176. window.history.replaceState( {}, '', window.location.pathname + window.location.hash );
  177. }
  178. const exp = new RegExp( escapeRegExp( v ), 'gi' );
  179. for ( const key in files ) {
  180. const section = files[ key ];
  181. for ( let i = 0; i < section.length; i ++ ) {
  182. filterExample( section[ i ], exp, tags );
  183. }
  184. }
  185. layoutList( files );
  186. updateLink( v );
  187. }
  188. function updateLink( search ) {
  189. // update docs link
  190. if ( search ) {
  191. const link = sectionLink.href.split( /[?#]/ )[ 0 ];
  192. sectionLink.href = `${link}?q=${search}`;
  193. } else {
  194. sectionLink.href = sectionDefaultHref;
  195. }
  196. }
  197. function filterExample( file, exp, tags ) {
  198. const link = links[ file ];
  199. if ( file in tags ) file += ' ' + tags[ file ].join( ' ' );
  200. const res = file.replace( /_+/g, ' ' ).match( exp );
  201. if ( res && res.length > 0 ) {
  202. link.classList.remove( 'hidden' );
  203. } else {
  204. link.classList.add( 'hidden' );
  205. }
  206. }
  207. function getName( file ) {
  208. const name = file.split( '_' );
  209. name.shift();
  210. return name.join( ' / ' );
  211. }
  212. function layoutList( files ) {
  213. for ( const key in files ) {
  214. let collapsed = true;
  215. const section = files[ key ];
  216. for ( let i = 0; i < section.length; i ++ ) {
  217. const file = section[ i ];
  218. if ( links[ file ].classList.contains( 'hidden' ) === false ) {
  219. collapsed = false;
  220. break;
  221. }
  222. }
  223. const element = document.querySelector( 'h2[data-category="' + key + '"]' );
  224. if ( collapsed ) {
  225. element.classList.add( 'hidden' );
  226. } else {
  227. element.classList.remove( 'hidden' );
  228. }
  229. }
  230. }
  231. function extractQuery() {
  232. const search = window.location.search;
  233. if ( search.indexOf( '?q=' ) !== - 1 ) {
  234. return decodeURI( search.slice( 3 ) );
  235. }
  236. return '';
  237. }
  238. function createElementFromHTML( htmlString ) {
  239. const div = document.createElement( 'div' );
  240. div.innerHTML = htmlString.trim();
  241. return div.firstChild;
  242. }
  243. </script>
  244. <template id="PlaceholderHTML">
  245. <!DOCTYPE html>
  246. <html lang="en">
  247. <head>
  248. <meta charset="utf-8">
  249. <title>three.js examples</title>
  250. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  251. <link rel="stylesheet" type="text/css" href="../files/main.css">
  252. <style>
  253. html, body {
  254. height: 100%;
  255. }
  256. body {
  257. height: 100%;
  258. display: flex;
  259. align-items: center;
  260. justify-content: center;
  261. user-select: none;
  262. }
  263. </style>
  264. </head>
  265. <body>
  266. Select an example from the sidebar
  267. </body>
  268. </html>
  269. </template>
  270. </body>
  271. </html>