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

canvas-thumbnail-cache

v2.1.1

Published

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

Downloads

11

Readme

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

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");
document.body.appendChild(canvas);

const COUNT = 50;

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

(async () => {
  const items = Array.from({ length: COUNT }, (_, index) => {
    return {
      id: index,
      src: `https://source.unsplash.com/collection/155977/${index}`,
      loader: "Image",
    };
  });

  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.

| Param | Type | Default | | --------- | -------------------------------- | --------------- | | [options] | Options | {} |

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

| Param | Type | Description | | ------ | ------------------------------ | ---------------------------------------------------------------------------------------------------- | | key | string | Slots map key | | source | CanvasImageSource | HTMLImageElement, 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

| Param | Type | | ----- | ------------------- | | key | string |

canvasThumbnailCache.remove(key)

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

Kind: instance method of CanvasThumbnailCache

| Param | Type | | ----- | ------------------- | | key | string |

Slot : Object

Kind: global typedef Properties

| Name | Type | Description | | ---- | ------------------- | -------------------------------- | | x | number | Horizontal position in the grid. | | y | number | Vertical position in the grid. |

Options : Object

Kind: global typedef Properties

| Name | Type | Default | Description | | ---------- | ------------------------------------- | ----------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | | [context] | CanvasRenderingContext2D | createCanvasContext("2d", { offscreen: true }).context | Canvas to render thumbnails too. Will try to get an offscreen canvas by default. | | [size] | number | 2 | Size of the canvas at start: a square with sides of length slotSize * size. | | [slotSize] | number | 64 | Size of the thumbnails. Will be drawn from center of the grid slot. |

License

MIT. See license file.