CanvasRenderingContext2D只提供了2个方法来绘制几何图形:
1.填充矩形区域:
fillRect(double x, double y, double width, double height)
- 填充矩形边框
strokeRect(double x, double y, double width, double height)
其他几何形状(如圆、三角形等)将使用路径的方式绘制。
如示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<canvas id="mc" width="200" height="100" style="border: 1px solid black"></canvas>
<script type="text/javascript">
let canvas = document.getElementById("mc")
let ctx = canvas.getContext("2d")
ctx.fillStyle = "#f00"
ctx.fillRect(10, 20, 70, 60)
ctx.strokeStyle = "#00f"
ctx.lineWidth = 10
ctx.strokeRect(90,20,70,60)
</script>
</body>
</html>