-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsakarvadia19mFPP1.js
284 lines (243 loc) · 7.69 KB
/
sakarvadia19mFPP1.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
window.addEventListener("load", init);
var alarmPrompt = "";
var timerPrompt = "";
var audio = "";
var mp3 = "";
var alarmFlag = true;
var timerFlag = true;
var now = new Date();
var timerHour = 0;
var timerMinute = 0;
var timerSecond = 0;
var num = 0;
var angNum = 0;
var tick = new Audio('Ticking-clock-sound.mp3');
var timerSound ;
function init()
{
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds(); //define seconds inside draw
window.setInterval(drawClock, 20);
function drawClock()
{
//when calculating the end point of the hands, the coordinates get a little tricky:
//we have to add the cos value to the Y coordinate and sin value to the x coordinate, because the clock is not oriented like the unit circle
//the y value must be negative becuase the clock is moving clockwise, as opposed to the counter clockwise unit circle
now =new Date();
//draws brown rim
ctx.beginPath();
ctx.arc(centerX,centerY,240,0,2*Math.PI);
ctx.fillStyle = "#654321";
ctx.fill();
ctx.closePath();
//draws little black interior rim
ctx.beginPath();
ctx.arc(centerX,centerY,205,0,2*Math.PI);
ctx.fillStyle = "#DAA520";
ctx.fill();
ctx.closePath();
//draws main clock circle
ctx.beginPath();
ctx.arc(centerX,centerY,200,0,2*Math.PI);
ctx.fillStyle = '#FAFAD2';
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.arc(centerX,centerY,20,0,2*Math.PI);
ctx.fillStyle = '#DAA520';
ctx.fill();
ctx.closePath();
//draws companyname
ctx.beginPath();
ctx.textBaseline="middle";
ctx.textAlign="center";
ctx.fillStyle = "black";
ctx.font = "15px Times New Roman";
ctx.fillText("MS Clockmakers Inc.",centerX, centerY+50);
ctx.closePath();
//draws company year
ctx.beginPath();
ctx.textBaseline="middle";
ctx.fillStyle = "black";
ctx.textAlign="center";
ctx.font = "15px Times New Roman";
ctx.fillText("Since 2001",centerX, centerY+70);
ctx.closePath();
//calls the function that draws numbers
drawNum();
//draws second hand
ctx.beginPath();
ctx.lineCap = "round";
ctx.strokeStyle= "red";
ctx.lineWidth = 1;
ctx.moveTo(centerX, centerY);
ctx.lineTo(centerX+170*Math.sin(((now.getSeconds())*6)*Math.PI /180),
centerY+170*-Math.cos(((now.getSeconds())*6)*Math.PI /180));
ctx.stroke();
tick.play();
ctx.closePath();
//draws minutes hand
ctx.beginPath();
ctx.lineCap = "round";
ctx.strokeStyle= "#DAA520";
ctx.lineWidth = 7;
ctx.moveTo(centerX, centerY);
ctx.lineTo(centerX+150*Math.sin(((now.getMinutes())*6)*Math.PI /180),
centerY+150*-Math.cos(((now.getMinutes())*6)*Math.PI /180));
ctx.stroke();
ctx.closePath();
//draws hours hand
ctx.beginPath();
ctx.lineCap = "round";
ctx.strokeStyle= "#DAA520";
ctx.lineWidth = 13;
ctx.moveTo(centerX, centerY);
ctx.lineTo((centerX+100*Math.sin( ((now.getHours())*30*Math.PI/180) +now.getMinutes()*.5*Math.PI /180)),
(centerY+100*-Math.cos( ((now.getHours())*30*Math.PI/180) +now.getMinutes()*.5*Math.PI /180)));
ctx.stroke();
ctx.closePath();
//little knob that covers all hands
ctx.beginPath();
ctx.arc(centerX,centerY,10,0,2*Math.PI);
ctx.fillStyle = "#654321";
ctx.fill();
ctx.closePath();
//draw numbers
function drawNum()
{
num = 1;
angNum =0;
for(num=1;num<13;num++)
{
angNum += 1;
ctx.beginPath();
ctx.fillStyle= "black";
ctx.textBaseline="middle";
ctx.textAlign="center";
ctx.font = "40px Times New Roman";
ctx.fillText(num,(centerX+165*Math.sin(angNum*30*Math.PI/180)),
(centerY+165*-Math.cos(angNum*30*Math.PI/180)));
ctx.closePath();
}
}
//draws 60 tick marks around perimeter of clock
function drawTick()
{
var tock = 0;
for(tock = 0;tock<61;tock++)
{
ctx.beginPath();
ctx.moveTo(centerX+190*Math.sin((tock*6)*Math.PI /180),
centerY+190*-Math.cos((tock*6)*Math.PI /180));
ctx.lineTo(centerX+200*Math.sin((tock*6)*Math.PI /180),
centerY+200*-Math.cos((tock*6)*Math.PI /180));
ctx.strokeStyle = "black";
ctx.lineWidth= 1;
ctx.stroke();
ctx.closePath();
}
}
drawTick();
}
document.getElementById("alarmButton").addEventListener("click", setAlarm);
document.getElementById("timerButton").addEventListener("click", setTimer);
}
function setTimer()
{
var now = new Date();
timerPrompt = prompt("Enter how long you want your timer to be.", "HH:MM:SS");
//figure out correct REGEX!!! --> timer will loop every 24 hours, but if 8 hour timer is set at 23:59... wat do u do
if (timerPrompt.search(/([2][0-3]|[0-1][0-9]):[0-5][0-9]:[0-5][0-9]/) == 0)
{
document.getElementById("Tconfirm").innerHTML = "Your timer is set for " + timerPrompt;
setTimeout(ringTimer,timerPrompt.slice(0,2)*3600000+timerPrompt.slice(3,5)*60000+timerPrompt.slice(-2)*1000);
setTimeout(stopTimer,500+timerPrompt.slice(0,2)*3600000+timerPrompt.slice(3,5)*60000+timerPrompt.slice(-2)*1000);
}
else
{
alert("The expected format is: HH:MM:SS");
}
}
//will give user the option to stop timer
function stopTimer()
{
if (!timerFlag)
{
document.getElementById('stopTimerButton').style.display = "inline-block";
document.getElementById('stopTimerButton').addEventListener("click", pauseTimer);
document.getElementById('stopTimerButton').addEventListener("click", function(){timerFlag=true});
document.getElementById('stopTimerButton').addEventListener("click", function(){
document.getElementById('stopTimerButton').style.display = "none"});
console.log(timerFlag);
document.getElementById("Tconfirm").innerHTML = "";
return;
}
}
//will ring when timer is up
function ringTimer()
{
var now = new Date();
timerFlag = false;
console.log(timerFlag);
timerSound = new Audio("bensound-cute.mp3");
timerSound.play();
}
//allows user to set alarm
function setAlarm()
{
var now = new Date();
alarmPrompt = prompt("Enter the time you want to sound the alarm. Enter in Military time.", "HH:MM:SS");
if ((alarmPrompt.search(/([2][0-3]|[0-1][0-9]):[0-5][0-9]:[0-5][0-9]/) == 0))
{
document.getElementById("Aconfirm").innerHTML = "Your Alarm is set to " + alarmPrompt;
var now = new Date();
window.setInterval(soundAlarm, 700);
}
else
{
alert("The expected format is MILITARY TIME: HH:MM:SS");
}
}
//will ring when the alarm time has been reached
function soundAlarm()
{
if (!alarmFlag)
{
document.getElementById('stopAlarmButton').style.display = "inline-block";
document.getElementById('stopAlarmButton').addEventListener("click", stopSound);
document.getElementById('stopAlarmButton').addEventListener("click", function(){alarmFlag=true});
document.getElementById('stopAlarmButton').addEventListener("click", function(){
document.getElementById('stopAlarmButton').style.display = "none"});
console.log(alarmFlag);
document.getElementById("Aconfirm").innerHTML = "";
return;
}
var now = new Date();
if ((parseInt(alarmPrompt.slice(0,2))==now.getHours()) &&
(parseInt(alarmPrompt.slice(3,5))==now.getMinutes()) &&
(parseInt(alarmPrompt.slice(-2))==now.getSeconds()))
{
alarmFlag = false;
startSound("bensound-littleidea.mp3");
}
}
function startSound()
{
audio = new Audio('bensound-littleidea.mp3');
audio.play();
document.getElementById('stopAlarmButton').style.display = "none";
}
function stopSound()
{
audio.pause();
}
function pauseTimer()
{
timerSound.pause();
}