-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_canvas_三条折线_避免状态绘制陷阱.html
52 lines (43 loc) · 1.23 KB
/
10_canvas_三条折线_避免状态绘制陷阱.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<canvas id="myCanvas" style="border:1px solid red;">
当前浏览器不支持canvas,请更换浏览器后再试
</canvas>
<script>
window.onload = function () {
var canvas = document.getElementById('myCanvas')
canvas.width = 800
canvas.height = 800
var ctx = canvas.getContext('2d')
ctx.beginPath()
ctx.moveTo(100, 100)
ctx.lineTo(200, 150)
ctx.lineTo(100, 200)
ctx.lineWidth = 2
ctx.strokeStyle = 'red'
ctx.stroke()
ctx.beginPath() // 使用beginPath告诉canvas绘制全新的路径,之前的状态不做保留,相当于跟之前的绘图状态做了一个隔离
ctx.moveTo(300, 100)
ctx.lineTo(400, 150)
ctx.lineTo(300, 200)
ctx.lineWidth = 3
ctx.strokeStyle = 'green'
ctx.stroke()
ctx.beginPath()
ctx.moveTo(500, 100)
ctx.lineTo(600, 150)
ctx.lineTo(500, 200)
ctx.lineWidth = 4
ctx.strokeStyle = 'blue'
ctx.stroke()
}
</script>
</body>
</html>