-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathscript.js
More file actions
212 lines (203 loc) · 5.58 KB
/
script.js
File metadata and controls
212 lines (203 loc) · 5.58 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* global Vue */
const BALL_PATH_RADIUS = 40;
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",
],
ballAngle: 0, // NEW: angle in degrees
animationFrameId: null, // NEW: for rAF
};
},
computed: {
arcDValue() {
return this.describeArc(
50,
50,
BALL_PATH_RADIUS,
this.arc[0],
this.arc[1]
);
},
ballCx() {
// Ball X position based on angle
return (
50 +
BALL_PATH_RADIUS * Math.cos(((this.ballAngle - 90) * Math.PI) / 180)
);
},
ballCy() {
// Ball Y position based on angle
return (
50 +
BALL_PATH_RADIUS * Math.sin(((this.ballAngle - 90) * Math.PI) / 180)
);
},
},
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() {
// Use the current ballAngle instead of DOM
return this.ballAngle;
},
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;
},
animateBall() {
if (this.state !== "started") return;
// Speed: base duration minus taps*5, as before
const speed = Math.max(500, 2000 - this.taps * 5); // ms for full circle
const now = performance.now();
if (!this._lastFrameTime) this._lastFrameTime = now;
const delta = now - this._lastFrameTime;
this._lastFrameTime = now;
// Advance angle
this.ballAngle = (this.ballAngle + (360 * delta) / speed) % 360;
this.animationFrameId = requestAnimationFrame(this.animateBall);
},
startPlay() {
this.state = "started";
this.taps = 0;
this.score = 0;
this.prevTapTime = Date.now();
this.ballAngle = 0;
this._lastFrameTime = null;
if (this.animationFrameId) cancelAnimationFrame(this.animationFrameId);
this.animationFrameId = requestAnimationFrame(this.animateBall);
},
stopPlay() {
if (this.state === "started") {
this.state = "stopped";
if (this.score > this.best)
window.localStorage.best = this.best = this.score;
if (this.animationFrameId) cancelAnimationFrame(this.animationFrameId);
this.animationFrameId = null;
}
},
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();
}
},
shareScore() {
if (navigator.share) {
navigator
.share({
title: "Looptap",
text: `Beat my score: ${this.score}\nLooptap - a minimal game to waste your time.`,
url: "https://vasanthv.github.io/looptap/",
})
.catch(() => {});
} else {
alert("Sharing is not supported on this browser.");
}
},
},
mounted() {
// Clean up on destroy
window.addEventListener("beforeunload", () => {
if (this.animationFrameId) cancelAnimationFrame(this.animationFrameId);
});
},
}).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");
}