Package Exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dicecube) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Dicecube
a JavaScript module for dicing and shuffling images on HTML canvas
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 loadwidth
: (number, default = 400) target canvas/image width in pixelscolumns
: (number, default = 5) columns/rows for image gridgap
: (number, default = 0) size of gaps in pixelsoffsetX
: (number, default = 0) offset image horizontally in pixelsoffsetY
: (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
: stringDicecube.width
: numberDicecube.columns
: numberDicecube.gap
: numberDicecube.offsetX
: numberDicecube.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 tileDicecube.prev
: (number) tracks the previously selected tileDicecube.selected
: (number) tracks the currently selected tile
Methods
Dicecube.resetPositions()
: resetsDicecube.positions
to the default unshuffled orderDicecube.updateCanvasSize()
: updates the canvas size to account for gaps and tile sizeDicecube.loadImage(src)
: loads image from src string path/url and returns aPromise<void>
Dicecube.draw()
: draws all of the image tiles to the canvas and updates the canvas sizeDicecube.render()
: wraps theDicecube.draw()
method in arequestAnimationFrame
Dicecube.init()
: a method that callsDicecube.loadImage(Dicecube.src)
and after the promise is resolved callsDicecube.render()
Dicecube.clearCell(i)
: clears the canvas at the position of the numberi
cellDicecube.drawCell(i)
: draws the image tile for the numberi
cellDicecube.renderCell(i)
: wraps theDicecube.drawCell(i)
method in arequestAnimationFrame
Dicecube.swap()
: swaps theDicecube.selected
cell with one of its neighbors (up, down, left, right) but avoids swapping withDicecube.prev
cellDicecube.shuffle()
: shuffles theDicecube.positions
array