forked from SimplyWenjing/SimplyWenjing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.html
73 lines (69 loc) · 1.96 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>canvas绘图练习</title>
</head>
<body>
<canvas id="drawing" width="900" height="900"></canvas>
<script type="text/javascript" src="../Code/event.js"></script>
<script type="text/javascript">
var drawing = document.getElementById("drawing");
if (drawing.getContext) {
var context = drawing.getContext("2d");
context.strokStyle = "red";
//绘制矩形
context.fillStyle = "#ff0000";
context.fillRect(10,10,50,50)
//绘制半透明蓝色矩形
context.fillStyle = "rgba(0,0,255,0.5)";
context.fillRect(30,30,50,50);
//清除一小部分矩形
context.clearRect(40,40,10,10);
//绘制一个不带数字的时钟表盘
//开始路径
context.beginPath();
//绘制外圆
context.arc(200,200,99,0,2*Math.PI,false);
//绘制内圆
context.moveTo(294,200);
context.arc(200,200,94,0,2*Math.PI,false);
//绘制分针
context.moveTo(200,200);
context.lineTo(200,125);
//绘制时针
context.moveTo(200,200);
context.lineTo(135,200);
context.stroke();
//绘制文本
context.font = "bold 14px Arial";
context.textAlign = "center";
context.textBaseLine = "middle";
context.fillText("12",200,120);
context.fillText("6",200,290);
context.fillText("3",285,205);
context.fillText("9",115,205);
//将原点变换为表盘中心
context.beginPath();
context.arc(500,500,100,0,2*Math.PI,false);
context.moveTo(595,500);
context.arc(500,500,95,0,2*Math.PI,false);
//变换原点
context.translate(500,500);
context.rotate(1);
context.moveTo(0,0);
context.lineTo(0,-85);
context.moveTo(0,0);
context.lineTo(-65,0);
context.stroke();
//绘制渐变矩形
context.translate(100,100)
var gradient = context.createLinearGradient(30,30,50,50);
gradient.addColorStop(0,"white");
gradient.addColorStop(1,"black");
context.fillStyle = gradient;
context.fillRect(30,30,50,50);
};
</script>
</body>
</html>