forked from MattStultz/extrusiongen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageFileReader.js
56 lines (49 loc) · 1.65 KB
/
ImageFileReader.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
/**
* This is a simple Javascript image reader that reads image files from the local
* file system and applies it to the bezier canvas handler (or any other component
* that can handle Image objects --- just implement it).
*
*
* Thanks to "Blake Plumb" at
* http://stackoverflow.com/questions/13373834/upload-image-using-javascript
*
* @author Ikaros Kappler
* @date 2014-03-18
* @version 1.0.0
**/
ImageFileReader = {
readBezierBackgroundImage : function() {
var imgFile = document.forms["bezier_background_form"].elements["bezier_background_file"];
ImageFileReader._readImageFile( imgFile,
function(image) {
//alert( "width=" + image.width + ", height=" + image.height );
bezierCanvasHandler.setCustomBackgroundImage(image,true); // redraw=true
}
);
},
_readImageFile : function( imgFile, successCallback ) {
//window.alert( imgFile );
//var imgFile = document.getElementById('submitfile');
if( imgFile.files && imgFile.files[0] ) {
var width;
var height;
var fileSize;
var reader = new FileReader();
reader.onload = function(event) {
var dataUri = event.target.result;
var image = new Image();
image.onload = function( event ) {
//alert( "width=" + this.width + ", height=" + this.height );
successCallback( image );
};
// This works because the file was read as Data-URL
image.src = dataUri;
};
reader.onerror = function(event) {
console.error("File could not be read! Code " + event.target.error.code);
window.alert( "File could not be read! Code " + event.target.error.code );
};
reader.readAsDataURL(imgFile.files[0]);
}
}
};