# canvas-cache

> An image cache for canvas.

Latest version **1.0.3** (published 2019-09-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install canvas-cache
pnpm add canvas-cache
yarn add canvas-cache
bun add canvas-cache
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2019-09-04 |
| First published | 2019-08-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Ron Gruesbeck |
| Maintainers | rongruesbeck |
| Keywords | canvas, cache, image, games |

## Links

- npm: https://www.npmjs.com/package/canvas-cache
- Repository: https://github.com/rgruesbeck/canvas-cache
- Homepage: https://github.com/rgruesbeck/canvas-cache#readme
- Issues: https://github.com/rgruesbeck/canvas-cache/issues
- npm.io page: https://npm.io/package/canvas-cache

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2019-09-04
- 1.0.2 — 2019-08-17
- 1.0.1 — 2019-08-17

## README

# Canvas Cache
An image cache for canvas and key-value store for [ImageData](https://developer.mozilla.org/en-US/docs/Web/API/ImageData) objects. Useful for fast draws in browser games with the `putImageData()` method.

## Install
```sh
npm install --save canvas-cache
```

## Use
```js
import canvasCache from 'canvas-cache';

// create cache instance
const cache = canvasCache();

/*
  your game draw method
*/
```

### Methods

#### has()
`cache.has({ key, width, height })`

Returns boolean if image with matching `key`, `width`, and `height` exists.

#### get()
`cache.get({ key, width, height })`

Returns matching `ImageData` object or `undefined`.

#### set()
`cache.set({ image, key, width, height })`

Sets new image and returns a new `ImageData` object.

#### delete()
`cache.delete({ key, width, height })`

Deletes matching image and returns boolean.

#### clear()
`cache.clear()`

Clear cache of all images.

## Example
```js
import canvasCache from 'canvas-cache';

// create cache instance
const cache = canvasCache();

// add image
cache.set({
	image: img,
	key: 'playerImage',
	width: 200,
	height: 200
});

// get image data
let imageData = cache.get({
	key: 'playerImage',
	width: 200,
	height: 200
});

// draw image data
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');

ctx.putImageData(imageData, 0, 0);
```

---
_Source: https://npm.io/package/canvas-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
