index.html 9.2 KB

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