forked from tarunsinghofficial/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CREATED]Create An analog watch using react and javascript tarunsingh…
…official#2631 Create An analog watch using react and javascript tarunsinghofficial#2631 Using react and Javascript.
- Loading branch information
1 parent
15bd720
commit 8ea4af3
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
An analog watch using react and javascript #2631 | ||
<br> | ||
Create a basic analog clock with three clock hands (hour, minute, and second). | ||
|
||
Code : | ||
import React, { Component } from 'react'; | ||
import './App.css'; | ||
|
||
class App extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
time: new Date() | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.intervalID = setInterval( | ||
() => this.tick(), | ||
1000 // Update the clock every second | ||
); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.intervalID); | ||
} | ||
|
||
tick() { | ||
this.setState({ | ||
time: new Date() | ||
}); | ||
} | ||
|
||
render() { | ||
const { time } = this.state; | ||
|
||
const secondDeg = ((time.getSeconds() / 60) * 360) - 90; | ||
const minuteDeg = ((time.getMinutes() / 60) * 360) - 90; | ||
const hourDeg = ((time.getHours() % 12) / 12 * 360) + ((time.getMinutes() / 60) * 30) - 90; | ||
|
||
return ( | ||
<div className="clock"> | ||
<div className="hand second-hand" style={{ transform: `rotate(${secondDeg}deg)` }}></div> | ||
<div className="hand minute-hand" style={{ transform: `rotate(${minuteDeg}deg)` }}></div> | ||
<div className="hand hour-hand" style={{ transform: `rotate(${hourDeg}deg)` }}></div> | ||
<div className="center-circle"></div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default App; | ||
|
||
|