Skip to content

niutech/jxl.js

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
img
December 17, 2022 12:43
December 17, 2022 13:17
November 15, 2022 05:20
December 17, 2022 12:43
December 17, 2022 12:43
December 17, 2022 12:43
December 17, 2022 12:43
December 14, 2022 01:34
November 15, 2022 05:20
December 17, 2022 12:43

JXL.js

This is a JPEG XL decoder in JavaScript using WebAssembly implementation from the Squoosh app running in Web Worker.

Usage

Just insert your JPEG XL images to your HTML document as usual: <img src="image.jxl" alt="..." width="..." height="..."> and append the minified JXL.js script to the <head> of your web page:

<script src="jxl.min.js"></script>

That's it!

You can enable/disable cache and choose target image type (JPEG/PNG/WebP) by editing config at the beginning of jxl.js file.

How it works

JXL.js uses Mutation Observer to watch for <img> and <picture> tags being added to the DOM as well as CSS background images and it decodes them as they appear using WebAssembly decoder in Web Worker. Then the JPEG XL image data is transcoded into JPEG/PNG/WebP image and cached using Cache API for faster subsequent page views (you can delete cached images using Dev Tools). The transcoding is performed using Offscreen Canvas in Web Worker for jank-free performance, if available.

See the demo

Multithread version

There is an experimental multithread version of JXL.js using 4 threads in Web Workers and WebAssembly SIMD operations, if available, based on the libjxl wasm demo. Requires HTTPS as well as Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy response headers (or the provided Service Worker script as a fallback).

See the multithread demo

Benchmark

Single-thread Multi-thread
test.jxl 790 ms 440 ms
test2.jxl 830 ms 590 ms

Tested on Microsoft Edge 108, average times, no cache.

License

© 2022 Jerzy Głowacki and Squoosh Developers under Apache 2.0 License.