-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock_b7.html
417 lines (342 loc) · 12.6 KB
/
clock_b7.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<!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">
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 = 5;
var weeknum_radius = 10;
var weekday_radius = 20;
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';
var debug = new PointText(new Point(100,100));
debug.fillColor = '#ff0000';
//debug.content = "hello";
var alarms_layer = new Layer();
var hands_layer = new Layer();
var numbers_layer = new Layer();
var numbers_hour_layer = new Layer();
var numbers_min_layer = new Layer();
// create centre of clock
var circle_centre = new Path.Circle(view.center, centre_radius);
circle_centre.strokeColor = '#ffffff';
circle_centre.strokeWidth = 0.1;
hands_layer.addChild(circle_centre);
var alarmStyle = {
strokeColor: '#999999',
strokeWidth: 2.0,
};
var handStyle = {
strokeColor: '#ffffff',
/*fillColor: '#ffffff',*/
strokeWidth: 4.0,
};
var dateHandStyle = {
strokeColor: handStyle.strokeColor,
strokeWidth: 0,
dashArray: [4,1],
};
var textStyle = {
fffont: "American Typewriter, Courier, typewriter",
font: "Helvetica",
justification: "center",
};
// create the date parts
var circle_weeknum = new Shape.Circle(new Point(0,0), weeknum_radius);
circle_weeknum.style = dateHandStyle;
hands_layer.addChild(circle_weeknum);
var circle_weekday = new Shape.Circle(new Point(0,0), weekday_radius);
circle_weekday.style = dateHandStyle;
hands_layer.addChild(circle_weekday);
// now create the time parts
// create hour 'hand'
var circle_hour = new Shape.Circle(new Point(0,0), hour_radius);
circle_hour.style = handStyle;
var group_hour = new Group(circle_hour); //,text_hour);
hands_layer.addChild(group_hour);
// create minutes 'hand'
var circle_min = new Shape.Circle(new Point(0,0), min_radius);
circle_min.style = handStyle;
var group_min = new Group(circle_min); //,text_min);
hands_layer.addChild(group_min);
// create seconds 'hand'
var circle_sec = new Shape.Circle(new Point(0,0), sec_radius);
circle_sec.style = handStyle;
var group_sec = new Group(circle_sec);
hands_layer.addChild(group_sec);
// create the masks for hours, minutes and seconds
var mask_hour = new Shape.Circle(new Point(0,0), hour_radius-(handStyle.strokeWidth/2));
var mask_hour_grp = new Group(mask_hour);
mask_hour_grp.opacity = 0.99;
mask_hour_grp.clipped = true;
var mask_min = new Shape.Circle(new Point(20,100), min_radius-(handStyle.strokeWidth/2));
var mask_min_grp = new Group(mask_min);
mask_min_grp.opacity = 0.99;
mask_min_grp.clipped = true;
var mask_sec = new Shape.Circle(new Point(20,100), sec_radius-(handStyle.strokeWidth/2));
var mask_sec_grp = new Group(mask_sec);
mask_sec_grp.opacity = 0.99;
mask_sec_grp.clipped = true;
// generate the numbers
createNumbers(mask_hour_grp, mask_min_grp, mask_sec_grp);
var nowDate = new Date();
//addAlarm( new Date(79,5,24,6,45,0) );
//addAlarm( new Date(nowDate.getFullYear(),nowDate.getMonth(),nowDate.getDate(),nowDate.getHours(),nowDate.getMinutes()+1.0,nowDate.getSeconds()) );
var _debug = false;
var _oldDate = new Date();
var _currentDate = new Date();
var _count = 0;
var _max = 5;
function onFrame(event) {
if (_debug){
// visually debug the date, by making it keep moving
var now = new Date();
var inc = 0;
_count += 1;
if (_count > _max){
_count = 0;
inc = 1;
}
_currentDate = new Date(_oldDate.getFullYear(),_oldDate.getMonth(),_oldDate.getDate()+inc,now.getHours(),now.getMinutes(),now.getSeconds(),now.getMilliseconds());
} else {
_currentDate = new Date();
}
positionHands(_currentDate,circle_weeknum,circle_weekday,group_hour,group_min,group_sec,null,null,null);
mask_hour.position = group_hour.position;
mask_min.position = group_min.position;
mask_sec.position = group_sec.position;
updateNumbers(mask_hour_grp, mask_min_grp, mask_sec_grp);
_oldDate = _currentDate;
}
function positionHands(d,g_weeknum,g_weekday,g_hour,g_min,g_sec,t_hour,t_min,t_sec){
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);
if (g_weeknum != null) positionItem(g_weeknum, view.center, angle_weeknum, centre_radius+weeknum_radius+(weekday_radius*2));
if (g_weekday != null) positionItem(g_weekday, g_weeknum.position, angle_weekday, weeknum_radius+weekday_radius);
positionItem(g_hour, view.center, angle_hour, centre_radius+/*(weeknum_radius*2)+(weekday_radius*4)*/+hour_radius/*+(min_radius*2)+(sec_radius*4)*/);
positionItem(g_min, g_hour.position, angle_min, hour_radius+min_radius+(sec_radius*2));
positionItem(g_sec, g_min.position, angle_sec, min_radius+sec_radius);
}
function onResize(){
circle_centre.position = view.center;
bg.bounds = view.bounds;
calculateSizes();
// now apply the new sizes
circle_hour.radius = hour_radius;
circle_min.radius = min_radius;
circle_sec.radius = sec_radius;
mask_hour.radius = hour_radius;
mask_min.radius = min_radius;
mask_sec.radius = sec_radius;
moveAlarms();
updateNumbers(mask_hour_grp, mask_min_grp);
}
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);
}
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 addAlarm(d){
var radius_border = 2.5;
var c_weeknum = new Path.Circle(new Point(0,0), weeknum_radius+radius_border);
c_weeknum.style = alarmStyle;
var c_weekday = new Path.Circle(new Point(0,0), weekday_radius+radius_border);
c_weekday.style = alarmStyle;
var c_hour = new Path.Circle(new Point(0,0), hour_radius+radius_border);
c_hour.style = alarmStyle;
alarms_layer.addChild(c_hour);
var c_min = new Path.Circle(new Point(0,0), min_radius+radius_border);
c_min.style = alarmStyle;
alarms_layer.addChild(c_min);
var c_sec = new Path.Circle(new Point(0,0), sec_radius+radius_border);
c_sec.style = alarmStyle;
alarms_layer.addChild(c_sec);
positionHands(d,c_weeknum,c_weekday,c_hour,c_min,c_sec);
alarms.push({date:d,weeknum:c_weeknum,weekday:c_weekday,hour:c_hour,min:c_min,sec:c_sec,trigger:0});
}
function moveAlarms(){
for(var i=0;i<alarms.length;i++){
var alarm = alarms[i];
positionHands(alarm['date'],alarm['weeknum'],alarm['weekday'],alarm['hour'],alarm['min'],alarm['sec']);
}
}
function createNumbers(hour_grp,min_grp,sec_grp){
var ii;
var tthr;
removeChildrenButNotMask(hour_grp);
removeChildrenButNotMask(min_grp);
removeChildrenButNotMask(sec_grp);
for(ii=2;ii<=12;ii+=2){
tthr = new PointText(new Point(0,0));
tthr.characterStyle = textStyle;
tthr.fontSize = hour_radius*.75;
tthr.justification = "center";
tthr.fillColor = "#ffffff"; //"#fafafa";
tthr.content = ii;
raster = tthr.rasterize();
tthr.remove();
raster.name = "hour_"+ii;
hour_grp.addChild(raster);
}
for(ii=0;ii<=59;ii+=5){
tthr = new PointText(new Point(100,100));
tthr.characterStyle = textStyle;
tthr.fontSize = min_radius*.75;
tthr.justification = "center";
tthr.fillColor = "#ffffff"; //"#fafafa";
tthr.content = ii;
raster = tthr.rasterize();
tthr.remove();
raster.name = "min_"+ii;
min_grp.addChild(raster );
}
for(ii=0;ii<=59;ii+=5){
tthr = new PointText(new Point(100,100));
tthr.characterStyle = textStyle;
tthr.fontSize = sec_radius*.75; // *1.25;
tthr.justification = "center";
tthr.fillColor = "#ffffff";
tthr.content = ii;
raster = tthr.rasterize();
tthr.remove();
raster.name = "sec_"+ii;
sec_grp.addChild(raster);
}
}
function removeChildrenButNotMask(grp){
for(var i=1;i<grp.children.length;i++){
grp.children[i].remove();
}
}
function updateNumbers(hour_grp,min_grp,sec_grp){
var ii;
var tthr;
var nowHour = _currentDate.getHours() % 12;
var nowMinute = _currentDate.getMinutes();
var nowSeconds = _currentDate.getSeconds();
for(ii=1;ii<=12;ii++){
tthr = hour_grp.children["hour_"+ii];
if (tthr != undefined){
var angle_txt_hour = ((ii/12)*(2*Math.PI))-(Math.PI/2.0);
positionItem(tthr, view.center, angle_txt_hour, centre_radius+hour_radius);
//tthr.fillColor = "#333333";
tthr.opacity = 1.0; // paperjs 0.22 bug - if a masked item is not 1.0 opacity then it becomes invisible
if ((ii % 12) == nowHour){
tthr.fillColor = "#ffffff";
tthr.opacity = 0.8;
//hour_grp.addChild(tthr);
}
}
}
for(ii=0;ii<=59;ii++){
tthr = min_grp.children["min_"+ii];
if (tthr != undefined){
var angle_txt_min = ((ii/60)*(2*Math.PI))-(Math.PI/2.0);
positionItem(tthr, group_hour.position, angle_txt_min, hour_radius+min_radius+(sec_radius*2));
//tthr.fillColor = "#222222";
tthr.opacity = 1.0; // paperjs 0.22 bug - if a masked item is not 1.0 opacity then it becomes invisible
if (ii == nowMinute){
tthr.fillColor = "#ffffff";
//min_grp.addChild(tthr);
tthr.opacity = 1.0;
}
}
}
if (sec_grp){
for(ii=0;ii<=59;ii++){
tthr = sec_grp.children["sec_"+ii];
if (tthr != undefined){
var angle_txt_sec = ((ii/60)*(2*Math.PI))-(Math.PI/2.0);
positionItem(tthr, group_min.position, angle_txt_sec, min_radius+sec_radius);
//tthr.fillColor = "#444444";
tthr.opacity = 1.0;
if (ii == nowSeconds){
tthr.fillColor = "#ffffff";
tthr.opacity = 1.0;
}
}
}
}
}
</script>
</head>
<body bgcolor="#000000">
<canvas id="canvas" resize></canvas>
</body>
</html>