-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.html
78 lines (66 loc) · 1.61 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
74
75
76
77
78
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bubbles App</title>
<style>
h1{
font-family: Sans-Serif;
text-align: center;
}
canvas{
border-width: 10px;
border-color: black;
border-style: solid;
display: block;
margin: auto;
}
button{
display: block;
margin: 10px auto;
}
</style>
</head>
<body>
<h1> Bubbles App</h1>
<canvas id="mycanvas" height="600px" width="600px"></canvas>
<button id="hit" onclick="hitButton()">HIT</button>
<button id="reset" onclick="resetButton()">RESET</button>
<script>
var circle = document.getElementById("mycanvas");
var c1 = circle.getContext("2d");
var ax = 580;
var ay = 240;
var interval;
function draw() {
c1.beginPath();
c1.arc(100, 230, 50, 0, 2 * Math.PI);
c1.stroke();
c1.fillStyle =ax < 180 ? "blue" : "red";
c1.fill();
c1.beginPath();
c1.moveTo(ax, ay);
c1.lineTo(ax - 20, 240);
c1.lineWidth = 1.5;
c1.stroke();
}
function moveArrow() {
ax -= 10;
c1.clearRect(0, 0, 600, 600);
draw();
if (ax < 180) {
clearInterval(interval);
}
}
function hitButton() {
interval = setInterval(moveArrow, 100);
}
function resetButton() {
ax = 580;
c1.clearRect(0, 0, 600, 600);
draw();
}
draw();
</script>
</body>
</html>