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 🙏

© 2025 – Pkg Stats / Ryan Hefner

png-hit-map

v1.0.0

Published

Efficient hit detection for transparent PNG images using compact binary hit maps

Readme

PNG Hit Map

A lightweight JavaScript library for efficient hit detection in transparent PNG images. Create compact binary hit maps that allow for fast point-in-shape testing without analyzing the full image at runtime.

Installation

npm install png-hit-map

Key Features

  • Pure Binary Format: Extremely compact binary data representation with minimal overhead
  • Run-Length Encoding (RLE): Specialized for binary data with alternating run values
  • Variable-Length Encoding: Short runs take just 1 byte, long runs (≥255) take 5 bytes
  • Maximum Efficiency: Approaches the theoretical minimum size

Basic Usage

import { createHitMap, testHit } from 'png-hit-map';

// Create a hit map from an image
const imgElement = document.getElementById('myImage');
const hitMapData = await createHitMap(imgElement);

// Store image dimensions with the hit map
const imageInfo = { 
  width: imgElement.width, 
  height: imgElement.height 
};

// Test if a point hits a non-transparent part (coordinates as percentages)
const isHit = testHit(hitMapData, 25, 50, imageInfo); // Test at 25% from left, 50% from top

API Reference

Core Functions

createHitMap(image, options)

Creates a binary hit map from a PNG image.

  • Parameters:
    • image: HTMLImageElement or URL string
    • options: (Optional) Configuration object
      • gridPercent: Grid cell size as percentage (default: 1)
      • base64Output: Whether to output as base64 (default: true)
  • Returns: Promise resolving to hit map data (ArrayBuffer or base64 string)

testHit(hitMapData, x, y, imageInfo)

Tests if a point hits a non-transparent part.

  • Parameters:
    • hitMapData: Hit map as ArrayBuffer or base64 string
    • x, y: Coordinates as percentages (0-100) of image dimensions
    • imageInfo: Object with width and height of the original image
  • Returns: Boolean indicating whether the point hits a non-transparent part

Visualization Tools

The package also includes optional visualization tools (not required for production):

import { visualTools } from 'png-hit-map';

// Visualize the hit map on a canvas
visualTools.visualizeGrid(canvasElement, hitMapData, imageInfo, {
  showGrid: true,
  fillColor: 'rgba(0, 255, 0, 0.2)'
});

How It Works

  1. Grid Creation: The image is divided into a grid, with each cell marked as 0 (transparent) or 1 (has non-transparent pixels)
  2. Run-Length Encoding: The grid is compressed as a sequence of alternating runs (0→1→0→1...)
  3. Binary Format:
    • First byte: Grid percentage (0-100)
    • Second byte: Starting value (0 or 1)
    • Remaining bytes: Run lengths with variable encoding
  4. Hit Detection: When testing a hit, the system:
    • Converts the click coordinates to a grid cell
    • Determines which run contains the cell
    • Returns whether that run represents a transparent or non-transparent region

Browser Compatibility

The library works in all modern browsers that support:

  • Canvas API
  • ArrayBuffer
  • DataView

Node.js Usage

While the primary use case is in browsers, the testHit function can be used in Node.js environments. The createHitMap function requires a browser environment with Canvas support.

License

MIT License