-
Notifications
You must be signed in to change notification settings - Fork 39
/
loader.js
58 lines (35 loc) · 1.3 KB
/
loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function activate( target, callback ) {
target = target || document;
target.addEventListener( 'dragover', function ( event ) {
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
} );
target.addEventListener( 'drop', function ( event ) {
event.preventDefault();
var files = event.dataTransfer.files;
if ( files.length > 0 ) {
handleFile( files[ 0 ], callback );
}
} );
}
function loadFile( file, callback ) {
var filename = file.name;
var reader = new FileReader();
reader.addEventListener( 'progress', function ( event ) {
var size = '(' + Math.floor( event.total / 1000 ).toFixed(2) + ' KB)';
var progress = Math.floor( ( event.loaded / event.total ) * 100 ) + '%';
console.log( 'Loading', filename, size, progress );
} );
reader.addEventListener( 'load', ( event ) => {
var contents = event.target.result;
var object = new THREE.OBJLoader().parse( contents );
object.name = filename;
callback( object );
} );
reader.readAsText( file );
}
function handleFile( file, callback ) {
var filename = file.name;
var extension = filename.split( '.' ).pop().toLowerCase()
loadFile( file, callback );
}