返回到:Canvas API:CanvasRenderingContext2D
CanvasRenderingContext2D.createPattern()
是 Canvas 2D API 使用指定的图像 (CanvasImageSource
) 创建模式的方法。它通过 repetition 参数在指定的方向上重复元图像。此方法返回一个CanvasPattern
对象。
语法
CanvasPattern ctx.createPattern(image, repetition);
参数
image
作为重复图像源的 CanvasImageSource
对象。可以是下列之一:
HTMLImageElement
(<img>
),HTMLVideoElement
(<video>
),HTMLCanvasElement
(<canvas>
),CanvasRenderingContext2D
,ImageBitmap
,ImageData
,Blob
.
repetition
DOMString
,指定如何重复图像。允许的值有:
"repeat"
(both directions),"repeat-x"
(horizontal only),"repeat-y"
(vertical only),"no-repeat"
(neither).
如果为空字符串 (''
) 或 null
(但不是 undefined
),repetition 将被当作”repeat”。
返回值
描述模式的不透明对象
示例
使用createPattern
方法
这是一段简单的代码片段,使用 createPattern 方法创建一个指定图像和重复的CanvasPattern
对象。创建完成后,可以使用CanvasPattern.setTransform()
方法对图案进行变形。如示例所示,你可以把此模式赋值给当前的fillStyle
,当你使用fillRect()
方法时,会在 canvas 上绘制出效果。
HTML
<canvas id="canvas"></canvas>
JavaScript
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var img = new Image();
img.src = 'canvas_createpattern.png';
img.onload = function() {
var pattern = ctx.createPattern(img, 'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0,0,400,400);
};
编辑以下代码并在线查看 canvas 变化:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="robots" content="noindex, nofollow"> <style> body { padding: 0; margin: 0; } svg:not(:root) { display: block; } .playable-code { background-color: #f4f7f8; border: none; border-left: 6px solid #558abb; border-width: medium medium medium 6px; color: #4d4e53; height: 100px; width: 90%; padding: 10px 10px 0; } .playable-canvas { border: 1px solid #4d4e53; border-radius: 2px; } .playable-buttons { text-align: right; width: 90%; padding: 5px 10px 5px 26px; } </style> <title>CanvasRenderingContext2D.createPattern() | Web176教程www.web176.com</title> </head> <body> <canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> <div class="playable-buttons"> <input id="edit" type="button" value="Edit" /> <input id="reset" type="button" value="Reset" /> </div> <textarea id="code" class="playable-code" style="height:120px"> var img = new Image(); img.src = '/wp-content/uploads/2022/12/canvas.png'; img.onload = function() { var pattern = ctx.createPattern(img, 'repeat'); ctx.fillStyle = pattern; ctx.fillRect(0,0,400,400); };</textarea> <script> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var textarea = document.getElementById("code"); var reset = document.getElementById("reset"); var edit = document.getElementById("edit"); var code = textarea.value; var img = new Image(); img.src = '/wp-content/uploads/2022/12/canvas.png'; img.onload = function() { var pattern = ctx.createPattern(img, 'repeat'); ctx.fillStyle = pattern; ctx.fillRect(0,0,400,400); }; function drawCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); eval(textarea.value); } reset.addEventListener("click", function() { textarea.value = code; drawCanvas(); }); edit.addEventListener("click", function() { textarea.focus(); }) textarea.addEventListener("input", drawCanvas); window.addEventListener("load", drawCanvas); </script> </body> </html>
返回到:Canvas API:CanvasRenderingContext2D
作者:terry,如若转载,请注明出处:https://www.web176.com/canvas_api/7988.html
评论列表(1条)
不错,刚好用到canvas的项目。