CanvasRenderingContext2D只提供了2個方法來繪制幾何圖形:
1.填充矩形區(qū)域:
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>