index.html 13 KB

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