npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

canvas-png-compression

v0.0.3

Published

Shim for HTMLCanvasElement.toDataURL() to include compression for png image format.

Downloads

6,353

Readme

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({
    /**
        * The windowBits parameter is the base two logarithm of the window size (the size of the history buffer). It should be in the range 8..15 for this version of the library. Larger values of this parameter result in better compression at the expense of memory usage. The default value is 15 if deflateInit is used instead.
        windowBits can also be –8..–15 for raw deflate. In this case, -windowBits determines the window size. deflate() will then generate raw deflate data with no zlib header or trailer, and will not compute an adler32 check value.
        */
    windowBits: 15,
    /**
        * - chunk size used for deflating data chunks, this should be power of 2 and must not be less than 256 and more than 32*1024
        */
    chunkSize: 32 * 1024,
    /**
        * var Z_FILTERED            = 1;
        var Z_HUFFMAN_ONLY        = 2;
        var Z_RLE                 = 3;
        var Z_FIXED               = 4;
        var Z_DEFAULT_STRATEGY    = 0;
        The strategy parameter is used to tune the compression algorithm. Use the value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no string match), or Z_RLE to limit match distances to one (run-length encoding). Filtered data consists mostly of small values with a somewhat random distribution. In this case, the compression algorithm is tuned to compress them better. The effect of Z_FILTERED is to force more Huffman coding and less string matching; it is somewhat intermediate between Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy parameter only affects the compression ratio but not the correctness of the compressed output even if it is not set appropriately. Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler decoder for special applications.
        */
    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.