forked from luogao/canvas-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawLine.html
86 lines (75 loc) · 2.66 KB
/
drawLine.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Canvas学习-画线</title>
<style>
html,
body {
height: 100%;
margin: 0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas">
<p>你的浏览器不支持Canvas</p>
</canvas>
<script>
function canvasApp() {
var canvas = document.getElementById("canvas");
canvas.height = window.innerHeight
canvas.width = window.innerWidth
var ctx = canvas.getContext('2d');
var w = canvas.width;
var h = canvas.height;
function drawScreen() {
var canvasGradient = ctx.createLinearGradient(50, 50, 250, 50)
ctx.lineWidth = 4; //改变线条宽度
ctx.strokeStyle = '#f36'; // 改变线条颜色
ctx.fillStyle = '#f36'; // 改变线条颜色
ctx.beginPath();
ctx.moveTo(50, 10) // 让画笔移动到指定坐标,即起始点
ctx.lineTo(150, 10) // 第二个点(如果是一条直线,即终点)
ctx.lineTo(150, 200)
ctx.lineTo(200, 200)
ctx.lineTo(200, 150)
ctx.fill();
// ctx.stroke(); //沿着绘制路径的坐标点顺序绘制直线
ctx.closePath()
ctx.beginPath();
ctx.moveTo(250, 10) // 让画笔移动到指定坐标,即起始点
ctx.lineTo(350, 10) // 第二个点(如果是一条直线,即终点)
ctx.lineTo(350, 200)
ctx.lineTo(400, 200)
ctx.lineTo(400, 150)
ctx.closePath()
// ctx.stroke(); //沿着绘制路径的坐标点顺序绘制直线
ctx.fill();
ctx.strokeStyle = 'black';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(350, 50);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(50.5, 100.5);
ctx.lineTo(350.5, 100.5);
ctx.closePath();
ctx.stroke();
}
drawScreen()
}
function eventWindowLoaded() {
canvasApp()
}
window.addEventListener('load', eventWindowLoaded, false)
window.addEventListener('resize', eventWindowLoaded, false)
</script>
</body>
</html>