Canvas Createpattern
π
2026-06-14 | π HTML
# HTML Canvas `createPattern()` Method
The `createPattern()` method of the Canvas 2D API creates a pattern using a specified image, canvas, or video. The pattern can be repeated in specified directions (horizontally, vertically, both, or neither) and can be used to fill or stroke shapes on the canvas.
---
## Syntax
```javascript
context.createPattern(image, repetition);
```
### Parameter Values
| Parameter | Description |
| :--- | :--- |
| `image` | The source element to be used as the pattern. This can be an `HTMLImageElement`, `HTMLCanvasElement`, `HTMLVideoElement`, `SVGImageElement`, or an `ImageBitmap`. |
| `repetition` | A string indicating how to repeat the pattern's image. |
The `repetition` parameter accepts the following values:
| Value | Description |
| :--- | :--- |
| `"repeat"` | **Default**. The pattern repeats both horizontally and vertically. |
| `"repeat-x"` | The pattern repeats horizontally only. |
| `"repeat-y"` | The pattern repeats vertically only. |
| `"no-repeat"` | The pattern is drawn only once (no repetition). |
### Return Value
* **`CanvasPattern`**: An opaque object describing a pattern. You can assign this object to the `fillStyle` or `strokeStyle` properties of the canvas context.
---
## Code Examples
### Example 1: Repeating an Image Horizontally and Vertically
This example demonstrates how to create a pattern that repeats in both directions (`"repeat"`) once the source image has finished loading.
```html
```
---
### Example 2: Using an Offscreen Canvas as a Pattern
You do not have to use external image files; you can also draw a pattern dynamically using another `