index.html 13 KB

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