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.
CanvasPattern ctx.createPattern(image, repetition);
image
CanvasImageSource
to be used as the pattern's image. It can be any of the following:
HTMLImageElement
(
<img>
)
SVGImageElement
(
<image>
)
HTMLVideoElement
(
<video>
, by using the capture of the video)
HTMLCanvasElement
(
<canvas>
)
ImageBitmap
OffscreenCanvas
repetition
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)
repetition
is specified as an empty string (
""
) 或
null
(but not
undefined
), a value of
"repeat"
会被使用。
CanvasPattern
An opaque object describing a pattern.
此范例使用
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.
The original image looks like this:
<canvas id="canvas" width="300" height="300"></canvas>
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);
};
In this example we create a pattern from the contents of an offscreen canvas. We then apply it to the fill style of our primary canvas, and fill that canvas with the pattern.
// Create a pattern, offscreen
const patternCanvas = document.createElement('canvas');
const patternContext = patternCanvas.getContext('2d');
// Give the pattern a width and height of 50
patternCanvas.width = 50;
patternCanvas.height = 50;
// Give the pattern a background color and draw an arc
patternContext.fillStyle = '#fec';
patternContext.fillRect(0, 0, patternCanvas.width, patternCanvas.height);
patternContext.arc(0, 0, 50, 0, .5 * Math.PI);
patternContext.stroke();
// Create our primary canvas and fill it with the pattern
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const pattern = ctx.createPattern(patternCanvas, 'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Add our primary canvas to the webpage
document.body.appendChild(canvas);
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'CanvasRenderingContext2D.createPattern' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
createPattern
|
Chrome Yes | Edge 12 | Firefox 1.5 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
null
or
undefined
image correctly throws a
TYPE_MISMATCH_ERR
异常。
null
为
repetition
parameter is now allowed and results in the repetition being set to
"repeat"
(
bug 762657
).
CanvasRenderingContext2D
CanvasPattern
CanvasRenderingContext2D
addHitRegion()
arc()
arcTo()
beginPath()
bezierCurveTo()
clearHitRegions()
clearRect()
clip()
closePath()
createImageData()
createLinearGradient()
createPattern()
createRadialGradient()
drawFocusIfNeeded()
drawImage()
drawWidgetAsOnScreen()
drawWindow()
ellipse()
fill()
fillRect()
fillText()
getImageData()
getLineDash()
getTransform()
isPointInPath()
isPointInStroke()
lineTo()
measureText()
moveTo()
putImageData()
quadraticCurveTo()
rect()
removeHitRegion()
resetTransform()
restore()
rotate()
save()
scale()
scrollPathIntoView()
setLineDash()
setTransform()
stroke()
strokeRect()
strokeText()
transform()
translate()