-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.html
110 lines (98 loc) · 2.87 KB
/
main.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>seastianLabs</title>
<style>
* {
padding: 0;
margin: 0;
}
canvas {
background: #eee;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="800" height="600"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var rightPressed = false;
var leftPressed = false;
var upPressed = false;
var downPressed = false;
var L1 = 100;
var L2 = 50;
var A1 = 0;
var A2 = 0;
var Pos1x = L1 * Math.cos(A1);
var Pos1y = L1 * Math.sin(A1);
var Pos2x = L2 * Math.cos(A2);
var Pos2y = L2 * Math.sin(A2);
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
function keyDownHandler(e) {
if (e.keyCode == 39) {
rightPressed = true;
} else if (e.keyCode == 37) {
leftPressed = true;
} else if (e.keyCode == 38) {
upPressed = true;
} else if (e.keyCode == 40) {
downPressed = true;
}
}
function keyUpHandler(e) {
if (e.keyCode == 39) {
rightPressed = false;
} else if (e.keyCode == 37) {
leftPressed = false;
} else if (e.keyCode == 38) {
upPressed = false;
} else if (e.keyCode == 40) {
downPressed = false;
}
}
function drawBrazo1() {
Pos1x = L1 * Math.cos(A1);
Pos1y = L1 * Math.sin(A1);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(Pos1x, Pos1y);
ctx.stroke();
ctx.closePath();
}
function drawBrazo2() {
Pos2x = L2 * Math.cos(A2);
Pos2y = L2 * Math.sin(A2);
ctx.beginPath();
ctx.moveTo(Pos1x, Pos1y);
ctx.lineTo(Pos1x + Pos2x, Pos1y + Pos2y);
ctx.stroke();
ctx.closePath();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBrazo1();
drawBrazo2();
if (rightPressed) {
A1 += 0.01;
}
if (leftPressed) {
A1 -= 0.01;
}
if (upPressed) {
A2 += 0.01;
}
if (downPressed) {
A2 -= 0.01;
}
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>