Skip to content

Commit 885abbc

Browse files
committed
Completed project 2
1 parent 8207d2b commit 885abbc

File tree

3 files changed

+102
-14
lines changed

3 files changed

+102
-14
lines changed

Diff for: 02 - JS and CSS Clock/index-START.html

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>JS + CSS Clock</title>
67
</head>
8+
79
<body>
810

911

10-
<div class="clock">
11-
<div class="clock-face">
12-
<div class="hand hour-hand"></div>
13-
<div class="hand min-hand"></div>
14-
<div class="hand second-hand"></div>
15-
</div>
12+
<div class="clock">
13+
<div class="clock-face">
14+
<div class="hand hour-hand"></div>
15+
<div class="hand min-hand"></div>
16+
<div class="hand second-hand"></div>
1617
</div>
18+
</div>
1719

1820

1921
<style>
@@ -43,17 +45,18 @@
4345
position: relative;
4446
padding: 2rem;
4547
box-shadow:
46-
0 0 0 4px rgba(0,0,0,0.1),
48+
0 0 0 4px rgba(0, 0, 0, 0.1),
4749
inset 0 0 0 3px #EFEFEF,
4850
inset 0 0 10px black,
49-
0 0 10px rgba(0,0,0,0.2);
51+
0 0 10px rgba(0, 0, 0, 0.2);
5052
}
5153

5254
.clock-face {
5355
position: relative;
5456
width: 100%;
5557
height: 100%;
56-
transform: translateY(-3px); /* account for the height of the clock hands */
58+
transform: translateY(-3px);
59+
/* account for the height of the clock hands */
5760
}
5861

5962
.hand {
@@ -62,13 +65,17 @@
6265
background: black;
6366
position: absolute;
6467
top: 50%;
68+
transform-origin: 100%;
69+
transform: rotate(90deg);
70+
transition: transform 0.05s cubic-bezier(0.1, 2.7, 0.58, 1);
6571
}
6672

73+
.second-hand {
74+
background-color: darkred;
75+
}
6776
</style>
6877

69-
<script>
70-
71-
72-
</script>
78+
<script src="typescripts.js"></script>
7379
</body>
74-
</html>
80+
81+
</html>

Diff for: 02 - JS and CSS Clock/typescripts.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var secHand = document.querySelector('.second-hand');
2+
var minHand = document.querySelector('.min-hand');
3+
var hourHand = document.querySelector('.hour-hand');
4+
function setDateLoop() {
5+
var now = new Date();
6+
var seconds = now.getSeconds();
7+
var secondsDegrees = (seconds / 60) * 360 + 90;
8+
if (secondsDegrees === 90) {
9+
secHand.style.transition = 'none';
10+
}
11+
else {
12+
secHand.style.transition = 'transform 0.05s cubic-bezier(0.1, 2.7, 0.58, 1)';
13+
}
14+
secHand.style.transform = "rotate(" + secondsDegrees + "deg)";
15+
var mins = now.getMinutes();
16+
var minsDegrees = (mins / 60) * 360 + 90;
17+
if (minsDegrees === 90) {
18+
minHand.style.transition = 'none';
19+
}
20+
else {
21+
minHand.style.transition = 'transform 0.05s cubic-bezier(0.1, 2.7, 0.58, 1)';
22+
}
23+
minHand.style.transform = "rotate(" + minsDegrees + "deg)";
24+
var hours = now.getHours();
25+
var hoursDegrees = (hours / 12) * 360 + 90;
26+
if (hoursDegrees === 90) {
27+
hourHand.style.transition = 'none';
28+
}
29+
else {
30+
hourHand.style.transition = 'transform 0.05s cubic-bezier(0.1, 2.7, 0.58, 1)';
31+
}
32+
hourHand.style.transform = "rotate(" + hoursDegrees + "deg)";
33+
window.requestAnimationFrame(setDateLoop);
34+
}
35+
setDateLoop();

Diff for: 02 - JS and CSS Clock/typescripts.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* JavaScript30 by Wes Bos, https://javascript30.com/
3+
* TypeScript implementation by Will Wager
4+
* Project: TS and CSS Clock
5+
* Concepts: Transforms and transitions
6+
* Key takeaways: Using Date, updating CSS with TS.
7+
* Sidenotes: Disable transition on the reset step to avoid flicker.
8+
*/
9+
10+
const secHand: HTMLElement = document.querySelector('.second-hand');
11+
const minHand: HTMLElement = document.querySelector('.min-hand');
12+
const hourHand: HTMLElement = document.querySelector('.hour-hand');
13+
14+
function setDateLoop(): void {
15+
const now = new Date();
16+
const seconds = now.getSeconds();
17+
const secondsDegrees = (seconds / 60) * 360 + 90;
18+
if (secondsDegrees === 90) {
19+
secHand.style.transition = 'none';
20+
} else {
21+
secHand.style.transition = 'transform 0.05s cubic-bezier(0.1, 2.7, 0.58, 1)';
22+
}
23+
secHand.style.transform = `rotate(${secondsDegrees}deg)`;
24+
25+
const mins = now.getMinutes();
26+
const minsDegrees = (mins / 60) * 360 + 90;
27+
if (minsDegrees === 90) {
28+
minHand.style.transition = 'none';
29+
} else {
30+
minHand.style.transition = 'transform 0.05s cubic-bezier(0.1, 2.7, 0.58, 1)';
31+
}
32+
minHand.style.transform = `rotate(${minsDegrees}deg)`;
33+
34+
const hours = now.getHours();
35+
const hoursDegrees = (hours / 12) * 360 + 90;
36+
if (hoursDegrees === 90) {
37+
hourHand.style.transition = 'none';
38+
} else {
39+
hourHand.style.transition = 'transform 0.05s cubic-bezier(0.1, 2.7, 0.58, 1)';
40+
}
41+
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
42+
43+
window.requestAnimationFrame(setDateLoop);
44+
}
45+
46+
setDateLoop();

0 commit comments

Comments
 (0)