-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
42 lines (40 loc) · 975 Bytes
/
Copy pathscript.js
File metadata and controls
42 lines (40 loc) · 975 Bytes
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
var disk = document.querySelector('#disk');
var bug = document.querySelector('#bug');
var y;
var x;
var size;
var radius;
var ratio;
var speed = 2;
window.onload = function () {
y = bug.offsetTop;
x = bug.offsetLeft;
size = bug.clientWidth;
radius = disk.clientWidth;
ratio = (1 - (size / radius)**2);
window.onkeydown = function (e) {
switch (e.key) {
case "ArrowDown":
y += speed * ratio;
break;
case "ArrowUp":
y -= speed * ratio;
break;
case "ArrowLeft":
x -= speed * ratio;
break;
case "ArrowRight":
x += speed * ratio;
break;
default:
break;
}
update();
}
}
function update() {
bug.style.top = y + 'px';
bug.style.left = x + 'px';
bug.style.width = size + 'px';
bug.style.height = size + 'px';
}