-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.html
361 lines (287 loc) · 9.71 KB
/
clock.html
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=320" />
<title>Clock</title>
<link rel="stylesheet" href="style.css">
<script type="text/javascript">
/**
* Returns the week number for this date. dowOffset is the day of week the week
* "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
* the week returned is the ISO 8601 week number.
* @param int dowOffset
* @return int
*/
Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */
dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
var newYear = new Date(this.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset; //the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() -
(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if(day < 4) {
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52) {
nYear = new Date(this.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weeknum = nday < 4 ? 1 : 53;
}
}
else {
weeknum = Math.floor((daynum+day-1)/7);
}
return weeknum;
};
</script>
<script type="text/javascript" src="lib-0.9.15/paper.js"></script>
<script type="text/paperscript" canvas="canvas">
// alarm functionality (not migrated)
var alarms = new Array();
// relative percentage of screen
var hour_radius_perc = 30;
var min_radius_perc = 50;
var sec_radius_perc = 10;
var centre_radius_perc = 5;
var weeknum_radius = 10;
var weekday_radius = 20;
var centre_radius;
var hour_radius;
var min_radius;
var sec_radius;
calculateSizes();
// create background
var bg = new Path.Rectangle(new Point(0,0),new Size(view.bounds.width,view.bounds.height));
bg.fillColor = '#000000';
// debug area
var debug = new PointText(new Point(100,100));
debug.fillColor = '#ff0000';
//debug.content = "hello";
// create centre of clock
var circle_centre = new Shape.Circle(view.center, centre_radius);
circle_centre.strokeColor = '#ffffff';
circle_centre.strokeWidth = 4.0;
// define the styles
var handStyle = {
strokeColor: '#ffffff',
/*fillColor: '#ffffff',*/
strokeWidth: 4.0,
};
var textStyle = {
font: "Helvetica",
justification: "center",
};
var _currentDate = new Date();
var _debug = false; // debug makes the time move faster
var _debugMins = 1.01; // how many minutes to add for each update
var _oldDate = _currentDate;
initialiseClocks();
function onFrame(event) {
if (_debug){
// visually debug the date / time, by making it keep moving
_currentDate = new Date(_oldDate.getTime() + (_debugMins*60000));
_oldDate = _currentDate;
} else {
_currentDate = new Date();
}
updateClocks(_currentDate);
}
function initialiseClocks()
{
// create hour hand
clockHands = [];
var hours = new ClockHands({type:"hour",
numerals_gap:2,
hand_radius:hour_radius,
position_radius:centre_radius+hour_radius,
anchor_object:circle_centre
});
var mins = new ClockHands({type:"min",
numerals_gap:5,
hand_radius:min_radius,
position_radius:hour_radius+min_radius+(sec_radius*2),
anchor_object:hours
});
var sec = new ClockHands({type:"sec",
numerals_gap:5,
hand_radius:sec_radius,
position_radius:min_radius+sec_radius,
anchor_object:mins
});
clockHands.push(hours);
clockHands.push(mins);
clockHands.push(sec);
}
function updateClocks(d){
var time_hour = d.getHours() % 12;
if (time_hour < 1) time_hour = 12-time_hour;
var time_min = d.getMinutes();
var time_sec = d.getSeconds();
var time_weekday = (d.getDay()-1) % 12;
var time_weeknum = d.getWeek(1);
// calculate the relative times
var perc_milli = d.getMilliseconds() / 1000;
var perc_sec = (time_sec+perc_milli)/60;
var perc_min = (time_min+perc_sec)/60;
var perc_hour = (time_hour+perc_min)/12;
var perc_weekday = time_weekday / 7;
var perc_weeknum = time_weeknum / 52;
// calculate the angles
var angle_weeknum = (perc_weeknum*(2*Math.PI))-(Math.PI/2.0);
var angle_weekday = (perc_weekday*(2*Math.PI))-(Math.PI/2.0);
var angle_hour = (perc_hour*(2*Math.PI))-(Math.PI/2.0);
var angle_min = (perc_min*(2*Math.PI))-(Math.PI/2.0);
var angle_sec = (perc_sec*(2*Math.PI))-(Math.PI/2.0);
var time = {
"weeknum" : angle_weeknum,
"weekday" : angle_weekday,
"hour" : angle_hour,
"min" : angle_min,
"sec" : angle_sec
};
for(var i=0;i<clockHands.length;i++)
{
clockHands[i].updateTime(time);
}
}
function onResize(){
circle_centre.position = view.center;
bg.bounds = view.bounds;
calculateSizes();
circle_centre.radius = centre_radius;
for(var i=0;i<clockHands.length;i++){
clockHands[i].updateRadius({"hour":{hand_radius:hour_radius, position_radius:(centre_radius+hour_radius) },
"min":{hand_radius:min_radius, position_radius:(hour_radius+min_radius+(sec_radius*2)) },
"sec":{hand_radius:sec_radius, position_radius:(min_radius+sec_radius) }
});
}
}
function calculateSizes(){
// work out which is the biggest dimension and set the r to that.
var w = view.size.width;
var h = view.size.height;
var r = w;
if (w > h) r = h;
hour_radius = r * (hour_radius_perc/400);
min_radius = r * (min_radius_perc/400);
sec_radius = r * (sec_radius_perc/400);
centre_radius = r * (centre_radius_perc/400);
}
function positionItem(item, anchor, angle, distance){
var xp = anchor.x + Math.cos(angle)*distance;
var yp = anchor.y + Math.sin(angle)*distance;
//debug.content = angle + "\n"+ Math.round(xp) + "\n" + yp;
item.position = new Point( xp, yp );
}
function removeChildrenButNotMask(grp){
for(var i=grp.children.length-1;i>=0;i--){
var item = grp.children[i];
if (item.name != "mask") item.remove();
}
}
// NEW OOP structure
//function ClockHands(hand_type,gap,hand_rad,position_rad,central_object)
function ClockHands(prefs)
{
// what type of hand is it?
this.hand_type = (prefs.type != undefined) ? prefs.type : "min";
this.isHourHand = false;
if (this.hand_type == "hour") this.isHourHand = true;
this.numerals_gap = (prefs.numerals_gap != undefined) ? prefs.numerals_gap : 1;
this.hand_radius = (prefs.hand_radius != undefined) ? prefs.hand_radius : 50;
this.position_radius = (prefs.position_radius != undefined) ? prefs.position_radius : 50;
this.anchor = (prefs.anchor_object != undefined) ? prefs.anchor_object : 50;;
ClockHands.prototype.init = function()
{
this.constructHands();
}
ClockHands.prototype.constructHands = function()
{
// create circle 'hand'
this.vec_hand = new Shape.Circle(new Point(0,0), this.hand_radius);
this.vec_hand.style = handStyle;
// create mask for numerals
this.vec_mask = new Shape.Circle(new Point(0,0), this.hand_radius-(handStyle.strokeWidth/2));
this.vec_mask.name = "mask";
this.numerals_group = new Group(this.vec_mask);
this.numerals_group.clipped = true;
// create numerals
this.layoutNumbers();
}
ClockHands.prototype.layoutNumbers = function()
{
removeChildrenButNotMask(this.numerals_group);
var start = 0;
var max = 59;
if (this.isHourHand){
start = this.numerals_gap;
max = 12;
}
for(var ii=start;ii<=max;ii+=this.numerals_gap){
var tthr = new PointText(new Point(0,0));
tthr.characterStyle = textStyle;
tthr.fontSize = this.hand_radius*.75;
tthr.justification = "center";
tthr.fillColor = "#ffffff";
tthr.content = ii;
//tthr.name = "counter_"+ii;
//this.numerals_group.addChild(tthr);
raster = tthr.rasterize();
tthr.remove();
raster.name = "counter_"+ii;
this.numerals_group.addChild(raster);
}
}
ClockHands.prototype.updateTime = function(time)
{
// setup variables
var min = 0;
var max = 59;
var angle_max = 60;
if (this.isHourHand){
min = 1;
max = 12;
angle_max = 12;
}
var current_angle = time[this.hand_type];
// update numerals
//
for(ii=min;ii<=max;ii++){
var tthr = this.numerals_group.children["counter_"+ii];
if (tthr != undefined){
var correct_angle = ((ii/angle_max)*(2*Math.PI))-(Math.PI/2.0);
positionItem(tthr, this.anchor.position, correct_angle, this.position_radius);
}
}
// update hand
//
positionItem(this.vec_hand, this.anchor.position, current_angle, this.position_radius);
// update mask
//
this.vec_mask.position = this.vec_hand.position;
this.position = this.vec_hand.position;
}
ClockHands.prototype.updateRadius = function(pref)
{
if (pref[this.hand_type] != undefined){
this.hand_radius = pref[this.hand_type].hand_radius;
this.position_radius = pref[this.hand_type].position_radius;
this.vec_hand.radius = this.hand_radius;
this.vec_mask.radius = this.hand_radius;
this.layoutNumbers();
}
}
this.init();
}
</script>
</head>
<body bgcolor="#000000">
<canvas id="canvas" resize></canvas>
</body>
</html>