Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Web Workers #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,48 @@ Simply include png.js and zlib.js on your HTML page, create a canvas element, an
</script>

The source code for the browser version resides in `png.js` and also supports loading and displaying animated PNGs.

### Without `canvas`
If you use this from Web Workers where there is no `document` and thus no `canvas`, or if you just don't want to use a canvas you can pass a callback to the second argument:

<script src="zlib.js"></script>
<script src="png.js"></script>
<script>
var canvas = document.getElementsByTagName('canvas')[0];
PNG.load('some.png', function(pnginfo) {
console.log('pnginfo:', pnginfo);
});
</script>

This will log to console:

> {
> animation: null,
> bits: 8,
> colorSpace: "DeviceRGB",
> colorType: 6,
> colors: 3,
> compressionMethod: 0,
> data: Uint8Array[9878],
> filterMethod: 0,
> hasAlphaChannel: true,
> height: 64,
> imgData: Uint8Array[7184],
> interlaceMethod: 0,
> palette: Array[0],
> pixelBitlength: 32,
> pos: 9874,
> text: Object,
> transparency: Object,
> width: 64
> }

If you want to get the pixel data you can do so like this:

var imagedata = new ImageData(pnginfo.width, pnginfo.height);
pnginfo.copyToImageData(imagedata, pnginfo.decodePixels());
var pixels = imagedata.data; // pixels is a 1d array (in rgba order) of decoded pixel data

## Node.js Usage
Install the module using npm

Expand Down
54 changes: 28 additions & 26 deletions png.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.