-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
94 lines (91 loc) · 2.65 KB
/
main.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
class Clicker{
constructor(clockElement, clickElement, options={}){
this.timer = 0
this.clicks = 0
this.now = Date.now()
this.started = false
this.clockElement = clockElement
this.clickElement = clickElement
this.options = options
this.options?.initOptions ? this.options.initOptions(this) : 0
this.clockElement.innerText = "0.00"
this.clickElement.innerText = "0"
this.updateInterval = false
}
start(){
this.now = Date.now()
this.updateInterval = setInterval(() => this.update(), 10)
this.started = true
}
stop(){
clearInterval(this.updateInterval)
this.clicks = 0
this.clockElement.innerText = "0.00"
this.clickElement.innerText = "0"
}
update(){
this.timer += Date.now() - this.now
this.now = Date.now()
this.clockElement.innerText = String((this.timer/1000).toFixed(2)).padStart(2, '0')
this.clickElement.innerText = this.clicks
this.options?.updateEvent ? this.options.updateEvent(this) : 0
}
click(){
this.clicks += 1
if(this.clicks == 100){
clearInterval(this.updateInterval)
this.update()
this.win()
return 0
}
if(this.clicks > 100){
return 1
}
this.options?.clickEvent ? this.options.clickEvent(this) : 0
}
getCPS(){
return this.clicks / this.timer
}
isRunning(){
return this.started
}
win(){
clearInterval(this.updateInterval)
this.timer += Date.now() - this.now
}
}
let options = {
initOptions: (object) => {
object.fontSize = 16
object.clickElement.style.fontSize = object.fontSize + "px"
},
clickEvent: (object) => {
object.clickElement.style.fontSize = "35px"
object.fontSize = 35
},
updateEvent: (object) => {
if(object.fontSize > 16){
object.fontSize--
object.clickElement.style.fontSize = object.fontSize + "px"
}
}
}
let clockElement = document.getElementById('time')
let clickElement = document.getElementById('clicks')
let currentTest = new Clicker(clockElement, clickElement, options)
currentTest.update()
document.addEventListener('keydown', () => {
if(currentTest.isRunning()){
currentTest.stop()
currentTest = new Clicker(clockElement, clickElement, options)
}
})
document.addEventListener('contextmenu', e => {
e.preventDefault()
})
document.addEventListener('click', () => {
if(!currentTest.isRunning()){
currentTest.start()
}
currentTest.click()
})