forked from external-repos/squoosh
* Returning an object seems to work well * This doesn't work * This does! * Better cast? * Updating usage in Squoosh
45 lines
1.3 KiB
HTML
45 lines
1.3 KiB
HTML
<!doctype html>
|
|
<script src='mozjpeg_enc.js'></script>
|
|
<script>
|
|
const module = mozjpeg_enc();
|
|
|
|
async function loadImage(src) {
|
|
// Load image
|
|
const img = document.createElement('img');
|
|
img.src = src;
|
|
await new Promise(resolve => img.onload = resolve);
|
|
// Make canvas same size as image
|
|
const canvas = document.createElement('canvas');
|
|
[canvas.width, canvas.height] = [img.width, img.height];
|
|
// Draw image onto canvas
|
|
const ctx = canvas.getContext('2d');
|
|
ctx.drawImage(img, 0, 0);
|
|
return ctx.getImageData(0, 0, img.width, img.height);
|
|
}
|
|
|
|
module.onRuntimeInitialized = async _ => {
|
|
console.log('Version:', module.version().toString(16));
|
|
const image = await loadImage('../example.png');
|
|
const result = module.encode(image.data, image.width, image.height, {
|
|
quality: 40,
|
|
baseline: false,
|
|
arithmetic: false,
|
|
progressive: true,
|
|
optimize_coding: true,
|
|
smoothing: 0,
|
|
color_space: 3,
|
|
quant_table: 3,
|
|
trellis_multipass: true,
|
|
trellis_opt_zero: true,
|
|
trellis_opt_table: true,
|
|
trellis_loops: 1,
|
|
});
|
|
|
|
const blob = new Blob([result], {type: 'image/jpeg'});
|
|
const blobURL = URL.createObjectURL(blob);
|
|
const img = document.createElement('img');
|
|
img.src = blobURL;
|
|
document.body.appendChild(img);
|
|
};
|
|
</script>
|