index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>three.js manual</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. <!-- console sandbox -->
  11. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. window.THREE = THREE;
  14. </script>
  15. </head>
  16. <body>
  17. <div id="panel">
  18. <div id="header" translate="no">
  19. <h1><a href="https://threejs.org">three.js</a></h1>
  20. <div id="sections">
  21. <a href="../docs/">docs</a>
  22. <span class="selected">manual</span>
  23. </div>
  24. <div id="expandButton"></div>
  25. </div>
  26. <div id="panelScrim"></div>
  27. <div id="contentWrapper">
  28. <div id="inputWrapper">
  29. <input placeholder="" type="text" id="filterInput" autocorrect="off" autocapitalize="off" spellcheck="false" />
  30. <div id="clearSearchButton"></div>
  31. </div>
  32. <br/>
  33. <div id="content"></div>
  34. </div>
  35. </div>
  36. <iframe name="viewer"></iframe>
  37. <script>
  38. const panel = document.querySelector( '#panel' );
  39. const content = document.querySelector( '#content' );
  40. const expandButton = document.querySelector( '#expandButton' );
  41. const clearSearchButton = document.querySelector( '#clearSearchButton' );
  42. const panelScrim = document.querySelector( '#panelScrim' );
  43. const filterInput = document.querySelector( '#filterInput' );
  44. let iframe = document.querySelector( 'iframe' );
  45. const pageProperties = {};
  46. const titles = {};
  47. const categoryElements = [];
  48. let navigation;
  49. init();
  50. async function init() {
  51. const list = await ( await fetch( 'list.json' ) ).json();
  52. redirectLegacyLanguage();
  53. // Functionality for hamburger button (on small devices)
  54. expandButton.onclick = function ( event ) {
  55. event.preventDefault();
  56. panel.classList.toggle( 'open' );
  57. };
  58. panelScrim.onclick = function ( event ) {
  59. event.preventDefault();
  60. panel.classList.toggle( 'open' );
  61. };
  62. // Functionality for search/filter input field
  63. filterInput.onfocus = function () {
  64. panel.classList.add( 'searchFocused' );
  65. };
  66. filterInput.onblur = function () {
  67. if ( filterInput.value === '' ) {
  68. panel.classList.remove( 'searchFocused' );
  69. }
  70. };
  71. filterInput.oninput = function () {
  72. updateFilter();
  73. };
  74. clearSearchButton.onclick = function () {
  75. filterInput.value = '';
  76. updateFilter();
  77. filterInput.focus();
  78. };
  79. // Activate content and title change on browser navigation
  80. window.onpopstate = function () {
  81. redirectLegacyLanguage();
  82. updateNavigation();
  83. createNewIframe();
  84. };
  85. // Redirect legacy localized deep links entered directly in the address bar
  86. window.addEventListener( 'hashchange', function () {
  87. if ( redirectLegacyLanguage() ) {
  88. updateNavigation();
  89. createNewIframe();
  90. }
  91. } );
  92. // Create the navigation panel and configure the iframe
  93. createNavigation( list );
  94. createNewIframe();
  95. // Handle search query
  96. filterInput.value = extractQuery();
  97. if ( filterInput.value !== '' ) {
  98. panel.classList.add( 'searchFocused' );
  99. updateFilter();
  100. }
  101. }
  102. // Navigation Panel
  103. function createLink( pageName, pageURL ) {
  104. const link = document.createElement( 'a' );
  105. const url = new URL( 'pages/' + pageURL, window.location.href );
  106. url.pathname += '.html';
  107. link.href = url.href;
  108. link.textContent = pageName;
  109. link.setAttribute( 'target', 'viewer' );
  110. link.addEventListener( 'click', function ( event ) {
  111. if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
  112. window.location.hash = pageURL;
  113. panel.classList.remove( 'open' );
  114. updateNavigation();
  115. } );
  116. return link;
  117. }
  118. function createNavigation( list ) {
  119. if ( navigation !== undefined ) {
  120. content.removeChild( navigation );
  121. }
  122. // Create the navigation panel using data from list.js
  123. navigation = document.createElement( 'div' );
  124. content.appendChild( navigation );
  125. const categories = list;
  126. const selectedPage = window.location.hash.substring( 1 ).replace( /\.html$/, '' );
  127. for ( const category in categories ) {
  128. // Create categories
  129. if ( category === '---' ) {
  130. const separator = document.createElement( 'hr' );
  131. navigation.appendChild( separator );
  132. continue;
  133. }
  134. const pages = categories[ category ];
  135. const categoryContainer = document.createElement( 'div' );
  136. navigation.appendChild( categoryContainer );
  137. const categoryHead = document.createElement( 'h3' );
  138. categoryHead.textContent = category;
  139. categoryContainer.appendChild( categoryHead );
  140. const categoryContent = document.createElement( 'ul' );
  141. categoryContainer.appendChild( categoryContent );
  142. for ( const pageName in pages ) {
  143. // Create page links
  144. const pageURL = pages[ pageName ];
  145. // Localisation
  146. const listElement = document.createElement( 'li' );
  147. categoryContent.appendChild( listElement );
  148. const linkElement = createLink( pageName, pageURL );
  149. listElement.appendChild( linkElement );
  150. // select current page
  151. if ( pageURL === selectedPage ) {
  152. linkElement.classList.add( 'selected' );
  153. }
  154. // Gather the main properties for the current subpage
  155. pageProperties[ pageName ] = {
  156. category: category,
  157. pageURL: pageURL,
  158. linkElement: linkElement
  159. };
  160. // Gather the document titles (used for easy access on browser navigation)
  161. titles[ pageURL ] = pageName;
  162. }
  163. // Gather the category elements for easy access on filtering
  164. categoryElements.push( categoryContent );
  165. }
  166. }
  167. function updateNavigation() {
  168. const selectedPage = window.location.hash.substring( 1 ).replace( /\.html$/, '' );
  169. content.querySelectorAll( 'a' ).forEach( function ( item ) {
  170. if ( selectedPage !== '' && item.href.includes( selectedPage ) ) {
  171. item.classList.add( 'selected' );
  172. } else {
  173. item.classList.remove( 'selected' );
  174. }
  175. } );
  176. }
  177. // Filtering
  178. function extractQuery() {
  179. const search = window.location.search;
  180. if ( search.indexOf( '?q=' ) !== - 1 ) {
  181. return decodeURI( search.slice( 3 ) );
  182. }
  183. return '';
  184. }
  185. function updateFilter() {
  186. let v = filterInput.value.trim();
  187. v = v.replace( /\s+/gi, ' ' ); // replace multiple whitespaces with a single one
  188. if ( v !== '' ) {
  189. window.history.replaceState( {}, '', '?q=' + v + window.location.hash );
  190. } else {
  191. window.history.replaceState( {}, '', window.location.pathname + window.location.hash );
  192. }
  193. //
  194. const regExp = new RegExp( filterInput.value, 'gi' );
  195. for ( let pageName in pageProperties ) {
  196. const linkElement = pageProperties[ pageName ].linkElement;
  197. const categoryClassList = linkElement.parentElement.classList;
  198. const filterResults = pageName.match( regExp );
  199. if ( filterResults !== null && filterResults.length > 0 ) {
  200. pageName = pageName.replaceAll( regExp, '<b>$&</b>' );
  201. categoryClassList.remove( 'hidden' );
  202. linkElement.innerHTML = pageName;
  203. } else {
  204. // Hide all non-matching page names
  205. categoryClassList.add( 'hidden' );
  206. }
  207. }
  208. displayFilteredPanel();
  209. }
  210. function displayFilteredPanel() {
  211. // Show/hide categories depending on their content
  212. // First check if at least one page in this category is not hidden
  213. categoryElements.forEach( function ( category ) {
  214. const pages = category.children;
  215. const pagesLength = pages.length;
  216. const sectionClassList = category.parentElement.classList;
  217. let hideCategory = true;
  218. for ( let i = 0; i < pagesLength; i ++ ) {
  219. const pageClassList = pages[ i ].classList;
  220. if ( ! pageClassList.contains( 'hidden' ) ) {
  221. hideCategory = false;
  222. }
  223. }
  224. // If and only if all page names are hidden, hide the whole category
  225. if ( hideCategory ) {
  226. sectionClassList.add( 'hidden' );
  227. } else {
  228. sectionClassList.remove( 'hidden' );
  229. }
  230. } );
  231. }
  232. // Routing
  233. function setUrl( href ) {
  234. // yea I know this is hacky.
  235. const re = /^(\/(?:manual\/|docs\/#?))(.*?)$/;
  236. const url = new URL( href );
  237. if ( url.origin === window.location.origin ) {
  238. const hrefNoOrigin = url.href.slice( url.origin.length );
  239. const m = re.exec( hrefNoOrigin );
  240. const [ , base, path ] = m;
  241. if ( base.includes( 'manual' ) ) {
  242. const newHash = `#${ path.replace( 'pages/', '' ).replace( '.html', '' ) }`;
  243. // Only create new iframe if we're actually changing pages.
  244. // We could just be going to an anchor on the same page.
  245. const newPrefix = newHash.split( '#' )[ 1 ];
  246. const oldPrefix = window.location.hash.split( '#' )[ 1 ];
  247. if ( newPrefix === oldPrefix ) {
  248. const newUrl = new URL( window.location.href );
  249. newUrl.hash = newHash;
  250. window.history.pushState( {}, '', newUrl.href );
  251. } else {
  252. window.location.hash = newHash;
  253. updateNavigation();
  254. createNewIframe();
  255. }
  256. return;
  257. }
  258. }
  259. window.location.href = href;
  260. }
  261. function setTitle( title ) {
  262. document.title = `${title} - three.js manual`;
  263. }
  264. function redirectLegacyLanguage() {
  265. // Legacy language-qualified deep links (e.g. #en/installation, #fr/installation)
  266. // now resolve to the language-neutral URL (e.g. #installation).
  267. const legacyLanguage = /^#(en|fr|ja|ko|ru|zh)(\/|$)/;
  268. if ( legacyLanguage.test( window.location.hash ) ) {
  269. window.history.replaceState( null, '', window.location.hash.replace( legacyLanguage, '#' ) );
  270. return true;
  271. }
  272. return false;
  273. }
  274. function createNewIframe() {
  275. // Change the content displayed in the iframe
  276. // First separate the member part of the fragment (if existing)
  277. const hash = window.location.hash.substring( 1 );
  278. const splitHash = decomposePageName( hash, '.', '#' );
  279. // Creating a new Iframe instead of assigning a new src is
  280. // a cross-browser solution to allow normal browser navigation;
  281. // - only assigning a new src would result in two history states each time.
  282. // Note: iframe.contentWindow.location.replace(hash) should work, too,
  283. // but it doesn't work in Edge with links from the subpages!
  284. const oldIframe = iframe;
  285. iframe = oldIframe.cloneNode();
  286. if ( hash ) {
  287. // We can have 2 hashes. One for the main page, one for the page it's referencing
  288. // In other words
  289. // #somePage#someSectionOfPage
  290. const subHash = splitHash[ 0 ].indexOf( '#' );
  291. let src;
  292. if ( subHash >= 0 ) {
  293. const beforeSubHash = splitHash[ 0 ].slice( 0, subHash );
  294. const afterSubHash = splitHash[ 0 ].slice( subHash );
  295. src = `pages/${beforeSubHash}.html${afterSubHash}${splitHash[ 1 ]}`;
  296. } else {
  297. src = 'pages/' + splitHash[ 0 ] + '.html' + splitHash[ 1 ];
  298. }
  299. iframe.src = src; // lgtm[js/client-side-unvalidated-url-redirection]
  300. iframe.style.display = 'unset';
  301. } else {
  302. iframe.src = '';
  303. iframe.style.display = 'none';
  304. }
  305. document.body.replaceChild( iframe, oldIframe );
  306. }
  307. function decomposePageName( pageName, oldDelimiter, newDelimiter ) {
  308. // Helper function for separating the member (if existing) from the pageName
  309. // For example: 'Geometry.morphTarget' can be converted to
  310. // ['Geometry', '.morphTarget'] or ['Geometry', '#morphTarget']
  311. // Note: According RFC 3986 no '#' allowed inside of an URL fragment!
  312. let parts = [];
  313. const dotIndex = pageName.indexOf( oldDelimiter );
  314. if ( dotIndex !== - 1 ) {
  315. parts = pageName.split( oldDelimiter );
  316. parts[ 1 ] = newDelimiter + parts[ 1 ];
  317. } else {
  318. parts[ 0 ] = pageName;
  319. parts[ 1 ] = '';
  320. }
  321. return parts;
  322. }
  323. //
  324. console.log( [
  325. ' __ __',
  326. ' __/ __\\ / __\\__ ____ _____ _____',
  327. '/ __/ /\\/ / /___\\/ ____\\/ _____\\/ _____\\',
  328. '\\/_ __/ / _ / / __/ / __ / / __ /_ __ _____',
  329. '/ / / / / / / / / / / / ___/ / ___/\\ _\\/ __\\/ _____\\',
  330. '\\/__/ \\/__/\\/__/\\/__/ \\/_____/\\/_____/\\/__/ / / / ___/',
  331. ' / __/ / \\__ \\',
  332. ' \\/____/\\/_____/'
  333. ].join( '\n' ) );
  334. </script>
  335. </body>
  336. </html>
粤ICP备19079148号