Skip to content

Commit 17a4b47

Browse files
authored
Uploaded Clock
Clock should be accessible through samsimmons.info/clock
1 parent 015e107 commit 17a4b47

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

clock/clock.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
html, body {
2+
margin: 0;
3+
padding: 0;
4+
height: 100%;
5+
display: flex;
6+
justify-content: center;
7+
align-items: center;
8+
}
9+
10+
canvas {
11+
12+
}

clock/clock.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
7+
<title>Sketch</title>
8+
9+
<link rel="stylesheet" type="text/css" href="clock.css">
10+
11+
<script src="libraries/p5.min.js"></script>
12+
<script src="sketch.js"></script>
13+
</head>
14+
15+
<body>
16+
<script src="sketch.js"></script>
17+
</body>
18+
</html>

clock/sketch.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
function setup() {
2+
createCanvas(min(windowWidth, windowHeight), min(windowWidth, windowHeight));
3+
sinceLastSecond = 0;
4+
lastSecond = second();
5+
clockSpeed = 1;
6+
radius = width / 2;
7+
}
8+
9+
function windowResized() {
10+
resizeCanvas(min(windowWidth, windowHeight), min(windowWidth, windowHeight))
11+
radius = width / 2;
12+
}
13+
14+
function getAngle(p1x, p1y, p2x, p2y) {
15+
let angle = acos((p2x - p1x) / dist(p1x, p1y, p2x, p2y))
16+
return (p1y - p2y < 0)? angle : -angle;
17+
}
18+
19+
function addVectors(vec1, vec2) {
20+
let posx = vec1[0] * cos(vec1[1]) + vec2[0] * cos(vec2[1])
21+
let posy = vec1[0] * sin(vec1[1]) + vec2[0] * sin(vec2[1])
22+
return ([
23+
dist(0, 0, posx, posy),
24+
getAngle(0, 0, posx, posy)
25+
])
26+
}
27+
28+
function calculateVectorAndColor(xx, yy, gravPoints) {
29+
let vecList = [];
30+
let myColor = color(0, 0, 0);
31+
let currentTotalInfluence = 0;
32+
for (let i = 0; i < gravPoints.length; i += 1) {
33+
vecList.push([
34+
constrain(pow(dist(xx, yy, gravPoints[i][0], gravPoints[i][1]), -2.2) * gravPoints[i][2], 0, 1),
35+
getAngle(xx, yy, gravPoints[i][0], gravPoints[i][1])
36+
]);
37+
currentTotalInfluence += vecList[i][0];
38+
myColor = lerpColor(myColor, gravPoints[i][3], vecList[i][0] / currentTotalInfluence);
39+
}
40+
function sumVectors(total, currentValue) {
41+
return addVectors(total, currentValue);
42+
}
43+
return ([vecList.reduce(sumVectors, [0, 0]), myColor]);
44+
}
45+
46+
function draw() {
47+
background(255);
48+
if (lastSecond != second()) {
49+
lastSecond = second();
50+
sinceLastSecond = deltaTime;
51+
} else {
52+
sinceLastSecond += deltaTime;
53+
}
54+
let hourAngle = ((hour() % 12) + minute() / 60 + second() / 3600 + sinceLastSecond / 3600000) / 12 * clockSpeed * 2 * PI - PI / 2;
55+
let minuteAngle = (minute() + second() / 60 + sinceLastSecond / 60000) / 60 * clockSpeed * 2 * PI - PI / 2;
56+
let secondAngle = (second() + sinceLastSecond / 1000) / 60 * clockSpeed * 2 * PI - PI / 2;
57+
let gravPoints = [
58+
[cos(hourAngle) * radius + radius, sin(hourAngle) * radius + radius, 15000, color(255, 0, 0)],
59+
[cos(minuteAngle) * (radius * 3 / 4) + radius, sin(minuteAngle) * (radius * 3 / 4) + radius, 2000, color(0, 255, 0)],
60+
[cos(secondAngle) * (radius / 2) + radius, sin(secondAngle) * (radius / 2) + radius, 600, color(0, 0, 255)]
61+
];
62+
for (let xx = 5; xx < radius * 2; xx += 5) {
63+
for (let yy = 5; yy < radius * 2; yy += 5) {
64+
if ((dist(radius, radius, xx, yy) < radius)) {
65+
let pointInfo = calculateVectorAndColor(xx, yy, gravPoints);
66+
let vec = pointInfo[0];
67+
stroke(pointInfo[1]);
68+
strokeWeight(vec[0]);
69+
line(xx, yy, xx + cos(vec[1]) * 3 * vec[0], yy + sin(vec[1]) * 3 * vec[0]);
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)