-
Notifications
You must be signed in to change notification settings - Fork 1
/
target.js
41 lines (34 loc) · 1.31 KB
/
target.js
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
class Target {
constructor({ delay, targetSize, onTargetHit, aimTrainerEl }) {
this.delay = delay || 1000;
this.targetSize = targetSize || 30;
this.onTargetHit = onTargetHit;
this.aimTrainerEl = aimTrainerEl;
}
start() {
const target = document.createElement("div");
target.style.position = "absolute";
target.style.border = `${this.targetSize/3}px solid black`;
target.style.transition = `all ${(this.delay * 2) / 1000}s`;
target.classList.add("target");
target.style.width = `${this.targetSize}px`;
target.style.height = `${this.targetSize}px`;
let y = Math.floor(this.aimTrainerEl.clientHeight * 0.9 * Math.random());
let x = Math.floor(this.aimTrainerEl.clientWidth * 0.9 * Math.random());
target.style.transform = `translate(${x}px, ${y}px)`;
this.timer = setInterval(() => {
y = Math.floor(
aimTrainer.aimTrainerEl.clientHeight * 0.9 * Math.random()
);
x = Math.floor(aimTrainer.aimTrainerEl.clientWidth * 0.9 * Math.random());
target.style.transform = `translate(${x}px, ${y}px)`;
}, this.delay * 2);
target.addEventListener("click", () => {
target.parentNode.removeChild(target);
clearInterval(this.timer);
this.timer = 0;
this.onTargetHit();
});
this.aimTrainerEl.append(target);
}
}