index.html 12 KB

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