-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEarthMoonSystem.html
More file actions
75 lines (60 loc) · 1.78 KB
/
EarthMoonSystem.html
File metadata and controls
75 lines (60 loc) · 1.78 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="tutorial" width="300" height="300">
你的浏览器不兼容Canvas,请升级您的浏览器!
</canvas>
<script>
window.onload = () => {
let canvasEL = document.getElementById('tutorial')
if(!canvasEL.getContext) {
return
}
let ctx = canvasEL.getContext('2d')
let sun = new Image()
sun.src = '../images/canvas_sun.png'
let earth = new Image()
earth.src = '../images/canvas_earth.png'
let moon = new Image()
moon.src = '../images/canvas_moon.png'
requestAnimationFrame(draw)
function draw() {
ctx.clearRect(0,0,300,300)
ctx.save()
ctx.save()
ctx.drawImage(sun,0,0)
ctx.translate(150,150)
ctx.strokeStyle = 'rgba(0,153,255,0.4)'
ctx.beginPath()
ctx.arc(0,0,105,0,Math.PI * 2)
ctx.stroke()
ctx.restore()
let time = new Date()
let seconds = time.getSeconds()
let milliseconds = time.getMilliseconds()
ctx.save()
ctx.translate(150,150)
ctx.rotate(Math.PI * 2 / 60 * seconds + Math.PI * 2 / 60 /1000 * milliseconds)
ctx.translate(105,0)
ctx.drawImage(earth,-12,-12)
ctx.save()
ctx.rotate(Math.PI * 2 / 10 * seconds + Math.PI * 2 / 10 / 1000 * milliseconds)
ctx.translate(0,28)
ctx.drawImage(moon,-3.5,-3.5)
ctx.restore()
ctx.save()
ctx.fillStyle = 'rgba(0, 0, 0, 0.4)'
ctx.fillRect(0, -12, 40, 24)
ctx.restore()
ctx.restore()
requestAnimationFrame(draw)
}
}
</script>
</body>
</html>