index.html 7.7 KB
Newer Older
M
Mr.doob 已提交
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
M
Mr.doob 已提交
4
		<meta charset="utf-8">
5
		<title>three.js examples</title>
M
Mr.doob 已提交
6
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
M
Mr.doob 已提交
7 8 9 10
		<link rel="shortcut icon" href="../files/favicon.ico" />
		<link rel="stylesheet" type="text/css" href="../files/main.css">
	</head>
	<body>
M
Mugen87 已提交
11

12
		<div id="panel">
M
Mr.doob 已提交
13

M
Mr.doob 已提交
14
			<div id="header">
M
Mugen87 已提交
15
				<h1><a href="https://threejs.org">three.js</a></h1>
M
Mr.doob 已提交
16

M
Mr.doob 已提交
17
				<div id="sections">
18
					<a href="../docs/index.html#manual/introduction/Creating-a-scene">docs</a>
Y
Yuin Chien 已提交
19
					<span class="selected">examples</span>
M
Mr.doob 已提交
20
				</div>
M
Mr.doob 已提交
21

22
				<div id="expandButton"></div>
M
Mugen87 已提交
23
			</div>
M
Mr.doob 已提交
24

Y
Yuin Chien 已提交
25 26 27
			<div id="panelScrim"></div>

			<div id="contentWrapper">
28 29

				<div id="inputWrapper">
M
munrocket@pm.me 已提交
30
					<input placeholder="" type="text" id="filterInput" autocorrect="off" autocapitalize="off" spellcheck="false" />
31 32
					<div id="exitSearchButton"></div>
				</div>
Y
Yuin Chien 已提交
33

34 35
				<div id="content">
					<img id="previewsToggler" src="./files/thumbnails.svg" width="20" height="20" />
36 37
				</div>
			</div>
M
Mr.doob 已提交
38

M
Mr.doob 已提交
39
		</div>
M
Mr.doob 已提交
40

41
		<iframe id="viewer" name="viewer" allowfullscreen allowvr onmousewheel=""></iframe>
M
Mr.doob 已提交
42

43 44
		<a id="button" target="_blank"><img src="../files/ic_code_black_24dp.svg"></a>

M
Mr.doob 已提交
45 46
		<script>

M
Mugen87 已提交
47 48 49 50 51 52 53 54
		const panel = document.getElementById( 'panel' );
		const content = document.getElementById( 'content' );
		const viewer = document.getElementById( 'viewer' );
		const filterInput = document.getElementById( 'filterInput' );
		const exitSearchButton = document.getElementById( 'exitSearchButton' );
		const expandButton = document.getElementById( 'expandButton' );
		const viewSrcButton = document.getElementById( 'button' );
		const panelScrim = document.getElementById( 'panelScrim' );
55

M
Mugen87 已提交
56
		const previewsToggler = document.getElementById( 'previewsToggler' );
M
Mugen87 已提交
57

M
Mugen87 已提交
58
		const links = {};
M
Mugen87 已提交
59
		const validRedirects = new Map();
M
Mugen87 已提交
60 61 62
		const container = document.createElement( 'div' );

		let selected = null;
M
Mugen87 已提交
63

64 65 66 67 68 69
		Promise.all( [
			fetch( 'files.json' ).then( response => response.json() ),
			fetch( 'tags.json' ).then( response => response.json() )
		] ).then( ( json ) => {

			init( json[ 0 ], json[ 1 ] );
M
Mugen87 已提交
70

71 72 73
		} );

		function init( files, tags ) {
M
munrocket@pm.me 已提交
74 75 76 77 78

			content.appendChild( container );

			viewSrcButton.style.display = 'none';

M
Mugen87 已提交
79
			for ( const key in files ) {
M
munrocket@pm.me 已提交
80

M
Mugen87 已提交
81
				const section = files[ key ];
M
munrocket@pm.me 已提交
82

M
Mugen87 已提交
83
				const header = document.createElement( 'h2' );
M
munrocket@pm.me 已提交
84 85 86 87
				header.textContent = key;
				header.setAttribute( 'data-category', key );
				container.appendChild( header );

M
Mugen87 已提交
88
				for ( let i = 0; i < section.length; i ++ ) {
M
munrocket@pm.me 已提交
89

M
Mugen87 已提交
90
					const file = section[ i ];
M
munrocket@pm.me 已提交
91

M
Mugen87 已提交
92
					const link = createLink( file );
M
munrocket@pm.me 已提交
93 94 95
					container.appendChild( link );

					links[ file ] = link;
M
Mugen87 已提交
96
					validRedirects.set( file, file + '.html' );
M
munrocket@pm.me 已提交
97 98

				}
M
Mugen87 已提交
99

M
Mr.doob 已提交
100
			}
M
Mugen87 已提交
101

M
Mugen87 已提交
102
			if ( window.location.hash !== '' ) {
M
Mugen87 已提交
103

M
Mugen87 已提交
104 105 106 107 108 109 110 111 112 113
				const file = window.location.hash.substring( 1 );

				// use a predefined map of redirects to avoid untrusted URL redirection due to user-provided value

				if ( validRedirects.has( file ) === true ) {

					selectFile( file );
					viewer.src = validRedirects.get( file );

				}
114

M
munrocket@pm.me 已提交
115
			}
M
Mr.doob 已提交
116

M
munrocket@pm.me 已提交
117
			filterInput.value = extractQuery();
M
Mugen87 已提交
118

M
munrocket@pm.me 已提交
119
			if ( filterInput.value !== '' ) {
M
Mugen87 已提交
120

M
munrocket@pm.me 已提交
121
				panel.classList.add( 'searchFocused' );
M
Mugen87 已提交
122

M
munrocket@pm.me 已提交
123
			}
M
Mr.doob 已提交
124

125
			updateFilter( files, tags );
Y
Yuin Chien 已提交
126

M
munrocket@pm.me 已提交
127
			// Events
Y
Yuin Chien 已提交
128

129
			filterInput.onfocus = function ( ) {
Y
Yuin Chien 已提交
130

M
munrocket@pm.me 已提交
131
				panel.classList.add( 'searchFocused' );
M
Mr.doob 已提交
132

M
munrocket@pm.me 已提交
133
			};
134

135
			filterInput.onblur = function ( ) {
136

M
munrocket@pm.me 已提交
137
				if ( filterInput.value === '' ) {
138

M
munrocket@pm.me 已提交
139
					panel.classList.remove( 'searchFocused' );
M
Mr.doob 已提交
140

M
munrocket@pm.me 已提交
141
				}
142

M
munrocket@pm.me 已提交
143
			};
M
Mr.doob 已提交
144

145
			exitSearchButton.onclick = function ( ) {
M
Mr.doob 已提交
146

M
munrocket@pm.me 已提交
147
				filterInput.value = '';
148
				updateFilter( files, tags );
M
munrocket@pm.me 已提交
149
				panel.classList.remove( 'searchFocused' );
M
Mr.doob 已提交
150

M
munrocket@pm.me 已提交
151
			};
M
Mr.doob 已提交
152

M
munrocket@pm.me 已提交
153 154
			filterInput.addEventListener( 'input', function () {

155
				updateFilter( files, tags );
M
Mr.doob 已提交
156 157 158 159

			} );


M
munrocket@pm.me 已提交
160
			expandButton.addEventListener( 'click', function ( event ) {
M
Mr.doob 已提交
161

M
munrocket@pm.me 已提交
162 163
				event.preventDefault();
				panel.classList.toggle( 'open' );
M
Mr.doob 已提交
164

M
munrocket@pm.me 已提交
165
			} );
M
Mr.doob 已提交
166

M
munrocket@pm.me 已提交
167
			panelScrim.onclick = function ( event ) {
M
Mr.doob 已提交
168

M
munrocket@pm.me 已提交
169 170
				event.preventDefault();
				panel.classList.toggle( 'open' );
M
Mr.doob 已提交
171

M
munrocket@pm.me 已提交
172
			};
M
Mr.doob 已提交
173

174
			previewsToggler.onclick = function ( event ) {
175 176

				event.preventDefault();
177
				content.classList.toggle( 'minimal' );
178 179 180

			};

M
munrocket@pm.me 已提交
181
			// iOS iframe auto-resize workaround
M
Mr.doob 已提交
182

M
munrocket@pm.me 已提交
183 184 185 186 187
			if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {

				viewer.style.width = getComputedStyle( viewer ).width;
				viewer.style.height = getComputedStyle( viewer ).height;
				viewer.setAttribute( 'scrolling', 'no' );
M
Mr.doob 已提交
188 189

			}
M
Mr.doob 已提交
190 191 192

		}

M
munrocket@pm.me 已提交
193 194
		function createLink( file ) {

M
Mugen87 已提交
195
			const template = [
196 197 198
				'<div class="card">',
				'	<a href="' + file + '.html" target="viewer">',
				'		<div class="cover">',
M
munrocket@pm.me 已提交
199
				'			<img src="screenshots/' + file + '.jpg" loading="lazy" width="400" />',
200 201 202 203 204 205
				'		</div>',
				'		<div class="title">' + getName( file ) + '</div>',
				'	</a>',
				'</div>'
			].join( "\n" );

M
Mugen87 已提交
206
			const link = createElementFromHTML( template );
207 208

			link.querySelector( 'a[target="viewer"]' ).addEventListener( 'click', function ( event ) {
M
munrocket@pm.me 已提交
209 210 211 212 213 214 215 216 217 218 219

				if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;

				selectFile( file );

			} );

			return link;

		}

M
Mr.doob 已提交
220
		function selectFile( file ) {
221

222
			if ( selected !== null ) links[ selected ].classList.remove( 'selected' );
M
Mr.doob 已提交
223

224
			links[ file ].classList.add( 'selected' );
M
Mr.doob 已提交
225 226

			window.location.hash = file;
J
Jaume Sanchez 已提交
227
			viewer.focus();
M
Mr.doob 已提交
228

Y
Yuin Chien 已提交
229
			panel.classList.remove( 'open' );
M
Mr.doob 已提交
230

M
Mr.doob 已提交
231 232
			selected = file;

233 234 235
			// Reveal "View source" button and set attributes to this example
			viewSrcButton.style.display = '';
			viewSrcButton.href = 'https://github.com/mrdoob/three.js/blob/master/examples/' + selected + '.html';
M
Mr.doob 已提交
236
			viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitHub';
237

238
		}
M
Mr.doob 已提交
239

240
		function updateFilter( files, tags ) {
M
Mugen87 已提交
241

M
Mugen87 已提交
242
			let v = filterInput.value.trim();
M
Mugen87 已提交
243
			v = v.replace( /\s+/gi, ' ' ); // replace multiple whitespaces with a single one
M
Mugen87 已提交
244 245 246 247 248

			if ( v !== '' ) {

				window.history.replaceState( {}, '', '?q=' + v + window.location.hash );

249
			} else {
M
Mugen87 已提交
250 251 252

				window.history.replaceState( {}, '', window.location.pathname + window.location.hash );

253 254
			}

M
Mugen87 已提交
255
			const exp = new RegExp( v, 'gi' );
M
Mugen87 已提交
256

M
Mugen87 已提交
257
			for ( const key in files ) {
M
Mugen87 已提交
258

M
Mugen87 已提交
259
				const section = files[ key ];
M
Mugen87 已提交
260

M
Mugen87 已提交
261
				for ( let i = 0; i < section.length; i ++ ) {
M
Mugen87 已提交
262

263
					filterExample( section[ i ], exp, tags );
M
Mugen87 已提交
264 265 266 267 268

				}

			}

269
			layoutList( files );
M
Mugen87 已提交
270 271 272

		}

273
		function filterExample( file, exp, tags ) {
M
Mugen87 已提交
274

M
Mugen87 已提交
275 276
			const link = links[ file ];
			const name = getName( file );
M
Mugen87 已提交
277
			if ( file in tags ) file += ' ' + tags[ file ].join( ' ' );
M
Mugen87 已提交
278 279
			const res = file.match( exp );
			let text;
M
Mugen87 已提交
280 281 282

			if ( res && res.length > 0 ) {

M
Mr.doob 已提交
283
				link.classList.remove( 'hidden' );
M
Mugen87 已提交
284

M
Mugen87 已提交
285
				for ( let i = 0; i < res.length; i ++ ) {
M
Mugen87 已提交
286

M
Mugen87 已提交
287
					text = name.replace( res[ i ], '<b>' + res[ i ] + '</b>' );
M
Mugen87 已提交
288

M
Mugen87 已提交
289 290
				}

291
				link.querySelector( '.title' ).innerHTML = text;
M
Mugen87 已提交
292 293 294

			} else {

M
Mr.doob 已提交
295
				link.classList.add( 'hidden' );
296
				link.querySelector( '.title' ).innerHTML = name;
M
Mugen87 已提交
297 298

			}
M
Mugen87 已提交
299

M
Mugen87 已提交
300 301 302 303
		}

		function getName( file ) {

M
Mugen87 已提交
304
			const name = file.split( '_' );
M
Mugen87 已提交
305
			name.shift();
M
Mugen87 已提交
306
			return name.join( ' / ' );
M
Mugen87 已提交
307 308 309

		}

310
		function layoutList( files ) {
M
Mugen87 已提交
311

M
Mugen87 已提交
312
			for ( const key in files ) {
M
Mugen87 已提交
313

M
Mugen87 已提交
314
				let collapsed = true;
M
Mugen87 已提交
315

M
Mugen87 已提交
316
				const section = files[ key ];
M
Mugen87 已提交
317

M
Mugen87 已提交
318
				for ( let i = 0; i < section.length; i ++ ) {
M
Mugen87 已提交
319

M
Mugen87 已提交
320
					const file = section[ i ];
M
Mugen87 已提交
321

M
Mugen87 已提交
322
					if ( links[ file ].classList.contains( 'hidden' ) === false ) {
M
Mugen87 已提交
323 324 325 326 327 328 329 330

						collapsed = false;
						break;

					}

				}

M
Mugen87 已提交
331
				const element = document.querySelector( 'h2[data-category="' + key + '"]' );
M
Mugen87 已提交
332

M
Mr.doob 已提交
333
				if ( collapsed ) {
M
Mugen87 已提交
334

M
Mr.doob 已提交
335
					element.classList.add( 'hidden' );
M
Mugen87 已提交
336 337 338

				} else {

M
Mr.doob 已提交
339
					element.classList.remove( 'hidden' );
M
Mugen87 已提交
340 341 342 343 344 345 346

				}

			}

		}

M
munrocket@pm.me 已提交
347 348
		function extractQuery() {

M
Mugen87 已提交
349
			const p = window.location.search.indexOf( '?q=' );
M
munrocket@pm.me 已提交
350 351

			if ( p !== - 1 ) {
M
Mr.doob 已提交
352

M
Mugen87 已提交
353
				return decodeURI( window.location.search.substr( 3 ) );
M
Mr.doob 已提交
354

M
munrocket@pm.me 已提交
355
			}
M
Mr.doob 已提交
356

M
munrocket@pm.me 已提交
357
			return '';
M
Mr.doob 已提交
358

M
munrocket@pm.me 已提交
359
		}
360

361 362 363

		function createElementFromHTML( htmlString ) {

M
Mugen87 已提交
364
			const div = document.createElement( 'div' );
365 366 367 368 369
			div.innerHTML = htmlString.trim();
			return div.firstChild;

		}

M
Mr.doob 已提交
370 371 372
		</script>

	</body>
373
</html>