-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
95 lines (84 loc) · 3.07 KB
/
index.html
File metadata and controls
95 lines (84 loc) · 3.07 KB
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
<input type='file' onchange="readURL(this);" />
<label for='quality'>Quality</label>
<input id='quality' type='number' value=1 min=0.01 max=1 />
<label for='iters'>resize iterations</label>
<input id='iters' type='number' value="5" />
<label for='width'>output width</label>
<input id='width' type='number' value="1600" />
<select id='type'>
<option>image/jpeg</option>
<option>image/webp</option>
</select>
<button onclick="compress()">compress!</button>
<span id='results'></span>
<h3>uncompressed</h3>
<canvas id="myCanvas"></canvas>
<h3>compressed</h3>
<img id="compressed"/>
<script>
let ready = false
const inputImg = new Image();
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.addEventListener(
"load",
function() {
var src = reader.result;
inputImg.src = src;
inputImg.onload = function() {
ready = true;
};
},
false
);
reader.readAsDataURL(input.files[0]);
}
}
const getSteps = (steps, original, target) => {
const stepSize = Math.floor((original - target) / steps);
const result = new Array(steps).fill(1)
let prevStep = original
for (let i=0; i<steps; i++) {
result[i] = prevStep - stepSize
prevStep = result[i]
}
result[result.length -1] = parseInt(target);
return result;
}
function compress() {
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.canvas.width = inputImg.width
ctx.canvas.height = inputImg.height
ctx.drawImage(inputImg, 0, 0);
const uncompressed = c.toDataURL();
const kbytesBefore = uncompressed.length / 1000
ctx.clearRect(0, 0, c.width, c.height);
const targetWidth = document.getElementById('width').value;
const numSteps = document.getElementById('iters').value;
const ratio = (inputImg.width / inputImg.height)
const start = Date.now()
const steps = getSteps(numSteps, inputImg.width, targetWidth)
const can2 = document.createElement('canvas');
const ctx2 = can2.getContext('2d')
can2.width = inputImg.width
can2.height = inputImg.height
for (const [idx, step] of steps.entries()) {
const width = step
const height = step / ratio
idx == 0 ? ctx2.drawImage(inputImg, 0, 0, width, height)
: ctx2.drawImage(can2, 0, 0, steps[idx -1], steps[idx-1] /ratio, 0, 0, width, height)
console.log(idx, width, height)
}
ctx.canvas.width = targetWidth
ctx.canvas.height = targetWidth / ratio
ctx.drawImage(can2, 0, 0, targetWidth, targetWidth / ratio,0, 0, targetWidth, targetWidth / ratio);
const quality = document.getElementById('quality').value;
const type = document.getElementById('type').value;
const compressed = c.toDataURL(type, parseFloat(quality))
const kbytesAfter = compressed.length / 1000
document.getElementById('results').innerText = `took: ${Date.now() - start}ms, kbytesBefore: ${kbytesBefore}, kbytesAfter: ${kbytesAfter}`
document.getElementById('compressed').src = compressed
}
</script>