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

dicecube

v0.1.0

Published

a JavaScript module for dicing and shuffling images on HTML canvas

Downloads

68

Readme

Dicecube

a JavaScript module for dicing and shuffling images on HTML canvas

Check out the demo page!

Install

npm i dicecube

Quick Start

<canvas class="dicecube"></canvas>
<script type="module">
import { Dicecube } from "./node_modules/dicecube/dicecube.js";

const canvas = document.querySelector("canvas.dicecube");

const cube = new Dicecube(canvas, {
  src: "https://picsum.photos/640/480?grayscale&random=1",
  gap: 1,
});

cube.init();
</script>

API

Options

const cube = new Dicecube(canvas, {
  src: "https://picsum.photos/640/480?grayscale&random=1",
  width: 420,
  columns: 6,
  gap: 3,
  offsetX: 110,
  offsetY: 30,
});
  • src: (string, default = "") path/url of image to load
  • width: (number, default = 400) target canvas/image width in pixels
  • columns: (number, default = 5) columns/rows for image grid
  • gap: (number, default = 0) size of gaps in pixels
  • offsetX: (number, default = 0) offset image horizontally in pixels
  • offsetY: (number, default = 0) offset image vertically in pixels

Properties

All of the options described above can also be modified dynamically on the Dicecube object.

cube.src = "https://picsum.photos/640/480?grayscale&random=1";
cube.width = 420;
cube.columns = 6;
cube.gap = 3;
cube.offsetX = 110;
cube.offsetY = 30;
  • Dicecube.src: string
  • Dicecube.width: number
  • Dicecube.columns: number
  • Dicecube.gap: number
  • Dicecube.offsetX: number
  • Dicecube.offsetY: number

There are a few properties that are automatically set by Dicecube, but are available to modify directly.

  • Dicecube.positions: (number[]) tracks the positions of each image tile
  • Dicecube.prev: (number) tracks the previously selected tile
  • Dicecube.selected: (number) tracks the currently selected tile

Methods

  • Dicecube.resetPositions(): resets Dicecube.positions to the default unshuffled order

  • Dicecube.updateCanvasSize(): updates the canvas size to account for gaps and tile size

  • Dicecube.loadImage(src): loads image from src string path/url and returns a Promise<void>

  • Dicecube.draw(): draws all of the image tiles to the canvas and updates the canvas size

  • Dicecube.render(): wraps the Dicecube.draw() method in a requestAnimationFrame

  • Dicecube.init(): a method that calls Dicecube.loadImage(Dicecube.src) and after the promise is resolved calls Dicecube.render()

  • Dicecube.clearCell(i): clears the canvas at the position of the number i cell

  • Dicecube.drawCell(i): draws the image tile for the number i cell

  • Dicecube.renderCell(i): wraps the Dicecube.drawCell(i) method in a requestAnimationFrame

  • Dicecube.swap(): swaps the Dicecube.selected cell with one of its neighbors (up, down, left, right) but avoids swapping with Dicecube.prev cell

  • Dicecube.shuffle(): shuffles the Dicecube.positions array