MaxLaumeister / Pixelated.js

A polyfill for the "image-rendering: pixelated" CSS property
https://www.maxlaumeister.com/software/pixelated-js/
MIT License
8 stars 2 forks source link
css image-rendering image-scaling javascript pixel-art pixelated polyfill

Pixelated.js - an "image-rendering: pixelated" Polyfill

Pixelated.js is a polyfill that makes it easy to display clean, scaled-up pixel art in the browser.

It provides functionality similar to the image-rendering: pixelated CSS property, which is currently supported by webkit browsers (Chrome, Safari, Opera), but not by Edge or Firefox.

Pixelated.js is also a more consistent alternative to using image-rendering: crisp-edges, because it guarantees that images will be scaled using the nearest neighbor algorithm. According to the spec, the scaling algorithm for crisp-edges is undefined, so Pixelated.js makes sure your images look the same across all modern browsers.

Live Demo

Click on the screenshot to see a live demo.

Pixelated.js Screenshot

Scaling With Nearest-Neighbor

Start with one or more scaled-up img tags that you'd like to apply nearest-neighbor scaling to. Add the pixelated attribute:

<img src="https://github.com/MaxLaumeister/Pixelated.js/raw/master/switch.png" style="width: 300px;" pixelated>

Then, include pixelated.js anywhere in your document:

<script src="https://github.com/MaxLaumeister/Pixelated.js/raw/master/pixelated.min.js"></script>

That's it!

Pixelated.js adds the image-rendering: pixelated property if the browser supports it, and if not, Pixelated.js polyfills it by automatically replacing the src of your image with a dynamically-generated scaled-up image.

Scaling With XBR

Pixelated.js optionally supports background-threaded xbr pixel art scaling using XbrWasm, a proprietary library. To get this working, grab a copy of XbrWasm and extract it into the same folder as Pixelated.js. Then, load it before Pixelated.js like this:

<script src="https://github.com/MaxLaumeister/Pixelated.js/raw/master/XbrWasm.js"></script>
<script src="https://github.com/MaxLaumeister/Pixelated.js/raw/master/pixelated.min.js"></script>

Then, on your images, specify the xbr algorithm like this:

<img src="https://github.com/MaxLaumeister/Pixelated.js/raw/master/switch.png" style="width: 300px;" pixelated pixelated-algo="xbr">

Features

Limitations