-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock-element.html
146 lines (135 loc) · 3.8 KB
/
clock-element.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
<link rel="import" href="../polymer/polymer.html">
<dom-module id="clock-element">
<style>
:host ::content {
display: block;
width: 100px;
height: 100px;
position: relative;
border: 6px solid black;
border-radius: 50%;
}
.clock-element-container::after {
position: absolute;
top: 50%;
left: 50%;
width: 12px;
height: 12px;
margin: -6px 0 0 -6px;
background: black;
border-radius: 6px;
content: "";
display: block;
}
.clock-element-container > div {
position: absolute;
top: 50%;
left: 50%;
background: black;
}
.clock-element-hour {
margin: -4px 0 -4px -25%;
padding: 4px 0 4px 25%;
-ms-transform-origin: 100% 50%;
-webkit-transform-origin: 100% 50%;
transform-origin: 100% 50%;
border-radius: 4px 0 0 4px;
}
.clock-element-minute {
margin: -40% -3px 0;
padding: 40% 3px 0;
-ms-transform-origin: 50% 100%;
-webkit-transform-origin: 50% 100%;
transform-origin: 50% 100%;
border-radius: 3px 3px 0 0;
}
.clock-element-second {
margin: -40% -1px 0 0;
padding: 40% 1px 0;
-ms-transform-origin: 50% 100%;
-webkit-transform-origin: 50% 100%;
transform-origin: 50% 100%;
}
</style>
<template>
<div class='clock-element-container' >
<div class='clock-element-hour'></div>
<div class='clock-element-minute'></div>
<div class='clock-element-second'></div>
</div>
</template>
</dom>
<script>
'use strict';
Polymer({
is: 'clock-element',
properties: {
hour:{
type: Number,
notify: true,
value: function() {
return this.hour;
}
},
minute:{
type: Number,
notify: true,
value: function() {
return this.minute;
}
},
second:{
type: Number,
notify: true,
value: function() {
return this.second;
}
}
},
observers: ['attributeChangedObserver(hour,minute,second)'],
updateClock: function() {
var now = new Date(),
hour = this.hour || now.getHours(),
minute = this.minute || now.getMinutes(),
second = this.second || now.getSeconds(),
secondAngle = second * 6,
minuteAngle = minute * 6 + secondAngle / 60,
hourAngle = ( ( hour % 12 ) / 12 ) * 360 + 90 + minute / 12,
hourRule = "rotate(" + hourAngle + "deg)",
minuteRule = "rotate(" + minuteAngle + "deg)",
secondRule = "rotate(" + secondAngle + "deg)";
this.hourElement.style.msTransform = hourRule;
this.hourElement.style.webkitTransform = hourRule;
this.hourElement.style.transform = hourRule;
this.minuteElement.style.msTransform = minuteRule;
this.minuteElement.style.webkitTransform = minuteRule;
this.minuteElement.style.transform = minuteRule;
this.secondElement.style.msTransform = secondRule;
this.secondElement.style.webkitTransform = secondRule;
this.secondElement.style.transform = secondRule;
},
ready: function() {
this.hourElement = this.querySelector( ".clock-element-hour" );
this.minuteElement = this.querySelector( ".clock-element-minute" );
this.secondElement = this.querySelector( ".clock-element-second" );
this.updateClock();
if ( !this.hour && !this.minute && !this.second ) {
this.interval = setInterval(function() {
this.updateClock();
}.bind(this), 1000 );
}
},
readAttributes : function() {
this.hour = this.getAttribute( "hour" );
this.minute = this.getAttribute( "minute" );
this.second = this.getAttribute( "second" );
},
attributeChangedObserver : function( attrName, oldVal, newVal ) {
console.log(attrName, oldVal, newVal );
if ( /hour|minute|second/.test( attrName ) ) {
this.readAttributes();
this.updateClock();
}
}
})
</script>