HTML Canvas Pattern

Beginner
8 min read
Updated: Nov 2026
5 examples
Live preview

Definition

On an <canvas>, createPattern(image, repetition) builds a repeating paint from an image, another canvas, a video frame, and similar sources. It returns a CanvasPattern. Assign it to fillStyle or strokeStyle, then draw.

Patterns do not draw by themselves. They are styles — like a solid color or a gradient — applied when you fill or stroke.

Baseline · Widely available.

createPattern is a stable Canvas 2D feature across modern browsers since about July 2015.

Null if image not ready.

If an HTMLImageElement is not fully loaded, createPattern returns null. Wait for load, or use an already-painted canvas as the tile source.

Syntax

pattern.js
const pattern = ctx.createPattern(image, repetition);
ctx.fillStyle = pattern;
ctx.fillRect(x, y, w, h);

// repetition: "repeat" | "repeat-x" | "repeat-y" | "no-repeat"

Optional: pattern.setTransform(matrix) scales, rotates, or skews the tile without changing the whole context.

Key Methods

Core pattern tools:

Method / valueDescriptionTry it
createPattern(image, repetition)Build a CanvasPattern from a sourceTry it
"repeat"Tile in both directionsTry it
"repeat-x" / "repeat-y"Tile on one axis onlyTry it
"no-repeat"Draw the tile once (no tiling)Try it
pattern.setTransform(matrix)Transform only the pattern (scale/rotate/skew)Try it
fillStyle / strokeStyleApply the pattern when filling or strokingTry it
Baseline · setTransform (Jan 2020+).

CanvasPattern.setTransform is widely available in modern browsers since about January 2020.

Global Attributes

The <canvas> tag also supports the Global Attributes in HTML.

Live Preview

Switch repetition mode and watch the same tile fill a rectangle.

Tile source is a small offscreen canvas (checker + dot).

Canvas

createPattern(tile, "repeat")

HTML Examples

Short, copy-ready snippets. Examples use an offscreen canvas as the tile so they run without external files. Use Try It Yourself for a live editor.

Example 1 — Tile Fill (repeat)

Paint a small tile canvas, then fill a rectangle with "repeat".

HTML
<canvas id="demo" width="300" height="160" aria-label="Repeating canvas pattern fill"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  const tile = document.createElement("canvas");
  tile.width = 40;
  tile.height = 40;
  const t = tile.getContext("2d");
  t.fillStyle = "#dbeafe";
  t.fillRect(0, 0, 40, 40);
  t.fillStyle = "#2563eb";
  t.fillRect(0, 0, 20, 20);
  t.fillRect(20, 20, 20, 20);

  const pattern = ctx.createPattern(tile, "repeat");
  ctx.fillStyle = pattern;
  ctx.fillRect(20, 20, 260, 120);
</script>
Try It Yourself

How It Works

  1. Make a tile (40×40 checker) on an offscreen canvas.
  2. Create the pattern with createPattern(tile, "repeat").
  3. Assign fillStyle = pattern.
  4. Fill a rectangle — the tile repeats in both directions.

Example 2 — Horizontal Only (repeat-x)

Tiles left-to-right; nothing repeats downward.

HTML
<canvas id="demo" width="300" height="160" aria-label="repeat-x canvas pattern"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  const tile = document.createElement("canvas");
  tile.width = 50;
  tile.height = 40;
  const t = tile.getContext("2d");
  t.fillStyle = "#ecfdf5";
  t.fillRect(0, 0, 50, 40);
  t.fillStyle = "#16a34a";
  t.beginPath();
  t.arc(25, 20, 12, 0, Math.PI * 2);
  t.fill();

  ctx.fillStyle = "#f1f5f9";
  ctx.fillRect(0, 0, 300, 160);

  const pattern = ctx.createPattern(tile, "repeat-x");
  ctx.fillStyle = pattern;
  ctx.fillRect(20, 40, 260, 80);
</script>
Try It Yourself

How It Works

  1. Build a green-dot tile.
  2. Use "repeat-x" so tiles run horizontally only.
  3. Fill a short strip taller than one tile to see the empty band below.
  4. Try "repeat-y" for a vertical column of tiles.

Example 3 — Pattern Stroke

Assign the pattern to strokeStyle for a textured outline.

HTML
<canvas id="demo" width="300" height="160" aria-label="Pattern stroked rectangle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  const tile = document.createElement("canvas");
  tile.width = 16;
  tile.height = 16;
  const t = tile.getContext("2d");
  t.fillStyle = "#ea580c";
  t.fillRect(0, 0, 8, 8);
  t.fillStyle = "#facc15";
  t.fillRect(8, 8, 8, 8);

  const pattern = ctx.createPattern(tile, "repeat");
  ctx.strokeStyle = pattern;
  ctx.lineWidth = 16;
  ctx.strokeRect(40, 30, 220, 100);
</script>
Try It Yourself

How It Works

  1. Create a tiny checker tile.
  2. Set strokeStyle = pattern (not fillStyle).
  3. Use a thick lineWidth so the texture is visible.
  4. Stroke a rectangle (or any path).

Example 4 — Single Tile (no-repeat)

Draw the pattern image once; the rest of the fill is transparent.

HTML
<canvas id="demo" width="300" height="160" aria-label="no-repeat canvas pattern"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  const tile = document.createElement("canvas");
  tile.width = 80;
  tile.height = 60;
  const t = tile.getContext("2d");
  t.fillStyle = "#7c3aed";
  t.fillRect(0, 0, 80, 60);
  t.fillStyle = "#fff";
  t.font = "bold 16px sans-serif";
  t.fillText("TILE", 18, 36);

  ctx.fillStyle = "#e2e8f0";
  ctx.fillRect(0, 0, 300, 160);

  const pattern = ctx.createPattern(tile, "no-repeat");
  ctx.fillStyle = pattern;
  ctx.fillRect(20, 20, 260, 120);
</script>
Try It Yourself

How It Works

  1. Paint a labeled tile (80×60).
  2. Gray background shows where the pattern does not cover.
  3. "no-repeat" places one tile at the pattern origin (top-left of the canvas space).
  4. Use setTransform if you need to move or scale that single tile.

Example 5 — Scale with setTransform

Scale the pattern matrix without transforming the whole context.

HTML
<canvas id="demo" width="300" height="160" aria-label="Pattern setTransform scale"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  const tile = document.createElement("canvas");
  tile.width = 30;
  tile.height = 30;
  const t = tile.getContext("2d");
  t.fillStyle = "#2563eb";
  t.fillRect(0, 0, 15, 15);
  t.fillStyle = "#22d3ee";
  t.fillRect(15, 15, 15, 15);

  const pattern = ctx.createPattern(tile, "repeat");
  pattern.setTransform(new DOMMatrix().scale(1.8));
  ctx.fillStyle = pattern;
  ctx.fillRect(20, 20, 260, 120);
</script>
Try It Yourself

How It Works

  1. Create a small checker pattern as usual.
  2. Call pattern.setTransform(new DOMMatrix().scale(1.8)).
  3. Only the tile grid scales — the fill rectangle size stays the same.
  4. You can also .rotate() on the matrix for diagonal textures.

Edge Cases & Pitfalls

Most pattern bugs are unloaded images, wrong repetition, or confusing context transforms with pattern transforms.

null

Unloaded image → null

Calling createPattern before an Image finishes loading returns null. Guard with if (!pattern) return; and use onload.

cors

Cross-origin tiles

Like other canvas image uses, cross-origin sources without CORS can taint the canvas and block pixel export. Prefer same-origin tiles for learning demos.

origin

Pattern origin is canvas space

Tiles align to the canvas coordinate system (unless you setTransform the pattern). Moving a filled shape does not automatically move the tile phase.

blur

Blurry tiles

Scaling patterns up (via matrix or CSS-sized canvases) softens edges. Draw crisp tiles at the size you need, or toggle imageSmoothingEnabled.

Browser Support

createPattern is part of the Canvas 2D API in every modern browser. CanvasPattern.setTransform is widely available since about January 2020.

✓ Baseline · Widely available

createPattern

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support createPattern for fills and strokes. Internet Explorer 9+ supports createPattern; prefer feature-detect for setTransform in very old browsers.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
createPattern yes
createPattern Widely supported

Bottom line: Safe for production tiled fills and strokes on Canvas 2D. Use setTransform freely in modern browsers.

Key Takeaways

  • Create: createPattern(source, repetition).
  • Apply: set fillStyle or strokeStyle, then draw.
  • Modes: repeat, repeat-x, repeat-y, no-repeat.
  • Ready source: wait for images; canvas tiles work immediately.

One line: tile ready → createPattern → assign style → draw.

Default CSS Settings

Patterns are a Canvas API concern. The <canvas> element still uses typical user-agent CSS:

default.css
canvas {
  display: inline;
}

Set bitmap size with HTML width/height. CSS-only sizing scales the bitmap and can soften pattern tiles.

Frequently Asked Questions

It builds a CanvasPattern from an image, canvas, video frame, or similar source. Assign the pattern to fillStyle or strokeStyle, then draw — createPattern itself does not paint.
"repeat" (both axes), "repeat-x", "repeat-y", and "no-repeat". null or "" are treated like "repeat".
If an HTMLImageElement is not fully loaded, createPattern returns null. Wait for the load event (or use an already-drawn canvas as the source).
Use pattern.setTransform(matrix) on the CanvasPattern, or transform the context (scale/rotate) before filling. Prefer setTransform when you only want the pattern transformed.
Yes. HTMLCanvasElement is a valid source. Paint a small offscreen canvas, then createPattern(thatCanvas, "repeat").
Yes. Assign the CanvasPattern to strokeStyle, set lineWidth, then stroke a path or use strokeRect.

Next: Manipulating Images

Learn drawImage, crop, and pixel filters with ImageData.

Canvas Manipulating Images →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful