-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36_canvas_绘制星空_草地.html
128 lines (95 loc) · 2.79 KB
/
36_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!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 = 1200
canvas.height = 800
var ctx = canvas.getContext('2d')
// 绘制背景
var grd = ctx.createLinearGradient(0, 0, 0, 800)
grd.addColorStop(0.0, '#000')
grd.addColorStop(1.0, '#035')
ctx.fillStyle = grd
ctx.fillRect(0, 0, canvas.width, canvas.height)
// 绘制星星
for (var i = 0; i < 200; i++) {
var r = Math.random() * 10 + 10
var x = Math.random() * canvas.width
var y = Math.random() * canvas.height * 0.55
var rot = Math.random() * 360
drawStar(ctx, x, y, r, r / 2, rot)
}
// 绘制月亮
fillMoon(ctx, 2, 900, 200, 100, 30)
// 绘制草地
drawLand(ctx)
}
function drawStar(ctx, x, y, R, r, rot) {
ctx.save()
ctx.translate(x, y)
ctx.rotate(rot / 180 * Math.PI)
ctx.scale(R, R)
starPath(ctx)
ctx.fillStyle = '#fb3'
ctx.fill()
ctx.restore()
}
function starPath(ctx) {
ctx.beginPath()
for (var i = 0; i < 5; i++) {
ctx.lineTo(Math.cos((18 + i * 72) / 180 * Math.PI),
-Math.sin((18 + i * 72) / 180 * Math.PI))
ctx.lineTo(Math.cos((54 + i * 72) / 180 * Math.PI) * 0.5,
-Math.sin((54 + i * 72) / 180 * Math.PI) * 0.5)
}
ctx.closePath()
}
function fillMoon(ctx, d, x, y, R, rot, fillColor) {
ctx.save()
ctx.translate(x, y)
ctx.rotate(rot * Math.PI / 180)
ctx.scale(R, R)
pathMoon(ctx, d)
ctx.fillStyle = fillColor || '#fb3'
ctx.fill()
ctx.restore()
}
function pathMoon(ctx, d) {
ctx.beginPath();
ctx.arc(0, 0, 1, 0.5 * Math.PI, 1.5 * Math.PI, true)
ctx.moveTo(0, -1)
ctx.arcTo(d, 0, 0, 1, dis(0, -1, d, 0) / d)
ctx.closePath()
}
function dis(x1, y1, x2, y2) {
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
}
function drawLand(ctx) {
ctx.save()
ctx.beginPath()
ctx.moveTo(0, 600)
ctx.bezierCurveTo(540, 400, 660, 800, 1200, 600)
ctx.lineTo(1200, 800)
ctx.lineTo(0, 800)
ctx.closePath()
var grd = ctx.createLinearGradient(0, 800, 0, 0)
grd.addColorStop(0.0, '#030')
grd.addColorStop(1.0, '#580')
ctx.fillStyle = grd
ctx.fill()
ctx.restore()
}
</script>
</body>
</html>