# crop-img

> Creates a crop region with the given dimensions and alignment, similar to background-size: cover / object-fit: cover

Latest version **1.0.0** (published 2019-02-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install crop-img
pnpm add crop-img
yarn add crop-img
bun add crop-img
```

## 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.0 |
| Published | 2019-02-25 |
| First published | 2019-02-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Nik Rowell |
| Maintainers | nikrowell |
| Keywords | canvas, canvas-2d, canvas-context, crop, image, webgl, webgl-textures |

## Links

- npm: https://www.npmjs.com/package/crop-img
- Repository: https://github.com/nikrowell/crop-img
- Homepage: https://github.com/nikrowell/crop-img#readme
- Issues: https://github.com/nikrowell/crop-img/issues
- npm.io page: https://npm.io/package/crop-img

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2019-02-25

## README

# crop-img

Creates a crop region with the given dimensions and alignment to be used with HTML canvas [`drawImage`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage), similar to `background-size: cover` / `object-fit: cover`.

## Install

```bash
npm install crop-img
```

## Use

```js
import crop from 'crop-img';

const width = window.innerWidth;
const height = window.innerHeight;

const context = document.createElement('canvas').getContext('2d');
context.canvas.width = width;
context.canvas.height = height;
document.body.appendChild(context.canvas);

const img = new Image();
img.crossOrigin = 'anonymous';
img.src = 'https://source.unsplash.com/random/1600x900';
img.onload = onload;

function onload() {
  // returns and object with x, y, width and height properties
  const rect = crop(img, { width, height, align: [0, 0]});
  context.drawImage(img, rect.x, rect.y, rect.width, rect.height);
}
```

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