index.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. <meta property="og:title" content="three.js examples">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/index.html">
  10. <meta property="og:image" content="https://threejs.org/files/og_image.jpg">
  11. <link rel="shortcut icon" href="../files/favicon_white.ico" media="(prefers-color-scheme: dark)"/>
  12. <link rel="shortcut icon" href="../files/favicon.ico" media="(prefers-color-scheme: light)" />
  13. <link rel="stylesheet" type="text/css" href="../files/main.css">
  14. </head>
  15. <body>
  16. <div id="panel">
  17. <div id="header" translate="no">
  18. <h1><a href="https://threejs.org">three.js</a></h1>
  19. <div id="sections">
  20. <span class="selected">examples</span>
  21. </div>
  22. <div id="expandButton"></div>
  23. </div>
  24. <div id="panelScrim"></div>
  25. <div id="contentWrapper">
  26. <div id="inputWrapper">
  27. <input placeholder="" type="text" id="filterInput" autocorrect="off" autocapitalize="off" spellcheck="false" />
  28. <div id="clearSearchButton"></div>
  29. </div>
  30. <div id="content">
  31. <img id="previewsToggler" src="./files/thumbnails.svg" width="20" height="20" />
  32. </div>
  33. </div>
  34. </div>
  35. <iframe id="viewer" name="viewer" allow="fullscreen; xr-spatial-tracking;"></iframe>
  36. <a id="button" target="_blank"><img src="../files/ic_code_black_24dp.svg"></a>
  37. <script>
  38. const panel = document.getElementById( 'panel' );
  39. const content = document.getElementById( 'content' );
  40. const viewer = document.getElementById( 'viewer' );
  41. const filterInput = document.getElementById( 'filterInput' );
  42. const clearSearchButton = document.getElementById( 'clearSearchButton' );
  43. const expandButton = document.getElementById( 'expandButton' );
  44. const viewSrcButton = document.getElementById( 'button' );
  45. const panelScrim = document.getElementById( 'panelScrim' );
  46. const previewsToggler = document.getElementById( 'previewsToggler' );
  47. const links = {};
  48. const validRedirects = new Map();
  49. const fragment = document.createDocumentFragment();
  50. let selected = null;
  51. init();
  52. async function init() {
  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. fragment.appendChild( header );
  62. for ( let i = 0; i < category.length; i ++ ) {
  63. const file = category[ i ];
  64. const link = createLink( file, tags[ file ] );
  65. fragment.appendChild( link );
  66. links[ file ] = link;
  67. validRedirects.set( file, file + '.html' );
  68. }
  69. }
  70. content.appendChild( fragment );
  71. if ( window.location.hash !== '' ) {
  72. const file = window.location.hash.substring( 1 );
  73. // use a predefined map of redirects to avoid untrusted URL redirection due to user-provided value
  74. if ( validRedirects.has( file ) === true ) {
  75. selectFile( file );
  76. updateLinkScroll();
  77. viewer.src = validRedirects.get( file );
  78. viewer.style.display = 'unset';
  79. }
  80. }
  81. if ( selected === null ) {
  82. panel.classList.add( 'open' );
  83. }
  84. if ( viewer.src === '' ) {
  85. viewer.srcdoc = document.getElementById( 'PlaceholderHTML' ).innerHTML;
  86. viewer.style.display = 'unset';
  87. }
  88. filterInput.value = extractQuery();
  89. if ( filterInput.value !== '' ) {
  90. panel.classList.add( 'searchFocused' );
  91. updateFilter( files, tags );
  92. }
  93. // Events
  94. filterInput.onfocus = function ( ) {
  95. panel.classList.add( 'searchFocused' );
  96. };
  97. filterInput.onblur = function ( ) {
  98. if ( filterInput.value === '' ) {
  99. panel.classList.remove( 'searchFocused' );
  100. }
  101. };
  102. clearSearchButton.onclick = function ( ) {
  103. filterInput.value = '';
  104. updateFilter( files, tags );
  105. filterInput.focus();
  106. };
  107. filterInput.addEventListener( 'input', function () {
  108. updateFilter( files, tags );
  109. } );
  110. expandButton.addEventListener( 'click', function ( event ) {
  111. event.preventDefault();
  112. panel.classList.toggle( 'open' );
  113. updateLinkScroll();
  114. } );
  115. panelScrim.onclick = function ( event ) {
  116. event.preventDefault();
  117. panel.classList.toggle( 'open' );
  118. };
  119. previewsToggler.onclick = function ( event ) {
  120. event.preventDefault();
  121. content.classList.toggle( 'minimal' );
  122. };
  123. // iOS iframe auto-resize workaround
  124. if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {
  125. viewer.style.width = getComputedStyle( viewer ).width;
  126. viewer.style.height = getComputedStyle( viewer ).height;
  127. viewer.setAttribute( 'scrolling', 'no' );
  128. }
  129. }
  130. function createLink( file, tags ) {
  131. const community = Array.isArray( tags ) && tags.includes( 'community' ) ? '<span class="tag">community</span>' : '';
  132. let href = file + '.html';
  133. if ( file === 'css3d_mixed' ) {
  134. href += `?${ new Date().getTime() }`;
  135. }
  136. const template = `
  137. <div class="card">
  138. <a href="${ href }" target="viewer">
  139. <div class="cover">
  140. <img src="screenshots/${ file }.jpg" loading="lazy" width="400" />
  141. ${ community }
  142. </div>
  143. <div class="title">${ getName( file ) }</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 or underscores 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. function updateLinkScroll() {
  235. if ( selected !== null ) {
  236. const link = links[ selected ];
  237. content.scrollTop = link.offsetTop - content.offsetTop - ( content.clientHeight - link.offsetHeight ) / 2;
  238. }
  239. }
  240. </script>
  241. <template id="PlaceholderHTML">
  242. <!DOCTYPE html>
  243. <html lang="en">
  244. <head>
  245. <meta charset="utf-8">
  246. <title>three.js examples</title>
  247. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  248. <link rel="stylesheet" type="text/css" href="../files/main.css">
  249. <style>
  250. html, body {
  251. height: 100%;
  252. }
  253. body {
  254. height: 100%;
  255. display: flex;
  256. align-items: center;
  257. justify-content: center;
  258. user-select: none;
  259. }
  260. </style>
  261. </head>
  262. <body>
  263. Select an example from the sidebar
  264. </body>
  265. </html>
  266. </template>
  267. </body>
  268. </html>
粤ICP备19079148号