Files
squoosh/codecs/mozjpeg_enc/example.html
Jake Archibald 56e10b3aa2 Rollup build
2020-11-19 11:00:23 +00:00

52 lines
1.5 KiB
HTML

<!DOCTYPE html>
<script type="module">
import mozjpeg_enc from './mozjpeg_enc.js';
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);
}
async function main() {
const module = await mozjpeg_enc();
console.log('Version:', module.version().toString(16));
const image = await loadImage('../example.png');
const result = module.encode(image.data, image.width, image.height, {
quality: 75,
baseline: false,
arithmetic: false,
progressive: true,
optimize_coding: true,
smoothing: 0,
color_space: 3,
quant_table: 3,
trellis_multipass: false,
trellis_opt_zero: false,
trellis_opt_table: false,
trellis_loops: 1,
auto_subsample: true,
chroma_subsample: 2,
separate_chroma_quality: false,
chroma_quality: 75,
});
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);
}
main();
</script>