-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.html
58 lines (51 loc) · 1.59 KB
/
example.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Image Uploader</title>
<style>
.img-export {
display: block;
}
</style>
</head>
<body>
<label class="img-upload-label">
Upload Image:
<input type="file" class="js-fileinput img-upload" accept="image/jpeg,image/png,image/gif">
</label>
<button class="js-export img-export">Export</button>
<canvas class="js-editorcanvas"></canvas>
<canvas class="js-previewcanvas"></canvas>
<script src="cropper.js"></script>
<script>
function exceptionHandler(message) {
alert(message);
}
// Auto-resize the cropped image
var dimensions = { width: 128, height: 128 };
try {
var uploader = new Uploader({
input: document.querySelector('.js-fileinput'),
types: [ 'gif', 'jpg', 'jpeg', 'png' ]
});
var editor = new Cropper({
size: dimensions,
canvas: document.querySelector('.js-editorcanvas'),
preview: document.querySelector('.js-previewcanvas')
});
// Make sure both were initialised correctly
if (uploader && editor) {
// Start the uploader, which will launch the editor
uploader.listen(editor.setImageSource.bind(editor), (error) => { throw error; });
}
// Allow the result to be exported as an actual image
var img = document.createElement('img');
document.body.appendChild(img);
document.querySelector('.js-export').onclick = (e) => editor.export(img);
} catch (error) {
exceptionHandler(error.message);
}
</script>
</body>
</html>