KKosty4ka

OWOT mouse cursors

Feb 2nd, 2025 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.59 KB | Source Code | 0 0
  1. /*
  2. OWOT mouse cursors script by KKosty4ka
  3. version 2
  4. */
  5.  
  6. !function() {
  7.     if (window.hasOwnProperty("mouse_cursors_script")) return;
  8.  
  9.     w.broadcastReceive();
  10.  
  11.     let cursors = {};
  12.     window.mouse_cursors_script = {
  13.         cursors
  14.     };
  15.  
  16.     let myX = null;
  17.     let myY = null;
  18.  
  19.     function createCursor(id, username) {
  20.         let cur = document.body.appendChild(document.createElement("div"));
  21.         cur.style.pointerEvents = "none";
  22.         cur.style.position = "fixed";
  23.         cur.style.opacity = "0.7";
  24.  
  25.         let img = cur.appendChild(document.createElement("img"));
  26.         img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAATCAYAAACk9eypAAAAa0lEQVQoz52R4QoAIQiD3bj3f+XdnxOkTO2EiGTfymVmJrsofruuAEljyG8YQ4yHCcS10UHMmhXEk9MJYvXeDGKXygpxkn2EOP1hh55KBKBPKREhLiZiOFSm5OJuFgQH7HNqM+NB/Lu0zvAClMs1CjxcukEAAAAASUVORK5CYII=";
  27.  
  28.         if (username) {
  29.             let nametag = cur.appendChild(document.createElement("div"));
  30.             nametag.innerText = username;
  31.  
  32.             nametag.style.backgroundColor = "black";
  33.             nametag.style.color = "white";
  34.             nametag.style.padding = "3px";
  35.             nametag.style.borderRadius = "3px";
  36.         }
  37.  
  38.         cursors[id] = cur;
  39.     }
  40.  
  41.     function redrawCursor(id) {
  42.         let x = (cursors[id].owotX * cellW + positionX + owotWidth / 2) / zoomRatio;
  43.         let y = (cursors[id].owotY * cellH + positionY + owotHeight / 2) / zoomRatio;
  44.  
  45.         cursors[id].style.left = `${x}px`;
  46.         cursors[id].style.top = `${y}px`;
  47.     }
  48.  
  49.     w.on("tilesRendered", () => {
  50.         for (let id in cursors) {
  51.             redrawCursor(id);
  52.         }
  53.     });
  54.  
  55.     w.on("cmd", e => {
  56.         if (w.socketChannel === e.sender) return;
  57.         if (!e.data.startsWith("mousecursors")) return;
  58.  
  59.         let msg;
  60.         try {
  61.             msg = JSON.parse(e.data.substring(12));
  62.         } catch {
  63.             return;
  64.         }
  65.  
  66.         if (!msg.hasOwnProperty("kind")) return;
  67.  
  68.         if (msg.kind === "move") {
  69.             if (!msg.hasOwnProperty("x")) return;
  70.             if (typeof(msg.x) !== "number" || !Number.isFinite(msg.x)) return;
  71.             if (!msg.hasOwnProperty("y")) return;
  72.             if (typeof(msg.y) !== "number" || !Number.isFinite(msg.y)) return;
  73.  
  74.             if (!cursors.hasOwnProperty(e.sender)) {
  75.                 createCursor(e.sender, e.username);
  76.             }
  77.  
  78.             cursors[e.sender].owotX = msg.x;
  79.             cursors[e.sender].owotY = msg.y;
  80.  
  81.             redrawCursor(e.sender);
  82.         } else if (msg.kind === "bye") {
  83.             if (!cursors.hasOwnProperty(e.sender)) return;
  84.  
  85.             cursors[e.sender].remove();
  86.             delete cursors[e.sender];
  87.         }
  88.     });
  89.  
  90.     function sendBye() {
  91.         myX = null;
  92.         myY = null;
  93.  
  94.         w.broadcastCommand("mousecursors" + JSON.stringify({
  95.             kind: "bye"
  96.         }));
  97.     }
  98.  
  99.     function sendCoords() {
  100.         if (myX === null || myY === null) return;
  101.  
  102.         w.broadcastCommand("mousecursors" + JSON.stringify({
  103.             kind: "move",
  104.             x: myX,
  105.             y: myY
  106.         }), true);
  107.     }
  108.  
  109.     setInterval(sendCoords, 1000);
  110.  
  111.     function mouseEvent(e) {
  112.         myX = (e.clientX * zoomRatio - owotWidth / 2 - positionX) / cellW;
  113.         myY = (e.clientY * zoomRatio - owotHeight / 2 - positionY) / cellH;
  114.  
  115.         sendCoords();
  116.     }
  117.  
  118.     document.addEventListener("mousemove", mouseEvent);
  119.     document.addEventListener("wheel", mouseEvent);
  120.  
  121.     document.addEventListener("mouseout", sendBye);
  122.     window.addEventListener("beforeunload", sendBye);
  123. }();
Add Comment
Please, Sign In to add comment