1
0

index.html 9.2 KB

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