PNGlib

Haxe externs for the pnglib.js library (for saving files from the browser).


Keywords
js
License
MIT
Install
haxelib install PNGlib 1.0.0

Documentation

haxe-PNGlib

License License

Haxe externs for the PNGlib library.

Usage

// create a new PNG image
var p:PNGlib = new PNGlib(width, height, colourDepth);

// access colours in the buffer
p.buffer[p.index(x, y)] = p.color(red, green, blue, alpha);

// output strings describing the file
var outputB64:String = p.getBase64();
var outputPNG:String = p.getDump();

NOTE: You must import the PNGlib library into your HTML file for this to work. In case the source ever goes offline, a copy can be found here: https://github.com/FuzzyWuzzie/haxe-PNGlib/blob/master/libs/pnglib.js

Examples

Drawing Some Circles

var p:PNGlib = new PNGlib(256, 256, 256);
var background:Int = p.color(0, 0, 0, 0);

for(i in 0...256) {
    for(j in 0...256) {
        var dist:Float = Math.sqrt((i - 128)*(i - 128) + (j - 128)*(j - 128));
        if(dist < 32) {
            p.buffer[p.index(i, j)] = p.color(255, 255, 255);
        }
        else if(dist < 64) {
            p.buffer[p.index(i, j)] = p.color(251,199,7);
        }
        else if(dist < 96) {
            p.buffer[p.index(i, j)] = p.color(234, 130, 32);
        }
        else if(dist < 128) {
            p.buffer[p.index(i, j)] = p.color(168,75,56);
        }
        else if(dist < 160) {
            p.buffer[p.index(i, j)] = p.color(20, 20, 25);
        }
    }
}

var base64:String = p.getBase64();
js.Browser.document.write('<img src="data:image/png;base64,'+p.getBase64()+'">');

Results in:

Concentric circles

Live Sample

A live sample is available which generates a PNG image and embeds it in a webpage.