-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.js
152 lines (130 loc) · 3.61 KB
/
clock.js
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
/**
* @license
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import {html, render, svg} from '../../lit-html.js';
/**
* Adapted from the Ractive.js clock example: http://www.ractivejs.org/examples/clock/
*/
export class LitClock extends HTMLElement {
get date() { return this._date; }
set date(v) { this._date = v; this.invalidate(); }
constructor() {
super();
this.attachShadow({mode: 'open'});
setInterval(() => {
this.date = new Date();
}, 1000);
}
render() {
return html`
<style>
:host {
display: block;
}
.square {
position: relative;
width: 100%;
height: 0;
padding-bottom: 100%;
}
svg {
position: absolute;
width: 100%;
height: 100%;
}
.clock-face {
stroke: #333;
fill: white;
}
.minor {
stroke: #999;
stroke-width: 0.5;
}
.major {
stroke: #333;
stroke-width: 1;
}
.hour {
stroke: #333;
}
.minute {
stroke: #666;
}
.second, .second-counterweight {
stroke: rgb(180,0,0);
}
.second-counterweight {
stroke-width: 3;
}
</style>
<div class='square'> <!-- so the SVG keeps its aspect ratio -->
<svg viewBox='0 0 100 100'>
<!-- first create a group and move it to 50,50 so
all co-ords are relative to the center -->
<g transform='translate(50,50)'>
<circle class='clock-face' r='48'/>
${minuteTicks}
${hourTicks}
<!-- hour hand -->
<line class='hour' y1='2' y2='-20'
transform='rotate(${ 30 * this.date.getHours() + this.date.getMinutes() / 2 })'/>
<!-- minute hand -->
<line class='minute' y1='4' y2='-30'
transform='rotate(${ 6 * this.date.getMinutes() + this.date.getSeconds() / 10 })'/>
<!-- second hand -->
<g transform='rotate(${ 6 * this.date.getSeconds() })'>
<line class='second' y1='10' y2='-38'/>
<line class='second-counterweight' y1='10' y2='2'/>
</g>
</g>
</svg>
</div>
`;
}
invalidate() {
if (!this.needsRender) {
this.needsRender = true;
Promise.resolve().then(() => {
this.needsRender = false;
render(this.render(), this.shadowRoot);
});
}
}
}
customElements.define('lit-clock', LitClock);
const minuteTicks = (() => {
const lines = [];
for (let i = 0; i < 60; i++) {
lines.push(svg`
<line
class='minor'
y1='42'
y2='45'
transform='rotate(${360 * i / 60})'/>
`);
}
return lines;
})();
const hourTicks = (() => {
const lines = [];
for (let i = 0; i < 12; i++) {
lines.push(svg`
<line
class='major'
y1='32'
y2='45'
transform='rotate(${360 * i / 12})'/>
`);
}
return lines;
})();