canvas-png-compression
Shim for HTMLCanvasElement.toDataURL()
to include compression for png image format.
This lib provides shim for HTMLCanvasElement.toDataURL()
when image type is 'image/png'
. It adds ability to
provide quality as second parameter to HTMLCanvasElement.toDataURL()
function. Quality is a Number between 0 and 1
indicating image quality if the requested type is 'image/jpeg'
or 'image/webp'
, for the image/png it indicate compression level.
Quality is normalized to compression level of zlib compression from 9 downto 0 (0 => 9, 1 => 0).
How it works
Canvas-png-compression accesses to raw data of canvas using getImageData method of context.
It then pako to apply zlib conversion to the filtered
imageData(one of those filters is used for each row if image Sub, Up, Average, Paeth from
filters spec). Compressed data is then packed with
other chunks into one Uint8Array buffer that represents
png image. It then is converted using window.btoa to base64 string;
#Usage
Install using bower
bower install canvas-png-compression
or npm
npm install canvas-png-compression
Then you'll need to include
<script src="components/canvas-png-compression/dist/bundle.js"></script>
or
<script src="node_modules/canvas-png-compression/dist/bundle.js"></script>
Then you can use CanvasPngCompression.replaceToDataURL();
to replace HTMLCanvasElement.toDataURL()
with canvas-png-compression implementation.
CanvasPngCompression.replaceToDataURL();
If for some reason you need to revert to native implementation, use:
CanvasPngCompression.revertToDataURL();
Passing params to pako
You can pass params to pako's zlib deflate like this:
CanvasPngCompression.replaceToDataURL({
windowBits: 15,
chunkSize: 32 * 1024,
strategy: 3
});
Running demo
Run
node ./server.js
then open http://localhost:3000/demo/demo.html
Acnowledgments
I've got my inspiration from:
Connected issues
https://code.google.com/p/chromium/issues/detail?id=179289
MIT License.