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

png-cartridge

v2.0.0

Published

Use PNG images to save and load data

Downloads

5

Readme

png-cartridge

A JavaScript library that enables you to use PNG images as virtual cartridges to store data.

Installation

npm install --save png-cartridge

If you want to use the library in the browser without a CommonJS packager like browserify you can add it as a script tag:

<script src="node_modules/png-cartridge/dist/bundle.js"></script>

This exports the library as a global variable called cartridge.

Example cartridges

These images contain the source code of the library:

Just plain data, without using a source image:

Plain cartridge without pretty source image

With a source image:

Cartridge created with a source image

Console Usage

Install globally:

npm install -g png-cartridge

Writing data from a JSON file into a raw data image:

cartridge write data.json > data.png

Writing data into a source image:

cartridge write data.json source.png > data.png

Reading data from an image into a JSON file:

cartridge read data.png > data.json

Reading/Writing DOM Images

Creating a cartridge:

var cartridge = require("png-cartridge/browser");

//
// The `save` function creates an Image instance with a data URI as its `src`.
//
var image = cartridge.writeImage({
    foo: "bar"
});

document.body.appendChild(image);

You can also use a source image to beautify the resulting image:

var template = document.querySelector(".source-image");
var image = cartridge.writeImage(data, template);

Loading data from an image works like this:

var source = document.querySelector(".cartridge-image");
var data = cartridge.readImage(source);

console.log(data);

Stream API (Node)

On Node you can use the read and write methods with streams like this:

var fs = require("fs");
var cartridge = require("png-cartridge");

var data = {
    foo: "bar"
};

// Writing plain data image:
cartridge.write(data, onFinish);

// Writing pretty data image using a source image:
cartridge.write(data, fs.createReadStream("source.png"), onFinish);

function onFinish(error, dataStream) {
    if (error) {
        console.error(error);
    }
    else {
        dataStream.pipe(fs.createWriteStream("data.png"));
    }
}

// Reading data from an image:
cartridge.read(fs.createReadStream("data.png"), function (error, data) {
    if (error) {
        console.error(error);
    }
    else {
        console.log(data);
    }
});

How it works

Data is stored in the red, green and blue channels of each pixel with an alpha value of 255. You can either create an ugly data-only image from scratch or supply your own source image.

Supplying your own source image is useful if you need something pretty. Just make sure that all the pixels of your image that shouldn't be used as data have an alpha value of 254 or less. The data appears in the resulting images as dark noise.

Data to be stored in a PNG cartridge is stringified to JSON, compressed using zlib (pako) and then saved as base64 data. Each data pixel stores 3 base64 characters.

Changelog

  • v2.0.0:
    • Adds stream API
    • Adds Node.js support
    • Adds CLI
    • Improves performance