-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.js
More file actions
189 lines (146 loc) · 3.07 KB
/
clock.js
File metadata and controls
189 lines (146 loc) · 3.07 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
$(document).ready(function(){
$('#clickMe').click(function(){
init();});
//variables
var canvas = $("#canvas")[0];
var w = $("#canvas").width();
var h = $("#canvas").height();
var ctx = canvas.getContext("2d");
var mode;
var hour;
var minute;
var second;
var secDis;
var minDis;
var hourDis;
var ampm;
var boolampm;
var valid1;
var altcolor;
var textcolor;
var textbool=true;
/**
* Checks that inputs are valid
*/
function valid(){
valid1=true;
if (mode=="12Hr"){
if(hour>12||minute>59||second>59){
valid1=false;
}
}
if (mode=="24Hr"){
if(hour>24||minute>59||second>59){
valid1=false;
}
}
}
/**
* Initializes clock variables and calls paint function
*/
function init(){
create_elements();
mode=document.getElementById("mode").value;
hour=document.getElementById("hour").value;
minute=document.getElementById("minute").value;
second=document.getElementById("second").value;
ampm=document.getElementById("ampm").value;
if(ampm=="am"){boolampm=true;}
else{boolampm=false;}
valid();
if(typeof loop != "undefined") clearInterval(loop);
loop = setInterval(paint, 1000);
}
/**
* creates canvas variable cty
*/
function create_elements(){
var c = document.getElementById("canvas");
cty = c.getContext("2d");
}
/**
* creates canvas that displays the clock
*/
function paint(){
//Canvas
time();
if(valid1==true){
textbool=true;}
else{textbool=!textbool;}
if(textbool==true){
textcolor="red";
}
else{textcolor="black";}
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, w, h);
ctx.strokeStyle = '#993300';
ctx.strokeRect(0, 0, w, h);
ctx.lineWidth=30;
//Display
if(valid1==true){
cty.fillStyle=textcolor;
cty.font = "150px Garamond";
cty.fillText(hourDis + ":" + minDis,20,160);
cty.font="60px Garamond";
cty.fillText(secDis,350,160);
if(mode=="12Hr"){
cty.fillText(ampm,350,100);
}}
else{
cty.fillStyle=textcolor;
cty.font = "150px Garamond";
cty.fillText("12" + ":" + "00",20,160);
cty.font="60px Garamond";
cty.fillText("00",350,160);
if(mode=="12Hr"){
cty.fillText(ampm,350,100);
}}
second++;
}
/**
* calculates when to change clock properties and adjusts hours/minutes/seconds
*/
function time(){
if (second%60==0&&second!=0){ //second reset
second=0;
minute++;}
if (minute%60==0&& minute!=0){ //minute reset
minute=0;
hour++;}
if(second<10){ //Display Correction
secDis="0"+second;}
else{secDis=second;}
if(minute<10){
minDis="0"+minute;}
else{minDis=minute;}
if(hour<10){
hourDis="0"+hour;}
else{hourDis=hour;}
if(hour==12&&minute==0&&second==0){ // am/pm switch
boolampm=!boolampm;
}
if(boolampm==false){
ampm="pm";
}
else{ampm="am";}
if(mode=="12Hr"){ //12 Hr Mode
if(hour>12){
hour=1;
}
}
else if(mode=="24Hr"){ //24 Hr mode
if(hour>24){
hour=1;
}
if(second<10){ //Display Correction
secDis="0"+second;}
else{secDis=second;}
if(minute<10){
minDis="0"+minute;}
else{minDis=minute;}
if(hour<10){
hourDis="0"+hour;}
else{hourDis=hour;}
}
}
})