index.html 14 KB

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