2021-10-28 03:54:04 +03:00
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" >
2021-10-28 17:03:04 +03:00
< meta name = "viewport" content = "user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1" > < title > Document< / title >
2021-10-28 03:54:04 +03:00
< script >
2021-10-30 19:20:27 +03:00
"use strict";
const domlog = message => document.body.appendChild(document.createTextNode(message));
window.onerror = e => domlog(` ERROR - ${e} `);
2021-10-29 04:11:15 +03:00
const HOLDING_THRESHOLD_MS = 300;
2021-10-29 16:00:35 +03:00
const SCROLL_X_DAMPENING = 0.05;
const SCROLL_Y_DAMPENING = 0.06;
2021-10-29 02:03:35 +03:00
2021-10-28 03:54:04 +03:00
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 {
2021-10-30 19:20:27 +03:00
constructor(url) {
2021-10-28 03:54:04 +03:00
this.ws = null;
this.log = Logger(["Connection"], ["log"]).log;
this.messageLog = Logger(["Connection", "Message"], ["log"]).log;
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 + ";";
});
this.ws.send(message);
return message;
}
disconnect() {
this.ws.close();
}
}
2021-10-28 17:03:04 +03:00
class TouchpadController {
constructor(connection) {
this.currentMoveX = 0;
this.currentMoveY = 0;
this.lastMoveX = 0;
this.lastMoveY = 0;
this.shouldResetLastMove = false;
2021-10-29 02:03:35 +03:00
this.isInHoldingMode = false;
2021-10-28 17:03:04 +03:00
this.ongoingTouches = {};
2021-10-29 04:11:15 +03:00
this.holdModeTimeout = null;
2021-10-28 17:03:04 +03:00
this.log = Logger(["TouchpadController"], ["log"]).log;
this.connection = connection;
}
2021-10-29 02:03:35 +03:00
getButtonCode(button) {
let buttonCode = 0;
if (button === "right") buttonCode = 1;
return buttonCode;
}
2021-10-30 19:20:27 +03:00
// takes a gesture name and a touch list
// adds the gesture name to the touch and returns false if that touch already had that gesture
_gesture(gestureName, touchList) {
for (let i = 0; i < touchList.length ; i + + ) {
const touch = this.ongoingTouches[touchList[i].identifier];
if (touch.gestured.includes(gestureName)) return false;
touch.gestured.push(gestureName);
}
return true;
}
2021-10-28 17:03:04 +03:00
_sendRelativeMouseMovement(dx, dy) {
if (dx === 0 & & dy === 0)
2021-10-30 19:20:27 +03:00
return false;
2021-10-28 17:03:04 +03:00
this.connection.sendMessage("r", [dx, dy]);
2021-10-30 19:20:27 +03:00
return true;
2021-10-28 17:03:04 +03:00
}
2021-10-29 04:11:15 +03:00
_sendRelativeMouseScroll(dx, dy) {
if (dx === 0 & & dy === 0)
2021-10-30 19:20:27 +03:00
return false;
2021-10-29 04:11:15 +03:00
this.connection.sendMessage("s", [dx, dy]);
2021-10-30 19:20:27 +03:00
return true;
2021-10-29 04:11:15 +03:00
}
2021-10-29 02:03:35 +03:00
_sendMouseButtonDown(button="left") {
this.connection.sendMessage("d", [this.getButtonCode(button)]);
}
_sendMouseButtonUp(button="left") {
this.connection.sendMessage("u", [this.getButtonCode(button)]);
}
2021-10-28 17:03:04 +03:00
2021-10-29 02:03:35 +03:00
_sendSingleClick(button="left") {
2021-10-30 19:20:27 +03:00
this.connection.sendMessage("c", [this.getButtonCode(button)]);
2021-10-28 17:03:04 +03:00
}
bindTo(element) {
element.addEventListener("touchmove", this.onTouchMove.bind(this));
element.addEventListener("touchend", this.onTouchEnd.bind(this));
element.addEventListener("touchstart", this.onTouchStart.bind(this));
}
2021-10-29 14:44:32 +03:00
onTouchMove(event) {
2021-10-30 19:20:27 +03:00
const touches = event.changedTouches;
2021-10-29 14:44:32 +03:00
event.preventDefault();
2021-10-30 19:20:27 +03:00
event.stopPropagation();
2021-10-28 17:03:04 +03:00
const targetTouch = touches[0];
2021-10-29 13:49:59 +03:00
this.currentMoveX = targetTouch.pageX;
this.currentMoveY = targetTouch.pageY;
2021-10-28 17:03:04 +03:00
// When ending a touch and starting a new one in another part of the touchpad,
// the cursor "rubber bands" to that position.
// To solve this, the "last move" parameters are reset when a touch is ended
// (see onTouchEnd())
if (this.shouldResetLastMove) {
this.shouldResetLastMove = false;
this.lastMoveX = this.currentMoveX;
this.lastMoveY = this.currentMoveY;
}
const deltaX = this.currentMoveX - this.lastMoveX;
const deltaY = this.currentMoveY - this.lastMoveY;
this.lastMoveX = this.currentMoveX;
this.lastMoveY = this.currentMoveY;
2021-10-29 04:11:15 +03:00
// if two touches moved at the same time, assume scrolling intent
2021-10-30 19:20:27 +03:00
// if _sendRelativeMouseScroll or _sendRelativeMouseMovement return true, it means that the delta values are non-zero and a packet has been sent to the server
// in that case, the touches will be marked as moved
let shouldMarkTouchesAsMoved = false;
2021-10-29 04:11:15 +03:00
if (touches.length === 2) {
2021-10-30 19:20:27 +03:00
if (
this._sendRelativeMouseScroll(deltaX * SCROLL_X_DAMPENING, deltaY * SCROLL_Y_DAMPENING)
) {
shouldMarkTouchesAsMoved = true;
}
} else if (touches.length === 3 & & this._gesture("GESTURE_RIGHT_CLICK", touches)) {
shouldMarkTouchesAsMoved = true;
this._sendSingleClick("right");
2021-10-29 04:11:15 +03:00
} else {
2021-10-30 19:20:27 +03:00
if (
this._sendRelativeMouseMovement(deltaX, deltaY)
) {
shouldMarkTouchesAsMoved = true;
}
}
if (shouldMarkTouchesAsMoved) {
for (let i = 0; i < touches.length ; i + + ) {
this.ongoingTouches[touches[i].identifier].hasMoved = true;
}
2021-10-29 04:11:15 +03:00
}
2021-10-28 17:03:04 +03:00
}
2021-10-29 14:44:32 +03:00
onTouchEnd(event) {
const changedTouches = event.changedTouches;
event.preventDefault();
2021-10-30 19:20:27 +03:00
event.stopPropagation();
2021-10-28 17:03:04 +03:00
this.shouldResetLastMove = true;
2021-10-30 19:20:27 +03:00
2021-10-29 02:03:35 +03:00
if (changedTouches.length === 1) {
// This is a single tap - left click
if (!this.ongoingTouches[changedTouches[0].identifier].hasMoved) {
this._sendSingleClick("left");
2021-10-30 19:20:27 +03:00
// We were in "holding mode" and now that touch event has ended,
// thus we have to stop holding the left click button
2021-10-29 02:03:35 +03:00
} else if (this.isInHoldingMode) {
this._sendMouseButtonUp("left");
this.isInHoldingMode = false;
}
}
2021-10-30 19:20:27 +03:00
// remove all ended touches
2021-10-28 17:03:04 +03:00
for (let i = 0; i < changedTouches.length ; i + + ) {
const touch = changedTouches[i];
this.ongoingTouches[touch.identifier] = null;
delete this.ongoingTouches[touch.identifier];
}
2021-10-28 03:54:04 +03:00
}
2021-10-28 17:03:04 +03:00
2021-10-29 14:44:32 +03:00
onTouchStart(event) {
const changedTouches = event.changedTouches;
event.preventDefault();
2021-10-30 19:20:27 +03:00
event.stopPropagation();
2021-10-29 13:49:59 +03:00
2021-10-29 04:11:15 +03:00
// Clear the hold mode time out if another touch begins
if (this.holdModeTimeout)
clearTimeout(this.holdModeTimeout);
// If the touch is still unmoved and held for a certain amount of time,
// we will enter "holding mode" which keeps the mouse button held while dragging
// allowing you to, for example, select text or drag a scrollbar
if (changedTouches.length === 1) {
const targetTouch = changedTouches[0];
this.holdModeTimeout = setTimeout(() => {
if (this.ongoingTouches[targetTouch.identifier] & & !this.ongoingTouches[targetTouch.identifier].hasMoved) {
this.isInHoldingMode = true;
this._sendMouseButtonDown("left");
}
}, HOLDING_THRESHOLD_MS);
}
2021-10-28 17:03:04 +03:00
for (let i = 0; i < changedTouches.length ; i + + ) {
const touch = changedTouches[i];
this.ongoingTouches[touch.identifier] = {
identifier: touch.identifier,
clientX: touch.clientX,
clientY: touch.clientY,
2021-10-30 19:20:27 +03:00
hasMoved: false,
gestured: []
2021-10-28 17:03:04 +03:00
};
2021-10-28 04:04:22 +03:00
}
2021-10-28 17:03:04 +03:00
}
}
2021-10-30 19:20:27 +03:00
const connection = new Connection(`ws://${location.host}/gateway`);
2021-10-28 17:03:04 +03:00
const controller = new TouchpadController(connection);
function main() {
connection.connect();
controller.bindTo(document.querySelector(".touchpad"));
2021-10-28 03:54:04 +03:00
}
< / script >
< style >
2021-10-30 19:20:27 +03:00
:root {
--body-bg-color: #2e2e2e;
--accent-bg-color: #d4d3d3;
--accent-color: #949494;
--grayed-text-color: #949494;
--button-accent-color: #3d3d3d;
--selected-bg-color: #2e2e2e;
--hover-bg-color: #4d4d4d;
--card-border-radius: 1rem;
--button-border-radius: 0.5rem;
--main-font-weight: 500;
--fonts-regular: "Noto Sans", "Liberation Sans", sans-serif;
}
body {
font-weight: var(--main-font-weight);
font-family: var(--fonts-regular);
background-color: var(--body-bg-color);
color: var(--accent-bg-color);
margin: 0;
padding: 24px;
}
2021-10-28 03:54:04 +03:00
.touchpad {
2021-10-30 19:20:27 +03:00
height: 30rem;
width: 100%;
background-color: var(--accent-bg-color);
border-radius: var(--card-border-radius);
2021-10-28 03:54:04 +03:00
}
< / style >
< / head >
< body onload = "main();" >
2021-10-28 17:03:04 +03:00
< div class = "touchpad" > < / div >
2021-10-28 03:54:04 +03:00
< / body >
< / html >