Remote-Mouse/src/core/static/js/events.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-06-12 03:38:20 +00:00
const socket = io();
2020-06-29 02:55:58 +00:00
let px = 0;
let py = 0;
2020-06-12 03:38:20 +00:00
socket.on('connect', function() {
console.log("Websocket connected...");
});
2020-06-29 02:55:58 +00:00
$(function () {
$.mousedirection();
$("#canvas").on("mousedirection", function (e) {
if (e.direction == "up") {
2020-05-28 18:07:53 +00:00
px = 0;
2020-06-29 02:55:58 +00:00
py = -1;
} else if (e.direction == "down") {
px = 0;
py = 1;
} else if (e.direction == "left") {
px = -1;
py = 0;
} else if (e.direction == "right") {
px = 1;
2020-05-28 18:07:53 +00:00
py = 0;
2020-06-29 02:55:58 +00:00
} else if (e.direction == "top-left") {
px = -1;
py = -1;
} else if (e.direction == "top-right") {
px = 1;
py = -1;
} else if (e.direction == "bottom-left") {
px = -1;
py = 1;
} else if (e.direction == "bottom-right") {
px = 1;
py = 1;
2020-05-28 18:07:53 +00:00
}
2020-06-29 02:55:58 +00:00
updateText (px, py);
});
});
2020-05-28 18:07:53 +00:00
2020-06-29 02:55:58 +00:00
function updateText (px, py) {
const coordsTxt = "X coords: " + px + ", Y coords: " + py;
2020-05-28 18:07:53 +00:00
document.getElementById("cordsText").innerText = coordsTxt;
2020-06-29 02:55:58 +00:00
// doAjax("/update-coords/xy/" + px + "/" + py, "" , "update-coords", "GET");
socket.emit('update_coords', px + "," + py);
2020-05-28 18:07:53 +00:00
}
2020-06-29 02:55:58 +00:00
// Touch events converted to mouse events
function touchHandler(event) {
let touches = event.changedTouches,
first = touches[0],
type = "";
switch(event.type) {
case "touchstart": type = "mousedown"; break;
case "touchmove": type = "mousemove"; break;
case "touchend": type = "mouseup"; break;
default: return;
}
2020-05-28 18:07:53 +00:00
2020-06-29 02:55:58 +00:00
let simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
first.screenX, first.screenY,
first.clientX, first.clientY, false,
false, false, false, 0/*left*/, null);
2020-05-28 18:07:53 +00:00
2020-06-29 02:55:58 +00:00
first.target.dispatchEvent(simulatedEvent);
event.preventDefault();
2020-05-28 18:07:53 +00:00
}
2020-06-29 02:55:58 +00:00
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);