index.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. <script async src="https://www.googletagmanager.com/gtag/js?id=G-JPPX9MZGZ4"></script>
  17. <script>
  18. window.dataLayer = window.dataLayer || [];
  19. function gtag(){dataLayer.push(arguments);}
  20. gtag('js', new Date());
  21. gtag('config', 'G-JPPX9MZGZ4');
  22. </script>
  23. <div id="panel">
  24. <div id="header" translate="no">
  25. <h1><a href="https://threejs.org">three.js</a></h1>
  26. <div id="sections">
  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="clearSearchButton"></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 clearSearchButton = document.getElementById( 'clearSearchButton' );
  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 links = {};
  55. const validRedirects = new Map();
  56. const fragment = document.createDocumentFragment();
  57. let selected = null;
  58. init();
  59. async function init() {
  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. fragment.appendChild( header );
  69. for ( let i = 0; i < category.length; i ++ ) {
  70. const file = category[ i ];
  71. const link = createLink( file, tags[ file ] );
  72. fragment.appendChild( link );
  73. links[ file ] = link;
  74. validRedirects.set( file, file + '.html' );
  75. }
  76. }
  77. content.appendChild( fragment );
  78. if ( window.location.hash !== '' ) {
  79. const file = window.location.hash.substring( 1 );
  80. // use a predefined map of redirects to avoid untrusted URL redirection due to user-provided value
  81. if ( validRedirects.has( file ) === true ) {
  82. selectFile( file );
  83. updateLinkScroll();
  84. viewer.src = validRedirects.get( file );
  85. viewer.style.display = 'unset';
  86. }
  87. }
  88. if ( selected === null ) {
  89. panel.classList.add( 'open' );
  90. }
  91. if ( viewer.src === '' ) {
  92. viewer.srcdoc = document.getElementById( 'PlaceholderHTML' ).innerHTML;
  93. viewer.style.display = 'unset';
  94. }
  95. filterInput.value = extractQuery();
  96. if ( filterInput.value !== '' ) {
  97. panel.classList.add( 'searchFocused' );
  98. updateFilter( files, tags );
  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. clearSearchButton.onclick = function ( ) {
  110. filterInput.value = '';
  111. updateFilter( files, tags );
  112. filterInput.focus();
  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. updateLinkScroll();
  121. } );
  122. panelScrim.onclick = function ( event ) {
  123. event.preventDefault();
  124. panel.classList.toggle( 'open' );
  125. };
  126. previewsToggler.onclick = function ( event ) {
  127. event.preventDefault();
  128. content.classList.toggle( 'minimal' );
  129. };
  130. // iOS iframe auto-resize workaround
  131. if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {
  132. viewer.style.width = getComputedStyle( viewer ).width;
  133. viewer.style.height = getComputedStyle( viewer ).height;
  134. viewer.setAttribute( 'scrolling', 'no' );
  135. }
  136. }
  137. function createLink( file, tags ) {
  138. const community = Array.isArray( tags ) && tags.includes( 'community' ) ? '<span class="tag">community</span>' : '';
  139. let href = file + '.html';
  140. if ( file === 'css3d_mixed' ) {
  141. href += `?${ new Date().getTime() }`;
  142. }
  143. const template = `
  144. <div class="card">
  145. <a href="${ href }" target="viewer">
  146. <div class="cover">
  147. <img src="screenshots/${ file }.jpg" loading="lazy" width="400" />
  148. ${ community }
  149. </div>
  150. <div class="title">${ getName( file ) }</div>
  151. </a>
  152. </div>
  153. `;
  154. const link = createElementFromHTML( template );
  155. link.querySelector( 'a[target="viewer"]' ).addEventListener( 'click', function ( event ) {
  156. if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
  157. selectFile( file );
  158. } );
  159. return link;
  160. }
  161. function selectFile( file ) {
  162. if ( selected !== null ) links[ selected ].classList.remove( 'selected' );
  163. links[ file ].classList.add( 'selected' );
  164. window.location.hash = file;
  165. viewer.focus();
  166. viewer.style.display = 'unset';
  167. panel.classList.remove( 'open' );
  168. selected = file;
  169. // Reveal "View source" button and set attributes to this example
  170. viewSrcButton.style.display = '';
  171. viewSrcButton.href = 'https://github.com/mrdoob/three.js/blob/master/examples/' + selected + '.html';
  172. viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitHub';
  173. }
  174. function escapeRegExp( string ) {
  175. string = string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); // https://stackoverflow.com/a/6969486/5250847
  176. return '(?=.*' + string.split( ' ' ).join( ')(?=.*' ) + ')'; // match all words, in any order
  177. }
  178. function updateFilter( files, tags ) {
  179. let v = filterInput.value.trim();
  180. v = v.replace( /[\s_]+/gi, ' ' ); // replace multiple whitespaces or underscores with a single one
  181. if ( v !== '' ) {
  182. window.history.replaceState( {}, '', '?q=' + v + window.location.hash );
  183. } else {
  184. window.history.replaceState( {}, '', window.location.pathname + window.location.hash );
  185. }
  186. const exp = new RegExp( escapeRegExp( v ), 'gi' );
  187. for ( const key in files ) {
  188. const section = files[ key ];
  189. for ( let i = 0; i < section.length; i ++ ) {
  190. filterExample( section[ i ], exp, tags );
  191. }
  192. }
  193. layoutList( files );
  194. }
  195. function filterExample( file, exp, tags ) {
  196. const link = links[ file ];
  197. if ( file in tags ) file += ' ' + tags[ file ].join( ' ' );
  198. const res = file.replace( /_+/g, ' ' ).match( exp );
  199. if ( res && res.length > 0 ) {
  200. link.classList.remove( 'hidden' );
  201. } else {
  202. link.classList.add( 'hidden' );
  203. }
  204. }
  205. function getName( file ) {
  206. const name = file.split( '_' );
  207. name.shift();
  208. return name.join( ' / ' );
  209. }
  210. function layoutList( files ) {
  211. for ( const key in files ) {
  212. let collapsed = true;
  213. const section = files[ key ];
  214. for ( let i = 0; i < section.length; i ++ ) {
  215. const file = section[ i ];
  216. if ( links[ file ].classList.contains( 'hidden' ) === false ) {
  217. collapsed = false;
  218. break;
  219. }
  220. }
  221. const element = document.querySelector( 'h2[data-category="' + key + '"]' );
  222. if ( collapsed ) {
  223. element.classList.add( 'hidden' );
  224. } else {
  225. element.classList.remove( 'hidden' );
  226. }
  227. }
  228. }
  229. function extractQuery() {
  230. const search = window.location.search;
  231. if ( search.indexOf( '?q=' ) !== - 1 ) {
  232. return decodeURI( search.slice( 3 ) );
  233. }
  234. return '';
  235. }
  236. function createElementFromHTML( htmlString ) {
  237. const div = document.createElement( 'div' );
  238. div.innerHTML = htmlString.trim();
  239. return div.firstChild;
  240. }
  241. function updateLinkScroll() {
  242. if ( selected !== null ) {
  243. const link = links[ selected ];
  244. content.scrollTop = link.offsetTop - content.offsetTop - ( content.clientHeight - link.offsetHeight ) / 2;
  245. }
  246. }
  247. </script>
  248. <template id="PlaceholderHTML">
  249. <!DOCTYPE html>
  250. <html lang="en">
  251. <head>
  252. <meta charset="utf-8">
  253. <title>three.js examples</title>
  254. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  255. <link rel="stylesheet" type="text/css" href="../files/main.css">
  256. <style>
  257. html, body {
  258. height: 100%;
  259. }
  260. body {
  261. height: 100%;
  262. display: flex;
  263. align-items: center;
  264. justify-content: center;
  265. user-select: none;
  266. }
  267. </style>
  268. </head>
  269. <body>
  270. Select an example from the sidebar
  271. </body>
  272. </html>
  273. </template>
  274. </body>
  275. </html>
粤ICP备19079148号