CanvasRenderingContext2D.createPattern()

Nuestros voluntarios aún no han traducido este artículo al Español. ¡Únete a nosotros y ayúdanos a traducirlo!
También puedes leer el artículo en English (US).

The CanvasRenderingContext2D.createPattern() method of the Canvas 2D API creates a pattern using the specified image and repetition. This method returns a CanvasPattern.

This method doesn't draw anything to the canvas directly. The pattern it creates must be assigned to the CanvasRenderingContext2D.fillStyle or CanvasRenderingContext2D.strokeStyle properties, after which it is applied to any subsequent drawing.

Syntax

CanvasPattern ctx.createPattern(image, repetition);

Parameters

image
A CanvasImageSource to be used as the pattern's image. It can be any of the following:
repetition
A DOMString indicating how to repeat the pattern's image. Possible values are:
  • "repeat" (both directions)
  • "repeat-x" (horizontal only)
  • "repeat-y" (vertical only)
  • "no-repeat" (neither direction)
If repetition is specified as an empty string ("") or null (but not undefined), a value of "repeat" will be used.

Return value

CanvasPattern
An opaque object describing a pattern.

Examples

Creating a simple pattern

This example uses the createPattern() method to create a CanvasPattern with a repeating source image. Once created, the pattern is assigned to the canvas context's fill style and applied to a rectangle.

HTML

<canvas id="canvas" width="300" height="300"></canvas>

JavaScript

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

var img = new Image();
img.src = 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png';
img.onload = function() {
  var pattern = ctx.createPattern(img, 'repeat');
  ctx.fillStyle = pattern;
  ctx.fillRect(0, 0, 300, 300);
};

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.createPattern' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support

Gecko-specific notes

  • Starting with Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), specifying a null or undefined image correctly throws a TYPE_MISMATCH_ERR exception.
  • Starting with Gecko 16.0 (Firefox 16.0 / Thunderbird 16.0 / SeaMonkey 2.13), specifying null for the repetition parameter is now allowed and results in the repetition being set to "repeat" (bug 762657).

See also

Etiquetas y colaboradores del documento

Colaboradores en esta página: rams3s, mfluehr, yonatan, fscholz, chrisdavidmills, Nasdav, nmve, erikadoyle
Última actualización por: rams3s,