-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClock.js
More file actions
191 lines (160 loc) · 6.08 KB
/
Copy pathClock.js
File metadata and controls
191 lines (160 loc) · 6.08 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
190
191
/// <reference path="JsChart-vsdoc.js" />
var Gauges = MindFusion.Gauges;
var d = MindFusion.Drawing;
var OvalScale = Gauges.OvalScale;
var Length = Gauges.Length;
var LengthType = Gauges.LengthType;
var Alignment = Gauges.Alignment;
var TickShape = Gauges.TickShape;
var Pointer = Gauges.Pointer;
var PointerShape = Gauges.PointerShape;
// create the gauge JS object
var clock = Gauges.OvalGauge.create(document.getElementById('clock'), false);
clock.addEventListener(Gauges.Events.prepaintBackground, onPrepaintBackground.bind(this));
clock.addEventListener(Gauges.Events.prepaintPointer, onPrepaintPointer.bind(this));
// define the scales
var scale = new OvalScale(clock);
scale.minValue = 0;
scale.maxValue = 60;
scale.startAngle = -90;
scale.endAngle = 270;
scale.fill = 'Transparent';
scale.stroke = 'Transparent';
// ticks for the hours
var majorSettings = scale.majorTickSettings;
majorSettings.showLabels = false;
majorSettings.tickShape = TickShape.Rectangle;
majorSettings.tickWidth = new Length(6, LengthType.Relative);
majorSettings.tickHeight = new Length(1.8, LengthType.Relative);
majorSettings.fill = '#334155'; // Slate 700
majorSettings.stroke = '#334155';
majorSettings.tickAlignment = Alignment.Inner;
majorSettings.count = 12;
// ticks for the minutes
var middleSettings = scale.middleTickSettings;
middleSettings.showLabels = false;
middleSettings.tickShape = TickShape.Rectangle;
middleSettings.tickWidth = new Length(3, LengthType.Relative);
middleSettings.tickHeight = new Length(0.8, LengthType.Relative);
middleSettings.fill = '#94A3B8'; // Slate 400
middleSettings.stroke = '#94A3B8';
middleSettings.tickAlignment = Alignment.Inner;
middleSettings.count = 5;
var minorSettings = scale.minorTickSettings;
minorSettings.showLabels = false;
minorSettings.showTicks = false;
// the clock hands
var now = new Date();
// hour hand
var hourHand = new Pointer();
hourHand.name = 'HourHand';
hourHand.fill = 'Transparent';
hourHand.stroke = 'Transparent';
hourHand.pointerWidth = new Length(55, LengthType.Relative);
hourHand.pointerHeight = new Length(4, LengthType.Relative);
hourHand.shape = PointerShape.Needle;
hourHand.value = ((now.getHours() * 5) % 60) + (now.getMinutes() / 60 * 5);
scale.addPointer(hourHand);
// minute hand
var minuteHand = new Pointer();
minuteHand.name = 'MinuteHand';
minuteHand.fill = 'Transparent';
minuteHand.stroke = 'Transparent';
minuteHand.pointerWidth = new Length(80, LengthType.Relative);
minuteHand.pointerHeight = new Length(3, LengthType.Relative);
minuteHand.shape = PointerShape.Needle;
minuteHand.value = now.getMinutes() % 60;
scale.addPointer(minuteHand);
// seconds hand
var secondHand = new Pointer();
secondHand.name = 'SecondHand';
secondHand.fill = 'Transparent';
secondHand.stroke = 'Transparent';
secondHand.pointerWidth = new Length(88, LengthType.Relative);
secondHand.pointerHeight = new Length(1.5, LengthType.Relative);
secondHand.shape = PointerShape.Needle;
secondHand.value = now.getSeconds();
scale.addPointer(secondHand);
// update the intervals at 1 sec
setInterval(updateTime, 1000);
function updateTime() {
var gauge = Gauges.OvalGauge.find("clock");
if (!gauge) return;
var hHand = gauge.getElementByName("HourHand");
var mHand = gauge.getElementByName("MinuteHand");
var sHand = gauge.getElementByName("SecondHand");
var current = new Date();
if (hHand) hHand.value = ((current.getHours() * 5) % 60) + (current.getMinutes() / 60 * 5);
if (mHand) mHand.value = current.getMinutes() % 60;
if (sHand) sHand.value = current.getSeconds();
}
// 7. Minimalist Dial Painting
function onPrepaintBackground(sender, args) {
args.cancelDefaultPainting = true;
var context = args.context;
var element = args.element;
var bounds = new d.Rect(0, 0, element.renderSize.width, element.renderSize.height);
var cx = bounds.center().x;
var cy = bounds.center().y;
var radius = bounds.width / 2;
context.save();
// paint the outer card background
context.beginPath();
context.arc(cx, cy, radius - 2, 0, 2 * Math.PI);
context.fillStyle = '#FFFFFF';
context.fill();
// paint the border ring
context.lineWidth = 3;
context.strokeStyle = '#F1F5F9'; // Slate 100
context.stroke();
// the inner track guideline
context.beginPath();
context.arc(cx, cy, radius - 14, 0, 2 * Math.PI);
context.lineWidth = 1;
context.strokeStyle = '#E2E8F0'; // Slate 200
context.stroke();
context.restore();
}
// paint the hand and the center cap
function onPrepaintPointer(sender, args) {
args.cancelDefaultPainting = true;
var context = args.context;
var element = args.element;
context.save();
context.transform.apply(context, element.transform.matrix());
context.scale(element.children[0].renderSize.width, element.children[0].renderSize.height);
context.beginPath();
if (element.name === "HourHand") {
context.fillStyle = "#0F172A"; // Dark Slate
drawRoundedPointer(context, 0, 0.38, 0.65, 0.24, 0.12);
} else if (element.name === "MinuteHand") {
context.fillStyle = "#334155"; // Mid Slate
drawRoundedPointer(context, 0, 0.40, 0.90, 0.20, 0.10);
} else if (element.name === "SecondHand") {
context.fillStyle = "#F97316"; // Vibrant Orange Accent
// Extended tail for a classic modern balance
drawRoundedPointer(context, -0.15, 0.43, 1.10, 0.14, 0.07);
}
context.fill();
// paint the cap pin in the center (drawn once over the second hand for layering)
if (element.name === "SecondHand") {
context.beginPath();
context.arc(0, 0.5, 0.18, 0, 2 * Math.PI);
context.fillStyle = "#0F172A";
context.fill();
// Inner accent dot on center pin
context.beginPath();
context.arc(0, 0.5, 0.06, 0, 2 * Math.PI);
context.fillStyle = "#F97316";
context.fill();
}
context.restore();
}
// a helper function to draw smooth rounded pointers
function drawRoundedPointer(ctx, x, y, width, height, radius) {
if (ctx.roundRect) {
ctx.roundRect(x, y, width, height, radius);
} else {
ctx.rect(x, y, width, height);
}
}