-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
144 lines (121 loc) · 4.58 KB
/
main.js
File metadata and controls
144 lines (121 loc) · 4.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
document.addEventListener("DOMContentLoaded", function () {
const cake = document.querySelector(".cake");
const candleCountDisplay = document.getElementById("candleCount");
const ageinput = document.getElementById("ageInput")
const storeAgeButton = document.getElementById("storeage");
let candles = [];
let audioContext;
let analyser;
let microphone;
let userAddcandles=0;
function showAlert(message,playsong=false) {
const alertBox = document.getElementById("customAlert");
const alertMessage = document.getElementById("alertMessage");
alertMessage.textContent = message;
alertBox.style.display = "block";
const speech = new SpeechSynthesisUtterance(message);
speech.lang = "en-US"; // Set language (change as needed)
speech.rate = 1; // Adjust speed (default is 1)
speech.pitch = 1; // Adjust pitch (default is 1)
if(!playsong) window.speechSynthesis.speak(speech);
document.getElementById("closeAlert").onclick = function () {
alertBox.style.display = "none";
};
}
storeAgeButton.addEventListener("click", function () {
const ageValue = parseInt(ageinput.value, 10);
if (!isNaN(ageValue) && ageValue > 0) {
userAge = ageValue;
userAddcandles = 0; // Reset candle count
candles = []; // Clear previous candles
showAlert("Age stored successfully: " + userAge + "\nPlease Add the candles !!");
} else {
alert("Please enter a valid age!");
}
});
function updateCandleCount() {
const activeCandles = candles.filter(
(candle) => !candle.classList.contains("out")
).length;
candleCountDisplay.textContent = activeCandles;
if (activeCandles === 0 && candles.length > 0) {
setTimeout(() => {
showAlert("Happy Birthday",true);
const audio = new Audio("bday.mp3"); // Use a direct URL
audio.play();
}, 500); // Small delay for effect
}
}
function addCandle(left, top) {
if (userAddcandles >= parseInt(ageinput.value, 10)) {
return;
}
const candle = document.createElement("div");
candle.className = "candle";
candle.style.left = left + "px";
candle.style.top = top + "px";
const flame = document.createElement("div");
flame.className = "flame";
candle.appendChild(flame);
cake.appendChild(candle);
candles.push(candle);
userAddcandles++;
updateCandleCount();
console.log(userAddcandles)
console.log(ageinput.value)
if (userAddcandles === parseInt(ageinput.value,10)) {
console.log("entered")
setTimeout(() => {
showAlert("Please blow the candles!",true);
}, 500);
}
}
cake.addEventListener("click", function (event) {
const rect = cake.getBoundingClientRect();
const left = event.clientX - rect.left;
const top = event.clientY - rect.top;
addCandle(left, top);
});
function isBlowing() {
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
analyser.getByteFrequencyData(dataArray);
let sum = 0;
for (let i = 0; i < bufferLength; i++) {
sum += dataArray[i];
}
let average = sum / bufferLength;
return average > 40; //
}
function blowOutCandles() {
let blownOut = 0;
if (isBlowing()) {
candles.forEach((candle) => {
if (!candle.classList.contains("out") && Math.random() > 0.5) {
candle.classList.add("out");
blownOut++;
}
});
}
if (blownOut > 0) {
updateCandleCount();
}
}
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices
.getUserMedia({ audio: true })
.then(function (stream) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
analyser = audioContext.createAnalyser();
microphone = audioContext.createMediaStreamSource(stream);
microphone.connect(analyser);
analyser.fftSize = 256;
setInterval(blowOutCandles, 200);
})
.catch(function (err) {
console.log("Unable to access microphone: " + err);
});
} else {
console.log("getUserMedia not supported on your browser!");
}
});