-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
185 lines (144 loc) · 5.42 KB
/
index.html
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=0.1">
<link rel="stylesheet" href="main.css">
</head>
<body style="margin: 0px; background: #0e0e0e; background: white;">
<canvas id="status-indicator"></canvas>
<button id="close-button">x</button>
<div class="c-camera">
<canvas id="camera-canvas"></canvas>
</div>
</body>
<!-- JavaScript scripts -->
<script>
/* Electron setups */
const isReachable = require('is-reachable');
let ipc = require("electron").ipcRenderer;
function init() {
// Quit with the x button
document.getElementById("close-button").addEventListener("click", function (e) {
console.log("close");
ipc.send("win.close");
});
};
// document.onreadystatechange = function () {
// if (document.readyState == "complete") {
// init();
// }
// };
init();
</script>
<script>
/* Camera drawing setups */
/* Change this number to your team number! */
const TEAM_NUMBER = "1720";
// Pad the team number for the IP address
let team_num = "0".repeat(4-TEAM_NUMBER.length) + TEAM_NUMBER;
let team_ip = team_num[0] + team_num[1] + "." + team_num[2] + team_num[3];
// The drawing plane
const cameraCanvas = document.getElementById("camera-canvas");
let camCtx = cameraCanvas.getContext('2d');
camCtx.canvas.width = window.innerWidth;
camCtx.canvas.height = window.innerHeight;
// Connection status. Red if not connected, green if connected.
const statusIndicator = document.getElementById("status-indicator");
let stCtx = statusIndicator.getContext('2d');
stCtx.canvas.width = 25;
stCtx.canvas.height = 400;
stCtx.fillStyle = "rgb(255, 0, 0)";
stCtx.fillRect(0, 0, stCtx.canvas.width, stCtx.canvas.height);
let { width, height } = cameraCanvas;
const lineThickness = 4.0;
const lineThicknessHalf = lineThickness / 2.0;
let midPoint = {
width: width / 2.0,
height: height / 2.0,
};
let img = new Image(); // This is where the camera feed goes
// Used for resizing and centering the image
let scale = 1;
let imageX = 0;
let imageY = 0;
let source = "";
let disconnected = false;
// Connect to the camera
(async () => {
let sourceMain = "http://10." + team_ip + ".2:1181"; // IP is team number dependent
let sourceUSB = "http://172.22.11.2:1181"; // Always the same
// Wait until first contact
while (true) {
if (await isReachable(sourceMain)) {
source = sourceMain + "/?action=stream";
break;
} else if (await isReachable(sourceUSB)) {
source = sourceUSB + "/?action=stream";
break;
}
console.log("No camera sources available!");
}
setup(source);
// Green for go!
stCtx.fillStyle = "rgb(0, 255, 0)";
stCtx.fillRect(0, 0, stCtx.canvas.width, stCtx.canvas.height);
})();
function setup (source) {
console.log("source: ", source);
img.src = source;
img.decode().then(() => { updateScale(); })
window.addEventListener('resize', updateScale);
window.setInterval("refreshCanvas()", 4);
}
function refreshCanvas () {
camCtx.globalCompositeOperation = "source-over";
camCtx.scale(scale, scale);
camCtx.fillStyle = "rgba(255, 255, 255, 1)";
camCtx.fillRect(0, 0, camCtx.canvas.width, camCtx.canvas.height);
try {
camCtx.drawImage(img, imageX, imageY);
if (disconnected) {
stCtx.fillStyle = "rgb(255, 255, 0)";
stCtx.fillRect(0, 0, stCtx.canvas.width, stCtx.canvas.height);
disconnected = false;
}
} catch {
img.src = source;
console.log(source);
console.log("stream unavailable");
disconnected = true;
stCtx.fillStyle = "rgb(255, 0, 255)";
stCtx.fillRect(0, 0, stCtx.canvas.width, stCtx.canvas.height);
}
camCtx.globalCompositeOperation = "difference";
camCtx.scale(1/scale, 1/scale);
camCtx.fillStyle = "rgba(127, 127, 127, 1)";
camCtx.fillRect(midPoint.width - lineThicknessHalf, 0.0, lineThickness, height);
camCtx.fillRect(0.0, midPoint.height - lineThicknessHalf, width, lineThickness);
camCtx.fillRect(midPoint.width - lineThicknessHalf, midPoint.height - lineThicknessHalf, lineThickness, lineThickness);
};
// function imageLoad () {
// }
function updateScale () {
camCtx.canvas.width = window.innerWidth;
camCtx.canvas.height = window.innerHeight;
width = cameraCanvas.width;
height = cameraCanvas.height;
midPoint = {
width: width / 2.0,
height: height / 2.0,
};
widthScale = camCtx.canvas.width / img.width;
heightScale = camCtx.canvas.height / img.height;
scale = widthScale > heightScale ? heightScale : widthScale;
imageX = 0;
imageY = 0;
if (widthScale > heightScale) {
imageX = camCtx.canvas.width - (img.width * scale);
imageX /= scale * 2;
} else {
imageY = camCtx.canvas.height - (img.height * scale);
imageY /= scale * 2;
}
};
</script>
</html>