|
1 |
| -import { compress,CompressOptions } from "../src"; |
2 |
| -import {Pane} from 'tweakpane'; |
| 1 | +import CEngine, { type CompressOptions } from "../src"; |
| 2 | +import { Pane } from "tweakpane"; |
3 | 3 |
|
4 | 4 | const pane = new Pane();
|
5 | 5 |
|
6 | 6 | const defaultConfig: CompressOptions = {
|
7 |
| - useWebp: true, |
8 |
| - quality: 0.90, |
| 7 | + useWebp: false, |
| 8 | + quality: 0.9, |
9 | 9 | fileSizeLimit: 30,
|
10 |
| - lenSizeLimit: 8192 |
11 |
| -} |
| 10 | + lenSizeLimit: 8192, |
| 11 | +}; |
12 | 12 |
|
13 | 13 | // 添加一个图片上传按钮
|
14 | 14 | const input = document.createElement("input");
|
15 | 15 | input.type = "file";
|
16 | 16 | input.accept = "image/*";
|
17 | 17 | input.multiple = true;
|
18 | 18 |
|
19 |
| - |
20 |
| -pane.addBinding(defaultConfig, 'useWebp', { label: 'useWebp' }); |
21 |
| -pane.addBinding(defaultConfig, 'quality', { label: 'quality', min: 0, max: 1 }); |
22 |
| -pane.addBinding(defaultConfig, 'fileSizeLimit', { label: 'fileSizeLimit', min: 1, max: 100 }); |
23 |
| -pane.addBinding(defaultConfig, 'lenSizeLimit', { label: 'lenSizeLimit', min: 1000, max: 16383 }); |
24 |
| -pane.addButton({ title: 'upload for compress' }).on('click', () => { |
| 19 | +pane.addBinding(defaultConfig, "useWebp", { label: "useWebp" }); |
| 20 | +pane.addBinding(defaultConfig, "quality", { label: "quality", min: 0, max: 1 }); |
| 21 | +pane.addBinding(defaultConfig, "fileSizeLimit", { label: "fileSizeLimit", min: 1, max: 100 }); |
| 22 | +pane.addBinding(defaultConfig, "lenSizeLimit", { label: "lenSizeLimit", min: 1000, max: 16383 }); |
| 23 | +pane.addButton({ title: "upload for compress" }).on("click", () => { |
25 | 24 | input.click();
|
26 |
| -}) |
27 |
| - |
28 |
| - |
| 25 | +}); |
29 | 26 |
|
30 | 27 | const container = document.createElement("div");
|
31 | 28 | container.style.display = "flex";
|
| 29 | +container.style.flexWrap = "wrap"; |
32 | 30 | document.body.appendChild(container);
|
33 | 31 |
|
34 | 32 | input.onchange = async (): Promise<void> => {
|
35 | 33 | try {
|
36 |
| - console.time("compress"); |
37 | 34 | if (!input.files) return;
|
38 |
| - const blobs = await compress(input.files, defaultConfig); |
39 |
| - console.timeEnd("compress"); |
40 |
| - // 下载 blob |
41 |
| - for (let i = 0; i < blobs.length; i++) { |
42 |
| - const blob = blobs[i]; |
43 |
| - const url = URL.createObjectURL(blob); |
44 |
| - const a = document.createElement("a"); |
45 |
| - a.href = url; |
46 |
| - a.download = `compressed-${i + 1}.${blob.type.split("/")[1]}`; |
47 |
| - a.textContent = `download`; |
48 |
| - const div = document.createElement("div"); |
49 |
| - const img = document.createElement("img"); |
50 |
| - img.src = url; |
51 |
| - img.style.maxWidth = "100px"; |
52 |
| - img.style.maxHeight = "100px"; |
53 |
| - div.appendChild(img); |
54 |
| - div.appendChild(a); |
55 |
| - container.appendChild(div); |
| 35 | + let count = input.files.length; |
| 36 | + console.log("files count", count); |
| 37 | + console.time("compress"); |
| 38 | + // const hash = await crypto.digest("SHA-256", await input.files[0].arrayBuffer()); |
| 39 | + for (let file of input.files) { |
| 40 | + const { size: beforeSize, name } = file; |
| 41 | + const promise = CEngine.runCompress(file, defaultConfig); |
| 42 | + promise.then((blob) => { |
| 43 | + const afterSize = blob.size; |
| 44 | + console.log(name, "beforeSize:", beforeSize >> 20, "afterSize:", afterSize >> 20, "rate:", (afterSize / beforeSize).toFixed(2)); |
| 45 | + |
| 46 | + count--; |
| 47 | + if (count === 0) { |
| 48 | + console.timeEnd("compress"); |
| 49 | + } |
| 50 | + preDownloadForBlobs(blob); |
| 51 | + }); |
56 | 52 | }
|
| 53 | + |
| 54 | + // 下载 blob |
57 | 55 | } catch (error) {
|
58 | 56 | console.error("压缩失败", error);
|
59 | 57 | }
|
60 | 58 | };
|
61 | 59 |
|
62 | 60 | // 页面流畅性检测
|
63 |
| -let time = new Date().getTime(); |
64 |
| -const show = () => { |
65 |
| - const cur = new Date().getTime(); |
66 |
| - const timeDiff = cur - time; |
67 |
| - if (timeDiff > 1000) { |
68 |
| - console.log("time:", timeDiff); |
69 |
| - } |
70 |
| - time = cur; |
71 |
| - requestAnimationFrame(show); |
72 |
| -}; |
73 |
| -show(); |
| 61 | +// let time = new Date().getTime(); |
| 62 | +// const show = () => { |
| 63 | +// const cur = new Date().getTime(); |
| 64 | +// const timeDiff = cur - time; |
| 65 | +// if (timeDiff > 1000) { |
| 66 | +// console.log("time:", timeDiff); |
| 67 | +// } |
| 68 | +// time = cur; |
| 69 | +// requestAnimationFrame(show); |
| 70 | +// }; |
| 71 | +// show(); |
| 72 | + |
| 73 | +function preDownloadForBlobs(blob: Blob, i = 0): void { |
| 74 | + const url = URL.createObjectURL(blob); |
| 75 | + const a = document.createElement("a"); |
| 76 | + a.href = url; |
| 77 | + a.download = `compressed-${i + 1}.${blob.type.split("/")[1]}`; |
| 78 | + a.textContent = `download`; |
| 79 | + |
| 80 | + const div = document.createElement("div"); |
| 81 | + const img = document.createElement("img"); |
| 82 | + img.src = url; |
| 83 | + img.style.maxWidth = "100px"; |
| 84 | + img.style.maxHeight = "100px"; |
| 85 | + |
| 86 | + div.appendChild(img); |
| 87 | + div.appendChild(a); |
| 88 | + container.appendChild(div); |
| 89 | +} |
0 commit comments