Skip to content

Instantly share code, notes, and snippets.

@kacpak
Last active February 25, 2017 20:06
Show Gist options
  • Save kacpak/5a54403ad0593925a64f983def0e0902 to your computer and use it in GitHub Desktop.
Save kacpak/5a54403ad0593925a64f983def0e0902 to your computer and use it in GitHub Desktop.
Watch
// zmienne
var canvas, ctx;
var clockImage;
var startDate;
function clear() {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
function drawClock(date, x, y, width, height, city) {
var size = Math.min(width, height);
var maxSize = Math.min(ctx.canvas.width, ctx.canvas.height);
var getRelativeSize = function(n) {
return Math.ceil(n * size / maxSize);
};
// Dostosowanie rozmiarów
var theta, hX, hY, weight;
var clockRadius = size / 2;
var fontSize = getRelativeSize(36);
var offset = getRelativeSize(15);
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
// Godzina
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
hours = hours > 12 ? hours - 12 : hours;
var hour = hours + minutes / 60;
var minute = minutes + seconds / 60;
// Zapisanie oryginalnego stanu
ctx.save();
// Tarcza zegara
ctx.drawImage(clockImage, x, y, width, height);
ctx.translate(x + width / 2, y + height / 2);
// Miasto
if (city !== undefined) {
ctx.font = (fontSize + 4) + 'px Arial';
ctx.fillStyle = '#000';
ctx.fillText(city, 0, height / 2 + 8);
}
// Godziny na tarczy
ctx.font = fontSize + 'px Arial';
ctx.fillStyle = '#000';
for (var n = 1; n <= 12; n++) {
theta = (n - 3) * (Math.PI * 2) / 12;
hX = clockRadius * 0.7 * Math.cos(theta);
hY = clockRadius * 0.7 * Math.sin(theta);
ctx.fillText(n, hX, hY);
}
// Wskazówka godzinowa
ctx.save();
weight = getRelativeSize(5);
theta = (hour - 3) * 2 * Math.PI / 12;
ctx.rotate(theta);
ctx.beginPath();
ctx.moveTo(-offset, -weight);
ctx.lineTo(-offset, weight);
ctx.lineTo(clockRadius * 0.5, 1);
ctx.lineTo(clockRadius * 0.5, -1);
ctx.fill();
ctx.restore();
// Wskazówka minutowa
ctx.save();
weight = getRelativeSize(4);
theta = (minute - 15) * 2 * Math.PI / 60;
ctx.rotate(theta);
ctx.beginPath();
ctx.moveTo(-offset, -weight);
ctx.lineTo(-offset, weight);
ctx.lineTo(clockRadius * 0.8, 1);
ctx.lineTo(clockRadius * 0.8, -1);
ctx.fillStyle = '#040';
ctx.fill();
ctx.restore();
// Wskazówka sekundowa
ctx.save();
weight = getRelativeSize(5);
theta = (seconds - 15) * 2 * Math.PI / 60;
ctx.rotate(theta);
ctx.beginPath();
ctx.moveTo(-offset, -weight);
ctx.lineTo(-offset, weight);
ctx.lineTo(clockRadius * 0.9, 1);
ctx.lineTo(clockRadius * 0.9, -1);
ctx.fillStyle = '#0f0';
ctx.fill();
ctx.restore();
// Przywrócenie oryginalnego stanu
ctx.restore();
}
function pad(n) {
return n < 10 ? '0'+n : n;
}
function drawStopwatch(x, y, width, height) {
ctx.font = '52px Arial';
ctx.fillStyle = '#000';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
var dateNow = new Date();
var time = dateNow.getTime() - startDate.getTime();
var h = Math.floor( time / (60 * 60 * 1000) );
time = time % (60 * 60 * 1000);
var m = Math.floor( time / (60 * 1000) );
time = time % (60 * 1000);
var s = Math.floor( time / 1000 );
var formatedTime = pad(h) + ':' + pad(m) + ':' + pad(s);
ctx.save();
ctx.fillText(formatedTime, x + width / 2, y + height / 2);
ctx.restore();
}
function getUtcDate(offset) {
var date = new Date();
var utcMs = date.getTime() + (date.getTimezoneOffset() * 60000);
return new Date(utcMs + (3600000 * offset));
}
function drawScene() {
clear();
var dateLocal = new Date();
var dateNewYork = getUtcDate(-4);
var dateLondon = getUtcDate(1);
var dateTokyo = getUtcDate(9);
// Lewy mały zegar - New York
drawClock(dateNewYork, 100, 250, 100, 100, 'New York -4');
// Środkowy mały zegar - Londyn
drawClock(dateLondon, 200, 290, 100, 100, 'London +1');
// Prawy mały zegar - Tokyo
drawClock(dateTokyo, 300, 250, 100, 100, 'Tokyo +9');
// Zegar czasu lokalnego (główny)
drawClock(dateLocal, 0, 0, 500, 500);
// Stoper
drawStopwatch(0, 500, 500, 100);
}
$(function(){
startDate = new Date();
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
clockImage = new Image();
clockImage.src = 'images/cface.png';
setInterval(drawScene, 1000);
});
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>Zegar</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="clock.js"></script>
</head>
<body>
<header>
<h2>Zegar HTML 5</h2>
</header>
<div class="clocks">
<canvas id="canvas" width="500" height="600"></canvas>
</div>
</body>
</html>
*{
margin:0;
padding:0;
}
body {
background-color:#eee;
color:#fff;
font:14px/1.3 Arial,sans-serif;
}
header {
background-color:#212121;
box-shadow: 0 -1px 2px #111111;
display:block;
height:70px;
position:relative;
width:100%;
z-index:100;
}
header h2 {
font-size:22px;
font-weight:normal;
left:50%;
margin-left:-400px;
padding:22px 0;
position:absolute;
width:540px;
}
header a.stuts,a.stuts:visited {
border:none;
text-decoration:none;
color:#fcfcfc;
font-size:14px;
left:50%;
line-height:31px;
margin:23px 0 0 110px;
position:absolute;
top:0;
}
header .stuts span {
font-size:22px;
font-weight:bold;
margin-left:5px;
}
.clocks {
height: 500px;
margin: 25px auto;
position: relative;
width: 500px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment