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
		init();
65

66
		async function init() {
M
munrocket@pm.me 已提交
67 68 69 70 71

			content.appendChild( container );

			viewSrcButton.style.display = 'none';

72 73 74
			const files = await ( await fetch( 'files.json' ) ).json();
			const tags = await ( await fetch( 'tags.json' ) ).json();

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

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

M
Mugen87 已提交
79
				const header = document.createElement( 'h2' );
M
munrocket@pm.me 已提交
80 81 82 83
				header.textContent = key;
				header.setAttribute( 'data-category', key );
				container.appendChild( header );

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

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

M
Mugen87 已提交
88
					const link = createLink( file );
M
munrocket@pm.me 已提交
89 90 91
					container.appendChild( link );

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

				}
M
Mugen87 已提交
95

M
Mr.doob 已提交
96
			}
M
Mugen87 已提交
97

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

M
Mugen87 已提交
100 101 102 103 104 105 106 107 108 109
				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 );

				}
110

M
munrocket@pm.me 已提交
111
			}
M
Mr.doob 已提交
112

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

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

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

M
munrocket@pm.me 已提交
119
			}
M
Mr.doob 已提交
120

121
			updateFilter( files, tags );
Y
Yuin Chien 已提交
122

M
munrocket@pm.me 已提交
123
			// Events
Y
Yuin Chien 已提交
124

125
			filterInput.onfocus = function ( ) {
Y
Yuin Chien 已提交
126

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

M
munrocket@pm.me 已提交
129
			};
130

131
			filterInput.onblur = function ( ) {
132

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

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

M
munrocket@pm.me 已提交
137
				}
138

M
munrocket@pm.me 已提交
139
			};
M
Mr.doob 已提交
140

141
			exitSearchButton.onclick = function ( ) {
M
Mr.doob 已提交
142

M
munrocket@pm.me 已提交
143
				filterInput.value = '';
144
				updateFilter( files, tags );
M
munrocket@pm.me 已提交
145
				panel.classList.remove( 'searchFocused' );
M
Mr.doob 已提交
146

M
munrocket@pm.me 已提交
147
			};
M
Mr.doob 已提交
148

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

151
				updateFilter( files, tags );
M
Mr.doob 已提交
152 153 154 155

			} );


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

M
munrocket@pm.me 已提交
158 159
				event.preventDefault();
				panel.classList.toggle( 'open' );
M
Mr.doob 已提交
160

M
munrocket@pm.me 已提交
161
			} );
M
Mr.doob 已提交
162

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

M
munrocket@pm.me 已提交
165 166
				event.preventDefault();
				panel.classList.toggle( 'open' );
M
Mr.doob 已提交
167

M
munrocket@pm.me 已提交
168
			};
M
Mr.doob 已提交
169

170
			previewsToggler.onclick = function ( event ) {
171 172

				event.preventDefault();
173
				content.classList.toggle( 'minimal' );
174 175 176

			};

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

M
munrocket@pm.me 已提交
179 180 181 182 183
			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 已提交
184 185

			}
M
Mr.doob 已提交
186 187 188

		}

M
munrocket@pm.me 已提交
189 190
		function createLink( file ) {

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

M
Mugen87 已提交
202
			const link = createElementFromHTML( template );
203 204

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

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

				selectFile( file );

			} );

			return link;

		}

M
Mr.doob 已提交
216
		function selectFile( file ) {
217

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

220
			links[ file ].classList.add( 'selected' );
M
Mr.doob 已提交
221 222

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

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

M
Mr.doob 已提交
227 228
			selected = file;

229 230 231
			// 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 已提交
232
			viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitHub';
233

234
		}
M
Mr.doob 已提交
235

236
		function updateFilter( files, tags ) {
M
Mugen87 已提交
237

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

			if ( v !== '' ) {

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

245
			} else {
M
Mugen87 已提交
246 247 248

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

249 250
			}

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

M
Mugen87 已提交
253
			for ( const key in files ) {
M
Mugen87 已提交
254

M
Mugen87 已提交
255
				const section = files[ key ];
M
Mugen87 已提交
256

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

259
					filterExample( section[ i ], exp, tags );
M
Mugen87 已提交
260 261 262 263 264

				}

			}

265
			layoutList( files );
M
Mugen87 已提交
266 267 268

		}

269
		function filterExample( file, exp, tags ) {
M
Mugen87 已提交
270

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

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

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

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

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

M
Mugen87 已提交
285 286
				}

287
				link.querySelector( '.title' ).innerHTML = text;
M
Mugen87 已提交
288 289 290

			} else {

M
Mr.doob 已提交
291
				link.classList.add( 'hidden' );
292
				link.querySelector( '.title' ).innerHTML = name;
M
Mugen87 已提交
293 294

			}
M
Mugen87 已提交
295

M
Mugen87 已提交
296 297 298 299
		}

		function getName( file ) {

M
Mugen87 已提交
300
			const name = file.split( '_' );
M
Mugen87 已提交
301
			name.shift();
M
Mugen87 已提交
302
			return name.join( ' / ' );
M
Mugen87 已提交
303 304 305

		}

306
		function layoutList( files ) {
M
Mugen87 已提交
307

M
Mugen87 已提交
308
			for ( const key in files ) {
M
Mugen87 已提交
309

M
Mugen87 已提交
310
				let collapsed = true;
M
Mugen87 已提交
311

M
Mugen87 已提交
312
				const section = files[ key ];
M
Mugen87 已提交
313

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

M
Mugen87 已提交
316
					const file = section[ i ];
M
Mugen87 已提交
317

M
Mugen87 已提交
318
					if ( links[ file ].classList.contains( 'hidden' ) === false ) {
M
Mugen87 已提交
319 320 321 322 323 324 325 326

						collapsed = false;
						break;

					}

				}

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

M
Mr.doob 已提交
329
				if ( collapsed ) {
M
Mugen87 已提交
330

M
Mr.doob 已提交
331
					element.classList.add( 'hidden' );
M
Mugen87 已提交
332 333 334

				} else {

M
Mr.doob 已提交
335
					element.classList.remove( 'hidden' );
M
Mugen87 已提交
336 337 338 339 340 341 342

				}

			}

		}

M
munrocket@pm.me 已提交
343 344
		function extractQuery() {

M
Mugen87 已提交
345
			const p = window.location.search.indexOf( '?q=' );
M
munrocket@pm.me 已提交
346 347

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

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

M
munrocket@pm.me 已提交
351
			}
M
Mr.doob 已提交
352

M
munrocket@pm.me 已提交
353
			return '';
M
Mr.doob 已提交
354

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

357 358 359

		function createElementFromHTML( htmlString ) {

M
Mugen87 已提交
360
			const div = document.createElement( 'div' );
361 362 363 364 365
			div.innerHTML = htmlString.trim();
			return div.firstChild;

		}

M
Mr.doob 已提交
366 367 368
		</script>

	</body>
369
</html>