-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (107 loc) · 3.52 KB
/
index.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
document.addEventListener("DOMContentLoaded", function(){
const MILISECONDS_IN_A_MINUTE = 60000;
const click = ((document.ontouchstart !== null) ? 'click' : 'touchstart');
// Elements
const tapButton = document.querySelector("button.tap");
const resetButton = document.querySelector("button.reset");
const previousTimestamp = document.querySelector("input.previous-timestamp");
const bpmHistoryElements = document.querySelectorAll('.output .history output');
const bpmAverage = document.querySelector('.output .average output');
const infoElement = document.querySelector('.output .info');
const refresh = function() {
let currentTimestamp = Date.now();
let previousTimestamp = getPreviousTimestamp();
let bpm = calculateBpm(previousTimestamp, currentTimestamp);
updatePreviousTimestamp(currentTimestamp);
updateHistory(bpm);
updateAverage();
updateInfoElement();
}
const calculateBpm = function(previousTimestamp, currentTimestamp) {
let previous = parseInt(previousTimestamp) || 0;
let current = parseInt(currentTimestamp) || 0;
if (previous > 0 && current > 0) {
let milisecondsDifference = (current - previous);
let bpm = Math.round(MILISECONDS_IN_A_MINUTE / milisecondsDifference);
if (bpm > 10) {
return bpm;
}
}
reset();
return '';
}
const getPreviousTimestamp = function() {
return previousTimestamp.value;
}
const updatePreviousTimestamp = function(value) {
previousTimestamp.value = value;
}
const updateHistory = function(newestBpm) {
bpmHistoryElements.forEach(function(bpmHistoryElement) {
let nextElement = bpmHistoryElement.nextElementSibling;
if (nextElement != null) {
newValue = nextElement.value;
} else {
newValue = newestBpm;
}
bpmHistoryElement.value = newValue;
});
}
const bpmHistoryElementValues = function() {
let arr = [];
bpmHistoryElements.forEach(function(bpmHistoryElement) {
let number = parseInt(bpmHistoryElement.value) || 0;
if (number > 0) {
arr.push(number);
}
});
return arr;
}
const updateAverage = function() {
let values = bpmHistoryElementValues();
bpmAverage.value = avg(values);
}
const avg = function(numbers) {
console.log(numbers);
if (numbers.length < 1) {
return '...';
}
let total = 0;
for (i = 0; i < numbers.length; i += 1) {
total = total + numbers[i];
}
return parseInt(total / numbers.length);
}
const reset = function() {
bpmHistoryElements.forEach(resetElement);
resetElement(bpmAverage);
resetElement(previousTimestamp);
updateInfoElement();
}
const resetElement = function(element) {
element.value = "";
}
const initializeInfoElement = function() {
infoElement.innerHTML = 'Start tapping';
}
const clearInfoElement = function() {
infoElement.innerHTML = '';
}
const updateInfoElement = function() {
let values = bpmHistoryElementValues();
if (values.length < 1) {
initializeInfoElement();
} else {
clearInfoElement();
}
}
// https://css-tricks.com/the-trick-to-viewport-units-on-mobile/
const fixViewportHeight = function() {
let vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
tapButton.addEventListener(click, refresh, { passive: false });
resetButton.addEventListener(click, reset, { passive: false });
reset();
fixViewportHeight();
});