2.2.0 • Published 5 months ago

canvas-thumbnail-cache v2.2.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 months ago

canvas-thumbnail-cache

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

Draw images into a canvas square grid for fast retrieval at a thumbnail size.

paypal coinbase twitter

npm.io

Installation

npm install canvas-thumbnail-cache

Usage

import CanvasThumbnailCache from "canvas-thumbnail-cache";
import createCanvasContext from "canvas-context";
import AsyncPreloader from "async-preloader";

const { canvas, context } = createCanvasContext("2d", {
  willReadFrequently: true,
});
document.body.appendChild(canvas);

const COUNT = 50;

const thumbnailsCache = new CanvasThumbnailCache({
  context,
  slotSize: 128,
});

const items = Array.from({ length: COUNT }, (_, index) => {
  let size = [(100 + index * 10) % 200, 200];
  if (index % 2 === 0) size.reverse();
  return {
    id: index,
    src: `https://picsum.photos/${size.join("/")}`,
    loader: "Image",
    body: "blob",
    options: {
      crossOrigin: "anonymous",
    },
  };
});

items.map(async (item) => {
  const image = await AsyncPreloader.loadItem(item);

  thumbnailsCache.add(item.id, image);
});

API

Classes

Typedefs

CanvasThumbnailCache

Kind: global class

new CanvasThumbnailCache(options)

Creates an instance of CanvasThumbnailCache.

ParamTypeDefault
optionsOptions{}

canvasThumbnailCache.slotDrawSize ⇒ number

Retrieve the slot draw size (slot size without padding)

Kind: instance property of CanvasThumbnailCache

canvasThumbnailCache.reset()

Reset and clear the canvas size and empty the thumbnails cache.

Kind: instance method of CanvasThumbnailCache

canvasThumbnailCache.add(key, source) ⇒ Slot

Add an image (or anything that can be draw into a 2D canvas) to the cache and return its slot.

Kind: instance method of CanvasThumbnailCache

ParamTypeDescription
keystringSlots map key
sourceCanvasImageSourceHTMLImageElement, SVGImageElement, HTMLVideoElement, HTMLCanvasElement, ImageBitmap, OffscreenCanvas

canvasThumbnailCache.get(key) ⇒ Slot

Get a slot

The slot can also be retrieved with get and the key passed when calling thumbnailsCache.add(key, source).

Kind: instance method of CanvasThumbnailCache

ParamType
keystring

canvasThumbnailCache.remove(key)

Remove the specified image from the cache and clear its slot.

Kind: instance method of CanvasThumbnailCache

ParamType
keystring

Slot : object

Kind: global typedef Properties

NameTypeDescription
xnumberHorizontal position in the grid.
ynumberVertical position in the grid.

Options : object

Kind: global typedef Properties

NameTypeDefaultDescription
contextCanvasRenderingContext2DcreateCanvasContext("2d", { offscreen: true }).contextCanvas to render thumbnails too. Will try to get an offscreen canvas by default.
sizenumber2Size of the canvas at start: a square with sides of length slotSize * size.
slotSizenumber64Size of the thumbnails. Will be drawn from center of the grid slot.
paddingnumber0Padding around the thumbnails, inside the slots.

License

MIT. See license file.

2.2.0

5 months ago

2.1.3

8 months ago

2.1.2

11 months ago

2.1.1

3 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.0

4 years ago