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