-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09_canvas_三条折线_状态绘制陷阱.html
52 lines (42 loc) · 1.44 KB
/
09_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.moveTo(100, 100)
ctx.lineTo(200, 150)
ctx.lineTo(100, 200)
ctx.lineWidth = 2
ctx.strokeStyle = 'red'
ctx.stroke()
ctx.moveTo(300, 100) // 使用moveTo移动笔尖(拿起笔将笔尖放到一个新位置)
ctx.lineTo(400, 150) // 使用lineTo画到规定位置(按着笔画到规定位置)
ctx.lineTo(300, 200)
ctx.lineWidth = 3
ctx.strokeStyle = 'green'
ctx.stroke()
ctx.moveTo(500, 100)
ctx.lineTo(600, 150)
ctx.lineTo(500, 200)
ctx.lineWidth = 4
ctx.strokeStyle = 'blue'
ctx.stroke()
// * 最终结果三个折线都是4宽度的蓝色
// * canvas是基于状态绘制的,在每一次实际绘制时会收集所有的状态(lineWidth、strokeStyle等,相同状态时,后定义的会覆盖之前定义的),然后再一起绘制(stroke、fill等)
}
</script>
</body>
</html>