# canvas-fit

> Small module for fitting a canvas element within the bounds of its parent.

Latest version **1.5.0** (published 2015-08-27) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.0 |
| Published | 2015-08-27 |
| First published | 2014-05-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/canvas-fit) |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 53 |
| Author | Hugh Kennedy |
| Maintainers | hughsk |
| Keywords | canvas, fit, screen, window, browser, stretch, resize |

## Links

- npm: https://www.npmjs.com/package/canvas-fit
- Repository: https://github.com/hughsk/canvas-fit
- Issues: https://github.com/hughsk/canvas-fit/issues
- npm.io page: https://npm.io/package/canvas-fit

## Dependencies (1)

- [element-size](https://npm.io/package/element-size.md) ^1.1.1

## 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.5.0 (latest) — 2015-08-27
- 1.4.0 — 2015-04-14
- 1.3.0 — 2015-04-02
- 1.2.0 — 2014-08-20
- 1.1.2 — 2014-08-02
- 1.1.1 — 2014-08-02
- 1.1.0 — 2014-08-02
- 1.0.0 — 2014-07-07
- 0.0.0 — 2014-05-13

## README

# canvas-fit [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)

Small module for fitting a canvas element within the bounds of its parent.
Useful, for example, for making a canvas fill the screen. Works with SVG
elements too!

## Usage

[![NPM](https://nodei.co/npm/canvas-fit.png)](https://nodei.co/npm/canvas-fit/)

### resize = fit(canvas[, parent[, scale]])

Creates a `resize` function for your `canvas` element. Calling this function
will resize the canvas to fit its parent element.

Here's a simple example to make your canvas update its dimensions when
resizing the window:

``` javascript
var fit = require('canvas-fit')
var canvas = document.createElement('canvas')

window.addEventListener('resize', fit(canvas), false)
```

You might want to override the `parent` element that the canvas should be
fitting within: in which case, pass that element in as your second argument:

``` javascript
window.addEventListener('resize'
  , fit(canvas, window)
  , false
)
```

You can also set the scale of the canvas element relative to its styled size
on the page using the `scale` argument – for example, passing in
`window.devicePixelRatio` here will scale the canvas resolution up on retina
displays.


### `resize.scale = <Number>`

Dynamically change the canvas' target `scale`. Note that you still need to
manually trigger a resize after doing this.


### `resize.parent = <DOMElement>`

Dynamically change the canvas' `parent` element. Note that you still need
to manually trigger a resize after doing this.

### `resize.parent = () => [width, height]`

Instead of filling a given element, explicitly set the width and height
of the canvas. Note that this value will still be scaled up according
to `resize.scale`

``` javascript
resize.parent = function() {
  return [ window.innerWidth - 300, window.innerHeight ]
}
```

## License

MIT. See [LICENSE.md](http://github.com/hughsk/canvas-fit/blob/master/LICENSE.md) for details.

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