https://github.com/dmnsgn/canvas-pattern
Draw and cache a repeated pattern on a canvas context.
https://github.com/dmnsgn/canvas-pattern
cache canvas pattern
Last synced: 8 months ago
JSON representation
Draw and cache a repeated pattern on a canvas context.
- Host: GitHub
- URL: https://github.com/dmnsgn/canvas-pattern
- Owner: dmnsgn
- License: mit
- Created: 2021-11-09T20:49:56.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-07-06T15:23:42.000Z (over 1 year ago)
- Last Synced: 2025-02-03T16:17:28.513Z (9 months ago)
- Topics: cache, canvas, pattern
- Language: JavaScript
- Homepage: https://dmnsgn.github.io/canvas-pattern/
- Size: 1.21 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# canvas-pattern
[](https://www.npmjs.com/package/canvas-pattern)
[](https://www.npmjs.com/package/canvas-pattern)
[](https://bundlephobia.com/package/canvas-pattern)
[](https://github.com/dmnsgn/canvas-pattern/blob/main/package.json)
[](https://github.com/microsoft/TypeScript)
[](https://conventionalcommits.org)
[](https://github.com/prettier/prettier)
[](https://github.com/eslint/eslint)
[](https://github.com/dmnsgn/canvas-pattern/blob/main/LICENSE.md)
Draw and cache a repeated pattern on a canvas context.
[](https://paypal.me/dmnsgn)
[](https://commerce.coinbase.com/checkout/56cbdf28-e323-48d8-9c98-7019e72c97f3)
[](https://twitter.com/dmnsgn)

## Installation
```bash
npm install canvas-pattern
```
## Usage
```js
import canvasPattern from "canvas-pattern";
import createCanvasContext from "canvas-context";
const { canvas, context } = createCanvasContext("2d");
document.body.appendChild(canvas);
const pattern = new Image();
pattern.src = "pattern.png";
canvasPattern(context, pattern);
```
## API
## Modules
## Typedefs
-
CanvasPatternRepetition :"repeat"|"repeat-x"|"repeat-y"|"no-repeat" -
CanvasPatternOptions :object -
Options to draw. All optional.
## canvasPattern
### canvasPattern(context, source, [options]) ⇒ CanvasPattern ⏏
Draw and cache a repeated pattern on a canvas context.
**Kind**: Exported function
| Param | Type | Default | Description |
| --------- | ---------------------------------------------------------- | --------------- | --------------------------------------------------------------- |
| context | CanvasRenderingContext2D | | The context to draw the pattern to |
| source | CanvasImageSource | | The pattern to be drawn |
| [options] | [CanvasPatternOptions](#CanvasPatternOptions) | {} | The options to position the pattern and controls its repetition |
## CanvasPatternRepetition : "repeat" \| "repeat-x" \| "repeat-y" \| "no-repeat"
## CanvasPatternOptions : object
Options to draw. All optional.
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| ------------ | ---------------------------------------------------------------- | -------------------------------------------------------------------------- |
| [pattern] | CanvasPattern | The CanvasPattern to draw. Created and returned by the function for reuse. |
| [repetition] | [CanvasPatternRepetition](#CanvasPatternRepetition) | A DOMString indicating how to repeat the pattern's image. |
| [x] | number | |
| [y] | number | |
| [width] | number | |
| [height] | number | |
## License
MIT. See [license file](https://github.com/dmnsgn/canvas-pattern/blob/main/LICENSE.md).