Skip to content

Instantly share code, notes, and snippets.

@rbiggs
Last active August 27, 2017 00:12
Show Gist options
  • Save rbiggs/d6b8b9c1cc181e379aff7dcc63cbba5f to your computer and use it in GitHub Desktop.
Save rbiggs/d6b8b9c1cc181e379aff7dcc63cbba5f to your computer and use it in GitHub Desktop.
SVG Seconds Clock with Composi
import {h, Component} from 'composi'
class Clock extends Component {
constructor(props) {
super(props)
this.state = {time: Date.now()}
this.styles = {
'p > span': {
fontFamily: 'Courier, Monospace'
}
}
}
render() {
let time = this.state.time
const angle = (time) => 2 * Math.PI * time / 60000
return (
<li>
<div>
<svg viewBox="0 0 100 100" width="150px">
<circle cx="50" cy="50" r="45" fill="blue" />
<line
stroke-width="2"
x1="50"
y1="50"
x2={ 50 + 40 * Math.cos(angle(time)) }
y2={ 50 + 40 * Math.sin(angle(time)) }
stroke="white"
/>
</svg>
<p>The time: <span>{ new Date(time).getHours() > 12 ? new Date(time).getHours() -12 : new Date(time).getHours() }:{ new Date(time).getMinutes() }:{ new Date(time).getSeconds() < 10 ? '0' + new Date(time).getSeconds() : new Date(time).getSeconds()} { new Date(time).getHours() > 12 ? 'PM' : 'AM' }</span></p>
</div>
</li>
)
}
tick() {
this.setState({time: new Date()})
}
componentWasCreated() {
this.timeID = setInterval(() => { this.tick() }, 1000)
}
}
const clock = new Clock({
root: '#clock'
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="msapplication-tap-highlight" content="no">
<title>SVG Clock</title>
<link rel="stylesheet" href="./css/chui-ios.css">
</head>
<body>
<ui-screen class="current" id="main">
<nav>
<h1>SVG Clock</h1>
</nav>
<section>
<h2>Counting Seconds</h2>
<ul id="clock" class="list"></ul>
</section>
</ui-screen>
<script src="js/app.js"></script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment