index.html 13 KB

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