-
Notifications
You must be signed in to change notification settings - Fork 19
/
script.js
140 lines (132 loc) · 3.47 KB
/
script.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* global Vue */
const loopTapApp = Vue.createApp({
data() {
return {
arc: [180, 270],
taps: 0,
score: 0,
best: window.localStorage.best || 0,
state: "init",
prevTapTime: 0,
colors: [
"#ED5565",
"#D9444F",
"#ED5F56",
"#DA4C43",
"#F87D52",
"#E7663F",
"#FAB153",
"#F59B43",
"#FDCE55",
"#F6BA43",
"#C2D568",
"#B1C353",
"#99D469",
"#83C251",
"#42CB70",
"#3CB85D",
"#47CEC0",
"#3BBEB0",
"#4FC2E7",
"#3CB2D9",
"#5C9DED",
"#4C8CDC",
"#9398EC",
"#7277D5",
"#CC93EF",
"#B377D9",
"#ED87BF",
"#D870AE",
],
};
},
computed: {
arcDValue() {
return this.describeArc(50, 50, 40, this.arc[0], this.arc[1]);
},
},
methods: {
polarToCartesian(centerX, centerY, radius, angleInDegrees) {
const angleInRadians = ((angleInDegrees - 90) * Math.PI) / 180.0;
return {
x: centerX + radius * Math.cos(angleInRadians),
y: centerY + radius * Math.sin(angleInRadians),
};
},
describeArc(x, y, radius, startAngle, endAngle) {
const start = this.polarToCartesian(x, y, radius, endAngle);
const end = this.polarToCartesian(x, y, radius, startAngle);
const arcFlag = endAngle - startAngle <= 180 ? "0" : "1";
const d = ["M", start.x, start.y, "A", radius, radius, 0, arcFlag, 0, end.x, end.y].join(" ");
return d;
},
getAngle(cx, cy, ex, ey) {
const dy = ey - cy;
const dx = ex - cx;
let theta = Math.atan2(dx, -dy);
theta *= 180 / Math.PI;
theta = theta < 0 ? theta + 360 : theta;
return theta;
},
getBallAngle() {
const bg = document.getElementById("bg").getBoundingClientRect();
const bgCenter = { x: bg.left + bg.width / 2, y: bg.top + bg.height / 2 };
const ball = document.getElementById("ball").getBoundingClientRect();
const ballCenter = { x: ball.left + ball.width / 2, y: ball.top + ball.height / 2 };
return this.getAngle(bgCenter.x, bgCenter.y, ballCenter.x, ballCenter.y);
},
setArc() {
const random = (i, j) => Math.floor(Math.random() * (j - i)) + i;
let arc = [];
arc.push(random(0, 300));
arc.push(random(arc[0] + 10, arc[0] + 110));
arc[1] = arc[1] > 360 ? 360 : arc[1];
this.arc = arc;
},
startPlay() {
this.state = "started";
this.taps = 0;
this.score = 0;
this.prevTapTime = Date.now();
},
stopPlay() {
if (this.state === "started") {
this.state = "stopped";
if (this.score > this.best) window.localStorage.best = this.best = this.score;
}
},
tap(e) {
e.preventDefault();
e.stopPropagation();
if (this.state === "started") {
const ballAngle = this.getBallAngle();
// adding a 6 for better accuracy as the arc stroke extends beyond the angle.
if (ballAngle + 6 > this.arc[0] && ballAngle - 6 < this.arc[1]) {
const currentTapTime = Date.now();
const tapInterval = currentTapTime - this.prevTapTime;
this.taps++;
this.score = this.score + (tapInterval < 500 ? 5 : tapInterval < 1000 ? 2 : 1);
this.prevTapTime = currentTapTime;
this.setArc();
} else this.stopPlay();
}
},
},
}).mount("#canvas");
if ("ontouchstart" in window) {
window.addEventListener("touchstart", loopTapApp.tap);
} else {
window.addEventListener("mousedown", loopTapApp.tap);
window.onkeypress = (e) => {
if (e.keyCode == 32) {
if (loopTapApp.state === "stopped") {
loopTapApp.startPlay();
} else {
loopTapApp.tap(e);
}
}
};
}
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/sw.js");
}