index.html 9.6 KB

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