index.html 13 KB

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