<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图像</title>
<style>
canvas{
border:1px solid red;
}
</style>
<script>
window.onload = function(){
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
// 创建一个圆的路径,裁剪
context.beginPath();
context.arc(150,150,150,0,Math.PI);
context.closePath();
// 裁切
context.clip();
// 绘制图像
// 1.创建图像对象 img节点
var img = new Image();
img.src = '../4.jpg';
img.onload = function(){
// 创建平铺对象
// 参数:img节点,平铺类型
var pattern = context.createPattern(img,'repeat');
// 使用
context.fillStyle = pattern;
context.fillRect(0,0,300,300);
}
}
</script>
</head>
<body>
<canvas id="canvas" width="700px" height="400px"></canvas>
</body>
</html>