# init-canvas

> input a dom or domId canvas or canvseDomId return canvas context and width height

Latest version **0.1.1** (published 2022-12-01) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2022-12-01 |
| First published | 2019-12-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | sz-p |
| Maintainers | psz |
| Keywords | init, canvas |

## Links

- npm: https://www.npmjs.com/package/init-canvas
- Repository: https://github.com/sz-p/init-canvas
- Issues: https://github.com/sz-p/init-canvas/issues
- npm.io page: https://npm.io/package/init-canvas

## 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

- 0.1.1 (latest) — 2022-12-01
- 0.1.0 — 2019-12-09

## README

# init-canvas
input a dom or domId  canvas or canvseDomId return canvas context and width height

# Install
```shell
npm install init-canvas
```

# Interface

```typescript
export interface CanvasInfor {
    canvasContext: CanvasRenderingContext2D;
    canvasDom: HTMLCanvasElement;
    width: number;
    height: number;
}
/**
 * input a dom or domId  canvas or canvseDomId return canvas context and width height
 * 
 * @param dom Element or ID of a HTMLDivElement or HTMLCanvasElement object
 * @param width if width is undefined canvas width is container`s width
 * @param height if height is undefined canvas height is container`s height
 */
export default function (dom: HTMLElement | string, width?: number, height?: number): CanvasInfor;
```


# Example

```html
<div id='divDom'></div>
<div id='divDomWithStyle' style={{ width: 200, height: 200 }} ></div>
<canvas id='canvasDom'></canvas>
<canvas id='canvasDomWithStyle' style={{ width: 200, height: 200 }} ></canvas>

<div id='divDomWithStyleGiveDom' ref={divDomRef} style={{ width: 200, height: 200 }} ></div>
<canvas id='canvasDomGiveDom' ref={canvseDomRef} style={{ width: 200, height: 200 }} ></canvas>


<div id='divDomWithSize'></div>
<div id='divDomWithStyleWithSize' style={{ width: 200, height: 200 }} ></div>
<canvas id='canvasDomWithSize'></canvas>
<canvas id='canvasDomWithStyleWithSize' style={{ width: 200, height: 200 }} ></canvas>

<div id='divDomWithStyleGiveDomWithSize' ref={divDomWithSizeRef} style={{ width: 200, height: 200 }} ></div>
<canvas id='canvasDomGiveDomWithSize' ref={canvseDomWithSizeRef} style={{ width: 200, height: 200 }} ></canvas>
```

```javascript
// input a divDom by id
canvasInfor = initCanvas('divDomWithStyle'); 

// canvasInfor {
//  canvasContext: CanvasRenderingContext2D {canvas: canvas, globalAlpha: 1, globalCompositeOperation: "source-over", filter: "none", imageSmoothingEnabled: true, …}
//  canvasDom: canvas
//  height: 200
//  width: 200
// }

// input a canvasDom by id
initCanvas('canvasDomWithStyle');
// input a divDom by dom
initCanvas(divDomRef.current);
// input a canvasDom by dom
initCanvas(canvseDomRef.current);

// with size
initCanvas('divDomWithStyleWithSize', 400, 400);

// canvasInfor {
//  canvasContext: CanvasRenderingContext2D {canvas: canvas, globalAlpha: 1, globalCompositeOperation: "source-over", filter: "none", imageSmoothingEnabled: true, …}
//  canvasDom: canvas
//  height: 400
//  width: 400
// }

initCanvas('canvasDomWithStyleWithSize', 400, 400);
initCanvas(divDomWithSizeRef.current, 400, 400);
initCanvas(canvseDomWithSizeRef.current, 400, 400);         
```

## TODO
[ ] use try catch and log error to replace throw error

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