-
Notifications
You must be signed in to change notification settings - Fork 8
/
demo.html
167 lines (146 loc) · 5.47 KB
/
demo.html
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html>
<head>
<script src="./dist/blitz.js"></script>
</head>
<body>
<input type="file" capture="camera" accept="image/*" id="cameraInput" name="cameraInput">
<h1>File input test</h1>
<div>
<p>File to Data: </p>
<textarea style="height: 400px; width: 640px;" id="dataOutput"></textarea>
</div>
<div>
<p>Image to Canvas:</p>
<div id="canvasOutput"></div>
</div>
<div>
<p>Data to Image:</p>
<div id="imageOutput"></div>
<div>
<div>
<p>Canvas to Image:</p>
<div id="canvasToImageOutput"></div>
</div>
<script>
function readFile(file) {
var reader = new FileReader();
reader.onload = readSuccess;
async function readSuccess(evt) {
// data to data (async/await style)
var b1 = Blitz.create()
try {
let output = await b1({
source: evt.target.result, // any canvas or image elements (jQuery or native), or File from FileReader
width: 640,
outputFormat: 'jpg', // [optional] jpg, gif, png or raw. when not defined, assumes png.
output: 'data', // [optional] ``data`, `image`, `file (download)` or `canvas`. If not entered output is same as input element.
quality: 0.7, // [optional] applicable for `image`, `file` or `data` output only
logPerformance: true
})
console.log('Resize using event successful')
document.getElementById("dataOutput").innerHTML = output
} catch(err) {
console.log(err)
}
// image -> canvas (promise style)
var img = new Image()
img.src = evt.target.result
var b2 = Blitz.create()
img.onload = function() {
b2({
source: img,
width: 640,
outputFormat: 'jpg',
output: 'canvas',
quality: 0.7,
logPerformance: true
}).then(output => {
console.log('Resize using img to canvas successful')
document.getElementById("canvasOutput").append(output)
})
}
// data -> image (callback style)
var b3 = Object.create(Blitz)
b3.resize({
source: evt.target.result,
width: 640,
outputFormat: 'jpg',
output: 'image',
quality: 0.7,
logPerformance: true
}, function(output) {
console.log('Resize using data successful')
document.getElementById("imageOutput").append(output)
})
// canvas -> image
var img2 = new Image()
img2.src = evt.target.result
var b4 = Object.create(Blitz)
img2.onload = function() {
var canvas = b4._imageToCanvas(img2)
b4.resize({
source: canvas,
width: 640,
outputFormat: 'jpg',
output: 'image',
quality: 0.7,
logPerformance: true
}, function(output) {
console.log('Resize using canvas successful')
document.getElementById("canvasToImageOutput").append(output)
})
}
};
reader.readAsDataURL(file);
}
document.getElementById('cameraInput').onchange = function(e) {
// file -> file
var b5 = Blitz.create()
b5({
source: e.srcElement.files[0],
width: 640,
outputFormat: 'jpg',
quality: 0.7,
logPerformance: true
}).then(file => {
console.log('Resize using file->file successful')
console.log('File object:')
console.log(file instanceof File)
console.log(file)
}).catch(err => {
console.log(err)
})
// file -> download
b5({
source: e.srcElement.files[0],
width: 640,
outputFormat: 'png',
output: 'download',
quality: 0.7,
logPerformance: true
}).then(download => {
console.log('Resize using file->download successful')
download()
}).catch(err => {
console.log(err)
})
//file -> blob
b5({
source: e.srcElement.files[0],
width: 640,
outputFormat: 'jpg',
output: 'blob',
quality: 0.7,
logPerformance: true
}).then(blob => {
console.log('Resize using file->blob successful')
console.log(blob)
}).catch(err => {
console.log(err)
})
readFile(e.srcElement.files[0]);
};
</script>
</body>
</html>