绘制一个定时从左上角已到右下角的小方块
方法 | 描述 |
ctx.clearRect(x,y,width,height); | 在给定矩形内清空一个矩形,默认白色 |
ctx.fillRect(x,y,width,height); | 绘制“被填充”的矩形,默认黑色 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>canvas4</title>
<script>
window.onload = function () {
var cv = document.getElementById("cv");
var ctx = cv.getContext('2d');
var num = 0;
ctx.fillRect(0, 0, 50, 50)
setInterval(function () {
if (num < 350) {
num++;
ctx.clearRect(0, 0, cv.width, cv.height)
ctx.fillRect(num, num, 50, 50);
}
}, 30)
}
</script>
</head>
<body>
<canvas id="cv" width="400px" height="400px" style="border:1px solid #333;">
不支持canvas的浏览器
</canvas>
</body>
</html>