-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
147 lines (108 loc) · 4.64 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
window.addEventListener("load", function() {
var timerCanvas = document.querySelector(".timer");
var context = timerCanvas.getContext("2d");
var timerVideo = document.querySelector(".renderedTimer");
var blocksLogo = new Image();
var peer = new Peer(null, {debug: 2});
var openConnections = [];
var endTime = null;
var note = null;
function parameter(parameter, url = new URL(window.location)) {
if (!(url instanceof URL)) {
url = new URL(url);
}
var searchParams = new URLSearchParams(url.search).get(parameter);
return typeof(searchParams) === "string" ? decodeURIComponent(searchParams) : searchParams;
}
function writeText(text, x, y, size = 32, colour = "white") {
context.font = `${size}px "ReithSans"`;
context.fillStyle = colour;
context.textBaseline = "middle";
context.textAlign = "center";
context.fillText(text, x, y);
}
function render() {
var timeDisplay = "--:--";
var currentTimeDisplay = new Date(Date.now() - (new Date().getTimezoneOffset() * 60_000)).toISOString().substring(11, 16);
if (endTime != null) {
var timeLeft = endTime - Date.now();
var timeString = new Date(Math.abs(timeLeft >= 0 ? timeLeft + 1_000 : timeLeft)).toISOString().substring(14, 19);
if (timeLeft >= 0) {
timeDisplay = `-${timeString}`;
} else {
timeDisplay = `+${timeString}`;
}
}
context.fillStyle = "black";
context.fillRect(0, 0, timerCanvas.width, timerCanvas.height);
context.drawImage(blocksLogo, 16, 16, 128, 128 * (blocksLogo.height / blocksLogo.width));
var timeColour = "white";
if (timeLeft < 0) {
timeColour = "red";
} else if (timeLeft < 30_000) {
timeColour = "yellow";
}
writeText(currentTimeDisplay, timerCanvas.width - 80, 42, 48);
writeText(timeDisplay, timerCanvas.width / 2, timerCanvas.height / 2, 128, timeColour);
writeText(note || "", timerCanvas.width / 2, timerCanvas.height - 64, 48);
requestAnimationFrame(render);
}
function connect(peerId) {
var connection = peer.connect(peerId);
document.querySelector(".connectionMessage").textContent = "Connecting...";
connection.on("open", function() {
document.querySelector(".connectionMessage").textContent = "Connected to host!";
openConnections.push(connection);
});
}
function sync() {
openConnections.forEach(function(connection) {
connection.send({
note,
endTime: endTime != null ? endTime.getTime() : null
});
});
}
document.querySelector("#applyEndTimeButton").addEventListener("click", function() {
endTime = new Date(document.querySelector("#endTime").value);
sync();
});
document.querySelector("#applyNoteButton").addEventListener("click", function() {
note = document.querySelector("#note").value;
sync();
});
document.querySelector("#clearEndTimeButton").addEventListener("click", function() {
endTime = null;
sync();
});
document.querySelector("#clearNoteButton").addEventListener("click", function() {
note = "";
sync();
});
peer.on("open", function() {
document.querySelector(".ownPeerId").textContent = `Own peer ID: ${peer.id}`;
if (parameter("peerId") != null) {
document.querySelector("#connectPeerId").value = parameter("peerId");
connect(parameter("peerId"));
} else {
window.history.replaceState(null, document.title, `${window.location.href.split("?")[0]}?peerId=${encodeURIComponent(peer.id)}`);
}
});
peer.on("error", function(error) {
document.querySelector(".connectionMessage").textContent = error;
});
document.querySelector("#connectButton").addEventListener("click", function() {
connect(document.querySelector("#connectPeerId").value);
});
peer.on("connection", function(connection) {
document.querySelector(".connectionMessage").textContent = "Connected to controller!";
connection.on("data", function(data) {
endTime = data.endTime != null ? new Date(data.endTime) : null;
note = data.note;
});
})
blocksLogo.src = "logo-white.svg";
timerVideo.srcObject = timerCanvas.captureStream();
document.querySelector("#endTime").value = new Date().toISOString().slice(0, 16);
render();
});