-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.html
63 lines (55 loc) · 1.54 KB
/
index.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
<input type="file" accept="image/*" id="file" />
<p>
<label for="maxWidth">Max Width </label>
<input type="number" id="maxWidth" value="320" />
</p>
<p>
<label for="maxHeight">Max Height</label>
<input type="number" id="maxHeight" value="0" />
</p>
<p>
<label for="cropped"> Crop </label>
<input type="checkbox" id="cropped" checked />
</p>
<img id="original" />
<img id="output" />
<button id="submit">Process</button>
<script type="module">
import Compress from "./build/index.js";
const original = document.getElementById("original");
const output = document.getElementById("output");
const isCrop = document.getElementById("cropped");
const maxWidth = document.getElementById("maxWidth");
const maxHeight = document.getElementById("maxHeight");
const submit = document.getElementById("submit");
let state = {};
submit.addEventListener("click", () => {
run();
});
const file = document.getElementById("file");
file.addEventListener(
"change",
async (evt) => {
const file = evt.target.files?.[0];
if (!file) return;
state.file = file;
run();
},
false
);
async function run() {
const file = state.file;
original.src = URL.createObjectURL(file);
const compress = new Compress({
maxWidth: maxWidth.valueAsNumber,
maxHeight: maxHeight.valueAsNumber,
crop: isCrop.checked,
});
try {
const newFile = await compress.compress(file);
output.src = URL.createObjectURL(newFile);
} catch (error) {
console.log(error);
}
}
</script>