capybara/public/index.html

143 lines
No EOL
4.6 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
const loggerOfType = (components, type='log') => (...args) => {
let str = '%c';
const style = 'color: #5e81ac; font-weight: bold;';
for (const i in components) {
const v = components[i];
if (components[i+1] === undefined) {
str += `[${v}]`;
} else {
str += `[${v}] `;
}
}
switch (type) {
case 'log': {
console.log(str, style, ...args);
break;
}
case 'error': {
console.error(str, style, ...args);
break;
}
case 'warn': {
console.warn(str, style, ...args);
break;
}
case 'genmsg': {
return str;
}
default: {
return str;
}
}
};
function Logger(components, types=['warn', 'error', 'log']) {
const loggerObj = {};
for (const type of types) {
loggerObj[type] = loggerOfType(components, type);
}
return loggerObj;
}
class Connection {
constructor(url, logMessages=false) {
this.ws = null;
this.log = Logger(["Connection"], ["log"]).log;
this.messageLog = Logger(["Connection", "Message"], ["log"]).log;
this.logMessages = logMessages;
this.url = url;
}
connect() {
this.ws = new WebSocket(this.url);
this.ws.onerror = (e) => this.log("Error", e);
this.ws.onopen = () => {
this.log("Open");
};
this.ws.onclose = () => {
this.log("Closed - attempting to reconnect in 4000ms");
setTimeout(() => this.connect(), 4000);
}
}
sendMessage(code, params=[]) {
let message = code;
params.forEach((param, i) => {
if (i == params.length - 1)
message += param;
else
message += param + ";";
});
if (this.logMessages) this.messageLog(message);
this.ws.send(message);
return message;
}
disconnect() {
this.ws.close();
}
}
let connection = new Connection(`ws://${location.host}/gateway`, true);
function main() {
connection.connect();
const touchpad = document.querySelector(".touchpad");
const movementState = {
currentX: 0,
currentY: 0,
lastCheckedX: 0,
lastCheckedY: 0,
shouldResetTouch: false,
}
touchpad.addEventListener("touchmove", (event) => {
movementState.currentX = event.touches[0].clientX;
movementState.currentY = event.touches[0].clientY;
if (movementState.shouldResetTouch) {
movementState.shouldResetTouch = false;
movementState.lastCheckedX = movementState.currentX;
movementState.lastCheckedY = movementState.currentY;
}
const deltaX = movementState.currentX - movementState.lastCheckedX;
const deltaY = movementState.currentY - movementState.lastCheckedY;
movementState.lastCheckedX = movementState.currentX;
movementState.lastCheckedY = movementState.currentY;
connection.sendMessage("r", [deltaX, deltaY]);
});
touchpad.addEventListener("touchend", () => {
movementState.shouldResetTouch = true;
});
}
</script>
<style>
.touchpad {
width: 75vw;
height: 50vh;
background-color: blue;
}
</style>
</head>
<body onload="main();">
<div class="touchpad">
</div>
<button onClick="connection.sendMessage('r', [20, 20])">Move mouse test</button>
</body>
</html>