-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.html
More file actions
516 lines (431 loc) · 15.2 KB
/
clock.html
File metadata and controls
516 lines (431 loc) · 15.2 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
ul {
list-style: none;
}
.canvas {
position: absolute;
width: 401px;
height: 401px;
top: 80px;
left: 50%;
transform: translateX(-50%);
}
.timepiece {
/* background-color: lightgreen;*/
position: relative;
width: 401px;
height: 401px;
border: 2px solid #000;
box-shadow: 2px 3px 30px #aaa;
border-radius: 50%;
margin: 80px auto;
user-select: none;
}
.constantly .li {
position: absolute;
left: 50%;
top: 0;
transform: translate(-50%);
width: 2px;
height: 9px;
background-color: #000;
transform-origin: center 200px;
}
.constantly .lip {
width: 4px;
height: 12px;
}
.interval {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 30px;
height: 30px;
background-color: #fff;
border-radius: 50%;
text-align: center;
line-height: 100px;
z-index: 2;
box-shadow: 0 3px 20px #000;
}
.second {
position: absolute;
left: 50%;
top: 0px;
transform: translateX(-50%);
transform-origin: center 200px;
width: 8px;
height: 18px;
background-color: aqua;
/* border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px; */
border-radius: 8px;
}
.minute {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -100%);
width: 5px;
height: 180px;
background-color: #fff;
border-radius: 2px;
transform-origin: bottom;
box-shadow: 0 0 10px #666;
}
a {
position: absolute;
font-size: 22px;
font-weight: blod;
}
a:nth-of-type(1) {
top: -34px;
left: 50%;
transform: translateX(-50%);
}
a:nth-of-type(2) {
right: -34px;
transform: translateY(-50%);
top: 50%;
}
a:nth-of-type(3) {
bottom: -34px;
left: 50%;
transform: translateX(-50%);
}
a:nth-of-type(4) {
transform: translateY(-50%);
top: 50%;
left: -34px;
}
button {
cursor: pointer;
border: 2px solid #ccc;
width: 140px;
height: 44px;
border-radius: 30px;
color: #fff;
background-color: #3383ff;
}
button[disabled=true] {
opacity: 0.6;
color: #aaa;
cursor: no-drop;
}
.input,
.input2 {
width: 200px;
height: 40px;
padding: 0 16px;
border-radius: 10px;
box-sizing: border-box;
border: 2px solid #ccc;
}
.setting {
width: 500px;
margin: 0 auto;
}
.select {
border: 2px solid #ccc;
width: 200px;
height: 40px;
border-radius: 16px;
}
</style>
</head>
<body>
<canvas class="canvas" width="401" height="401"></canvas>
<div class="timepiece">
<a>0</a>
<a>15</a>
<a>30</a>
<a>45</a>
<!-- 钟表的时刻 -->
<ul class="constantly"></ul>
<!-- 钟表的中间 -->
<div class="interval"></div>
<!-- 绘制一个分钟的指针 -->
<div class="minute"></div>
</div>
<div class="setting">
<button class="start">开始</button>
<button class="pause">暂停</button>
<button class="reset">重置</button>
<p>
当前时间:
<input class="input" type="number" placeholder="请输入time"></input>
s
</p>
<p>
定时器:
<input class="input2" type="number"></input>
ms
</p>
<p>
铃声:
<select class="select"></select>
</p>
</div>
<script>
const timepiece = document.querySelector('.timepiece');
const ul = document.querySelector('.constantly')
const interval = document.querySelector('.interval')
const minute = document.querySelector('.minute')
const canvas = document.querySelector('.canvas')
const startBtn = document.querySelector('.start');
const pauseBtn = document.querySelector('.pause');
const resetBtn = document.querySelector('.reset');
const input = document.querySelector(".input");
const input2 = document.querySelector(".input2");
const select = document.querySelector('.select');
const ctx = canvas.getContext('2d');
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const radius = canvas.height / 2;
const startAngle = Math.PI * 1.5;
// 遍历实现时钟刻度
for (let i = 0; i < 12; i++) {
let li = document.createElement('li')
li.style.transform = `translate(-50%) rotate(${i * 30}deg)`
ul.appendChild(li)
ul.children[i].classList.add('li')
}
for (let j = 0; j < 4; j++) {
ul.children[j * 3].classList.add('lip')
}
const MAX_TIME = 3600;
// 1deg 等于 10秒,360deg = 3600s
const ONE_DEG = 10;
// 五分钟走一格
let time = 0;
// 设置定时器让时钟实时动起来
let timer = null;
// 设定的定时器多少秒执行一次
let startIntervalTime = 1000;
let audio = null;
const oneDegValue = Math.PI / 180;
let curDeg = time / 10;
init();
function start() {
timer = setInterval(() => {
time++;
// time += 10;
render();
}, startIntervalTime);
}
function pause() {
clearInterval(timer);
timer = null;
}
function renderElement() {
input.value = time;
const deg = time / ONE_DEG;
curDeg = deg;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
if (deg > 270) {
// 红色
ctx.fillStyle = 'red';
} else if (deg > 180) {
// 黄色
ctx.fillStyle = 'yellow';
} else if (deg >= 90) {
// 蓝色
ctx.fillStyle = 'lightblue';
} else {
// 绿色
ctx.fillStyle = 'lightgreen';
}
ctx.beginPath();
ctx.moveTo(centerX, centerY);
ctx.arc(centerX, centerY, radius, startAngle, startAngle - (Math.PI * (2 - (deg / 180))), true);
ctx.lineTo(centerX, centerY);
ctx.closePath();
ctx.fill();
ctx.restore();
minute.style.transform = `translate(-50%, -100%) rotate(${deg}deg)`;
}
function render() {
if (time >= MAX_TIME) {
pause();
if (audio) {
audio = audio.url === select.value ? audio : new Audio(select.value);
audio.play();
}
resetBtn.removeAttribute("disabled");
pauseBtn.setAttribute("disabled", true);
}
renderElement();
}
function init() {
render();
input2.value = startIntervalTime;
pauseBtn.setAttribute('disabled', true);
resetBtn.setAttribute('disabled', true);
startBtn.addEventListener("click", () => {
if (!timer) {
start();
audio && audio.pause();
input.setAttribute('disabled', true);
input2.setAttribute('disabled', true);
select.setAttribute('disabled', true);
startBtn.setAttribute('disabled', true);
pauseBtn.removeAttribute('disabled');
resetBtn.setAttribute('disabled', true);
}
});
pauseBtn.addEventListener("click", () => {
input.removeAttribute('disabled');
input2.removeAttribute('disabled');
select.removeAttribute('disabled');
startBtn.removeAttribute('disabled');
pauseBtn.setAttribute('disabled', true);
resetBtn.removeAttribute('disabled');
pause();
});
resetBtn.addEventListener("click", () => {
audio && audio.pause();
resetBtn.setAttribute('disabled', true);
input.removeAttribute('disabled');
input2.removeAttribute('disabled');
select.removeAttribute('disabled');
startBtn.removeAttribute('disabled');
pauseBtn.setAttribute('disabled', true);
time = 0;
input.value = time;
render();
});
input.addEventListener("change", e => {
const value = Math.min(Math.abs(parseInt(e.target.value)), 3600);
e.target.value = value;
time = value;
render();
});
input2.addEventListener("change", e => {
const value = Math.abs(parseInt(e.target.value));
e.target.value = value;
startIntervalTime = value;
});
createSelect();
createDrag();
}
function createSelect() {
const mp3List = [
{
value: "https://hao.haolingsheng.com/ring/000/995/52513bb6a4546b8822c89034afb8bacb.mp3",
label: "歌曲《是我太傻了》"
},
{
value: "https://hao.haolingsheng.com/ring/000/965/231624ece7e0b9646a9ff9bed66171c4.mp3",
label: "歌曲《告白气球》"
},
{
value: "https://hao.haolingsheng.com/ring/000/993/fc0fab7d5d00c2b345b4f57913df868b.mp3",
label: "歌曲《等你下课》"
},
{
value: "https://hao.haolingsheng.com/ring/000/965/4e42a8159a0f5c25f1515f10f50b22e8.mp3",
label: "歌曲《爱情废柴》"
},
{
value: "https://hao.haolingsheng.com/ring/000/995/f1b408f77bc8d27a6f5665c1be02df5d.mp3",
label: "歌曲《Making Me Feel》"
},
{
value: "https://hao.haolingsheng.com/ring/000/995/6f98624d7ca22b56291a09ea08812c94.mp3",
label: "歌曲《No matter》"
},
{
value: "https://hao.haolingsheng.com/ring/000/970/5704d7c21c42ddab6456f6aa25620960.mp3",
label: "iphone7 铃声"
},
{
value: "https://hao.haolingsheng.com/ring/000/982/3dac89285d5412642006598c09c907af.mp3",
label: "歌曲《Despacito (Remix)》"
},
{
value: "https://hao.haolingsheng.com/ring/000/977/f35a119f8b07cf1f81e091e5c0823d2b.mp3",
label: "歌曲《I Miss You》"
},
{
value: "https://hao.haolingsheng.com/ring/000/977/bfb42d74c4d03597060341477a757579.mp3",
label: "歌曲《光年之外》"
},
{
value: "https://hao.haolingsheng.com/ring/000/995/899004608491de178a7234ab5538feb9.mp3",
label: "歌曲《Next of Kin》"
}
];
const documentFragment = document.createDocumentFragment();
mp3List.forEach(item => {
const option = document.createElement("option");
option.value = item.value;
option.innerText = item.label;
documentFragment.appendChild(option);
});
select.appendChild(documentFragment);
audio = new Audio(select.value || mp3List[0].value);
}
function createDrag(){
let isDrag = false;
// 是否绑定了事件
let isBindEvent = false;
function startDrag (ev) {
// 开始计时了,不能移动
if (timer) return;
isDrag = true;
// 获取旧位置
const disX = ev.clientX - minute.offsetLeft;
const disY = ev.clientY - minute.offsetTop;
function mousemove(e){
if (!isDrag) return;
// 设置新位置
const x = e.clientX - disX - radius;
// 145是指针的高度
const y = radius - (e.clientY - disY - 145);
const sqrt = Math.sqrt(x * x + y * y);
let cos = Math.acos(y / sqrt);
let deg = cos / oneDegValue;
if (x < 0) {
deg = (Math.PI * 2 - cos) / oneDegValue;
}
time = parseInt(parseFloat((deg).toFixed(2)) * 10);
renderElement();
};
function mouseup () {
isDrag = false;
};
if (isBindEvent) return;
isBindEvent = true;
timepiece.addEventListener('mousemove', mousemove);
timepiece.addEventListener('mouseup', mouseup);
// timepiece.addEventListener('touchmove', mouseup);
return false;
}
// minute.addEventListener("touchstart", startDrag)
minute.addEventListener("mousedown", startDrag);
// 处理移出去当前元素后,不能drag
document.addEventListener("mouseout", function(e) {
if (timepiece.contains(e.target)) {
return;
}
isDrag = false;
});
// document.addEventListener("touchend", function(e) {
// if (timepiece.contains(e.target)) {
// return;
// }
// isDrag = false;
// });
};
</script>
</body>
</html>