index.html 13 KB

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