-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsMeshLib.js
84 lines (70 loc) · 3.23 KB
/
JsMeshLib.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* JsMeshLib JavaScript Mesh Library v0.1
*
* Copyright 2013, Erik de Bruijn
*
* Parts are based on Bruno Barbieri's JIC.js which is found here:
* https://github.com/brunobar79/J-I-C/
*/
/**
* Create the meshLib object.
* @constructor
*/
var meshLib = {
/**
* Receives a File Object (STL) and returns a new File Object compressed
* @param {Blob} source_obj The source File Object
* @return {Boolean} Returns true if it is an ASCII STL file.
*/
isAsciiSTL: function(source_obj){
// TODO detect type
return false;
},
/**
* Receives a File Object (STL) and returns a new File Object compressed
* @param {Blob} source_obj The source File Object
* @return {Blob} result_obj The compressed STL Object
*/
asciiToBinarySTL: function(source_obj){
// var cvs = document.createElement('canvas');
// cvs.width = source_img_obj.naturalWidth;
// cvs.height = source_img_obj.naturalHeight;
// var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0);
// var newImageData = cvs.toDataURL("image/jpeg", quality);
// var result_obj = new Image();
// result_obj.src = newImageData;
return result_obj;
},
/**
* Receives an Image Object and upload it to the server via ajax
* @param {Image} compressed_obj The Compressed File Object
* @param {String} The server side url to send the POST request
* @param {String} file_input_name The name of the input that the server will receive with the file
* @param {String} filename The name of the file that will be sent to the server
* @param {function} the callback to trigger when the upload is finished.
*/
uploadSTL: function(compressed_obj, upload_url, file_input_name, filename, callback){
//ADD sendAsBinary compatibilty to older browsers
if (XMLHttpRequest.prototype.sendAsBinary === undefined) {
XMLHttpRequest.prototype.sendAsBinary = function(string) {
var bytes = Array.prototype.map.call(string, function(c) {
return c.charCodeAt(0) & 0xff;
});
this.send(new Uint8Array(bytes).buffer);
};
}
var type= 'image/jpeg';// FIXME: change this
var data = cvs.toDataURL(type);// FIXME: we're not dealing with a canvas...
data = data.replace('data:' + type + ';base64,', '');
var xhr = new XMLHttpRequest();
xhr.open('POST', upload_url, true);
var boundary = 'someboundary';
xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
xhr.sendAsBinary(['--' + boundary, 'Content-Disposition: form-data; name="' + file_input_name + '"; filename="' + filename + '"', 'Content-Type: ' + type, '', atob(data), '--' + boundary + '--'].join('\r\n'));
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status==200) {
callback(this.responseText);
}
};
}
};