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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mtillmann/circlepacker

v0.2.3

Published

simple circle-packing library

Readme

CirclePacker

Simple circle-packing library based on Ken Goulding's gist and Kevin Dorff's png2svgcircles:

  • removes jquery dependency
  • uses modern javascript/typescript
  • provides esm and umd builds
  • helper functions for reading and writing images
  • passes through original colors
  • fix misc issues with original code

Usage

Install: npm install @mtillmann/circlepacker

Typically you'd use one of the from*-functions to create an instance and then use one of the as*-methods to render the circles to the format you need:

import { fromCircle } from "@mtillmann/circlepacker";
document.body.appendChild((await fromCircle(200, 'red')).asSVG());
//or
fromCircle(200, 'red').then((instance) => {
    document.body.appendChild(instance.asSVG());
})

The basic concept is to create an instance of CirclePacker and call the pack method with an ImageData object and the width of the image. The pack method will generate and return a list of packed circles for the given ImageData object.

The CirclePacker provides several helpers that return renderings of the packed circles.

The pack-method always needs the width of the source image as the second argument to work correctly!

ESM Bundler

import { CirclePacker } from "@mtillmann/circlepacker";

const instance = new CirclePacker();
await instance.pack(imageData, imageWidth);

ESM Browser

<script type="module">
    import { CirclePacker } from ".../@mtillmann/circlepacker/dist/index.js";

    const instance = new CirclePacker();
    await instance.pack(imageData, imageWidth);
</script>

UMD Browser

<script src=".../@mtillmann/circlepacker/dist/index.umd.js"></script>
<script>
    //note the namespace
    const instance = new MTCP.CirclePacker();
    await instance.pack(imageData, imageWidth);
</script>

node

When using node, you need to install canvas (npm install canvas@next), then either pass the CanvasRenderingContext2D object to the fromContext2D-function, use the fromCanvas-function or a CirclePacker-instance's pack method.

import { fromCanvas, CirclePacker } from "@mtillmann/circlepacker";
import { createCanvas } from "canvas"

const canvas = createCanvas(200, 200)
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'red'
ctx.fillRect(0, 0, 200, 200)

console.log((await fromCanvas(canvas)).asSVGString());

In node basically only asSVGString and asArray work. To create bitmaps, use asArray to get the circles, then draw them with the canvas library.

Options

Options can be passed to the class constructor and to any of the from* functions as the last argument. The following options are available:

| Option | Type | Default | Description | | --- | --- | --- | --- | | spacing | number | 1 | Spacing of the circles | | numCircles | number | 1000 | Number of circles to draw | | minRadius | number | 1 | Minimum radius of the circles | | maxRadius | number | 10 | Maximum radius of the circles | | higherAccuracy | boolean | false | Use higher accuracy for circle packing | | colors | string\|Array<string> | 'auto' | If 'auto', the input ImageData's colors are used. If an array of colors is given, the given colors are used randomly | | minAlpha | number | 1 | Minimum alpha value of input pixels to be considered. If the alpha value of a pixel is below this value, it is ignored | | useMainThread | boolean | false | Use main thread for circle packing - disables Web Worker where available | | reuseWorker | boolean | true | When set to false, the Worker instances are terminated after use |

ExportOptions

Export options can be passed to most of as* methods. The following options are available:

| Option | Type | Default | Description | | --- | --- | --- | --- | | scale | number | globalThis.devicePixelRatio || 1 | Scale of the output image - only useful for canvas and image renders. Default value prevents blurry images | | quality | number | 1 | Quality float of the desired image format. Only works when Image is rendered. | | format | string | 1 | Desired Format of rendered image |

Input Helpers

All input helpers are async functions that return a CirclePacker instance.

async! fromBlob(blob:Blob, options:Options)

Creates instance from blob data with given options.

async! fromCanvas(canvas:HTMLCanvasElement, options:Options)

Creates instance from canvas element.

async! fromCircle((radius:number, color:string,options:Options)

Draws a circle with given radius and color, then creates instance from it.

async! fromContext2D(ctx:CanvasRenderingContext2D, options:Options)

Creates instance from canvas context.

async! fromImage(image:HTMLImageElement, options:Options)

Creates instance from image element.

async! fromImageData(imageData:ImageData, imageWidth:number, options:Options)

Creates an instance from given image data. Alias for new CirclePacker(imageData, imageWidth, options).render(imageData, imageWidth).

async! fromRect(width:number, height:number, color:string, options:Options)

Draws a rectangle with given width, height and color, then creates instance from it.

async! fromSquare(edgeLength:number = 200, color:string="black", options:Options)

Draws a square with given edge length and color, then creates instance from it.

async! fromURL

Fetches image from given url, then creates instance from it.

Output Helpers

Output helpers exist on the instance and can be called after the pack method has been called.

asCanvas(options:ExportOptions)

Returns a canvas element with the circles drawn on it.

asImageData(options:ExportOptions)

Returns an ImageData object with the circles drawn on it.

async! asBlob(options:ExportOptions)

Returns a blob of the image with the circles drawn on it. Supports Format and Quality options.

async! asBlobURL(options:ExportOptions)

Returns a blob url of the image with the circles drawn on it. Supports Format and Quality options.

asDataURL(options:ExportOptions)

Returns a data url of the image with the circles drawn on it. Supports Format and Quality options.

asSVG(options:ExportOptions)

Returns an SVG node with the circles drawn on it.

asSVGString(options:ExportOptions)

Returns an SVG string with the circles drawn on it.

asArray()

Returns an array of circle objects, basically a getter for the internal placedCircles array.