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

@cdgugler/shatter

v2.0.1

Published

Shatter.js provides simple image shattering by dividing an image (DOM Image element) by an array of coordinates.

Downloads

73

Readme

Shatter.js

Shatter.js provides simple image shattering by dividing an image (DOM Image element) by an array of coordinates.

It also includes a generator, to generate a random set of coordinates based on a Voronoi diagram. The resulting array of images represents a 'shattered' version of the original image.

Install

npm install @cdgugler/shatter

Usage

Shatter

import { Shatter } from 'shatter';
const shattered = new Shatter('/img/square.png');

// Set up an array of 'pieces'
// Each piece is an array of [x, y] coordinates
shattered.setPieces([
    [
        [0, 0],
        [50, 0],
        [50, 100],
        [0, 100],
    ],
    [
        [50, 0],
        [100, 0],
        [100, 100],
        [50, 100],
    ],
]);

// .shatter() returns a Promise due to the
// image.src being asynchronous
let result = await shattered.shatter();

// result is an array of image pieces consisting of
// { image: DOMImageElement, x: xOffset, y: yOffset }
result.forEach((res, i) => {
    container.appendChild(res.image);
});

Examples

To run examples, clone repository and run:

npm install
npm run buildexample
npx http-server examples/

API

Constructor

new Shatter(url) - optional url of image to load. If not set, must use .setImage(image).

Methods

| Name | Description | | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | setImage(image) | Provide a DOM Image element to shatter. Will override any image loaded from constructor. | | setPieces(pieces) | Provide an array of segments/pieces to split the image into. Each 'piece' is an array of [x,y] coordinates. | | shatter() | Split the image using the provided coordinates. Returns a Promise that resolves with an array of 'shattered' objects containing the resulting images and additional data |

Shattered Object

Shatter.shatter() returns an array of objects that contain images and x, y coordinates for each piece.

  • result[i].image - The image segment as a DOM Image element
  • result[i].x - X-offset
  • result[i].y - Y-offset

Generators

You might not want to manually come up with all the pieces with their individual x & y coordinates to shatter an image.

Included is one generator, which uses d3-voronoi behind the scenes to split an image into somewhat random pieces.

To use the generator, call it with an options object consisting of the height and width of the image, and the number of pieces to generate.


import { VoronoiPieces } from 'shatter';

const voropieces = VoronoiPieces({
    height: original.width,
    width: original.height,
    numPieces: num,
});

Then pass the result into the .setPieces() method on Shatter.

Tests

npm run test - run unit tests

npm run e2e - run end to end tests

Live Demo

See my post about the project.

License