index.html 7.6 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>

G
gero3 已提交
45
		<script src="files.js"></script>
46

M
Mr.doob 已提交
47 48
		<script>

M
Mugen87 已提交
49 50 51 52 53 54 55 56
		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' );
57

M
Mugen87 已提交
58
		const previewsToggler = document.getElementById( 'previewsToggler' );
M
Mugen87 已提交
59

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

		let selected = null;
M
Mugen87 已提交
65

M
munrocket@pm.me 已提交
66
		init();
M
Mugen87 已提交
67

M
munrocket@pm.me 已提交
68 69 70 71 72 73
		function init() {

			content.appendChild( container );

			viewSrcButton.style.display = 'none';

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

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

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

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

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

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

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

				}
M
Mugen87 已提交
94

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

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

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

				}
109

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

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

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

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

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

M
munrocket@pm.me 已提交
120
			updateFilter();
Y
Yuin Chien 已提交
121

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

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

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

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

130
			filterInput.onblur = function ( ) {
131

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

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

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

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

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

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

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

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

				updateFilter();
M
Mr.doob 已提交
151 152 153 154

			} );


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

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

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

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

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

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

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

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

			};

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

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

			}
M
Mr.doob 已提交
185 186 187

		}

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

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

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

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

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

				selectFile( file );

			} );

			return link;

		}

M
Mr.doob 已提交
215
		function loadFile( file ) {
M
Mr.doob 已提交
216

217 218 219
			selectFile( file );
			viewer.src = file + '.html';

M
Mr.doob 已提交
220
		}
221

M
Mr.doob 已提交
222
		function selectFile( file ) {
223

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

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

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

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

M
Mr.doob 已提交
233 234
			selected = file;

235 236 237
			// 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 已提交
238
			viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitHub';
239

240
		}
M
Mr.doob 已提交
241

M
Mugen87 已提交
242 243
		function updateFilter() {

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

			if ( v !== '' ) {

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

251
			} else {
M
Mugen87 已提交
252 253 254

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

255 256
			}

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

M
Mugen87 已提交
259
			for ( const key in files ) {
M
Mugen87 已提交
260

M
Mugen87 已提交
261
				const section = files[ key ];
M
Mugen87 已提交
262

M
Mugen87 已提交
263
				for ( let i = 0; i < section.length; i ++ ) {
M
Mugen87 已提交
264 265 266 267 268 269 270 271 272 273 274

					filterExample( section[ i ], exp );

				}

			}

			layoutList();

		}

M
Mugen87 已提交
275
		function filterExample( file, exp ) {
M
Mugen87 已提交
276

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

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

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

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

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

M
Mugen87 已提交
291 292
				}

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

			} else {

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

			}
M
Mugen87 已提交
301

M
Mugen87 已提交
302 303 304 305
		}

		function getName( file ) {

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

		}

		function layoutList() {

M
Mugen87 已提交
314
			for ( const key in files ) {
M
Mugen87 已提交
315

M
Mugen87 已提交
316
				let collapsed = true;
M
Mugen87 已提交
317

M
Mugen87 已提交
318
				const section = files[ key ];
M
Mugen87 已提交
319

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

M
Mugen87 已提交
322
					const file = section[ i ];
M
Mugen87 已提交
323

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

						collapsed = false;
						break;

					}

				}

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

M
Mr.doob 已提交
335
				if ( collapsed ) {
M
Mugen87 已提交
336

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

				} else {

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

				}

			}

		}

M
munrocket@pm.me 已提交
349 350
		function extractQuery() {

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

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

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

M
munrocket@pm.me 已提交
357
			}
M
Mr.doob 已提交
358

M
munrocket@pm.me 已提交
359
			return '';
M
Mr.doob 已提交
360

M
munrocket@pm.me 已提交
361
		}
362

363 364 365

		function createElementFromHTML( htmlString ) {

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

		}

M
Mr.doob 已提交
372 373 374
		</script>

	</body>
375
</html>