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.
Only the tile grid scales — the fill rectangle size stays the same.
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.
Compatibility
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 ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetAll modern
Full support
Internet Explorer9+
createPattern yes
createPatternWidely supported
Bottom line: Safe for production tiled fills and strokes on Canvas 2D. Use setTransform freely in modern browsers.
Remember
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.
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.