<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>rect</title>
</head>
<body>
<canvas id="myCanvas" width="500" height="500"> 您的浏览器不支持canvas </canvas>
<script type="text/javascript">
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
// fillRect(x, y, width, height) 绘制已填充的矩形,默认填充色黑色
// x y 是起始坐标(左上角), width height 矩形大小 单位是px
ctx.fillRect(0, 0, 200, 200);
</script>
</body>
</html>
上述运行结果:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>rect</title>
</head>
<body>
<canvas id="myCanvas" width="500" height="500"> 您的浏览器不支持canvas </canvas>
<script type="text/javascript">
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
// strokeRect(x, y, width, height) 绘制矩形(无填充),笔触的默认颜色是黑色
// x y 是起始坐标(左上角), width height 矩形大小 单位是px
ctx.strokeRect(0, 0, 200, 200);
</script>
</body>
</html>
上述运行结果:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>rect</title>
</head>
<body>
<canvas id="myCanvas" width="500" height="500"> 您的浏览器不支持canvas </canvas>
<script type="text/javascript">
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
// fillRect(x, y, width, height) 绘制已填充的矩形,默认填充色黑色
// x y 是起始坐标(左上角), width height 矩形大小 单位是px
ctx.fillRect(0, 0, 200, 200);
// ctx.fillStyle = 'red';
// strokeRect(x, y, width, height) 绘制矩形(无填充),笔触的默认颜色是黑色
// x y 是起始坐标(左上角), width height 矩形大小 单位是px
ctx.strokeRect(0, 0, 200, 200);
</script>
</body>
</html>
上述运行结果: