var websocket = new WebSocket("wss://hjdczy.top:2345/front"); websocket.onopen = function (evt) { console.log("Connected to WebSocket server."); //使得id = sockettimedout隐藏 document.getElementById('sockettimedout').style.display = 'none'; }; websocket.onclose = function (evt) { console.log("Disconnected"); //使得id = sockettimedout显示 document.getElementById('sockettimedout').style.display = 'block'; }; websocket.onerror = function (evt) { console.log("Error to connect to WebSocket server"); }; // //引入vue3 // import { createApp } from 'vue' // import Mouse from './Mouse.vue' var coord_k = 1.806 //原坐标1.806代表地图1像素 var images = {}; let zoom = 0; let touchPoints = false; let mousePosition = { x: 0, y: 0 }; let devicetype = 'mouse'; // 检测是否为触摸设备 function isTouchDevice() { return 'ontouchstart' in window || navigator.maxTouchPoints > 0; } // 检测是否为鼠标设备 function isMouseDevice() { return 'onmousemove' in window || 'onmousedown' in window; } // 输出设备类型检测结果 function outputDeviceType() { const touch = isTouchDevice(); const mouse = isMouseDevice(); if (touch && mouse) { console.log('当前设备既支持触摸也支持鼠标'); devicetype = 'touch&mouse'; } else if (touch) { console.log('当前设备是触摸设备'); devicetype = 'touch'; } else if (mouse) { console.log('当前设备是鼠标设备'); devicetype = 'mouse'; } else { console.log('当前设备既不支持触摸也不支持鼠标'); devicetype = 'none'; } } // 调用函数输出设备类型 outputDeviceType(); // 全局监听 mousemove 事件,保存鼠标的位置 document.onmousemove = function (e) { mousePosition.x = e.clientX; mousePosition.y = e.clientY; } function getimagepx (x,y){ //从游戏坐标转换到图片坐标 //原图片为12000*12000像素 //游戏坐标原点位于地图的6506,6277 //玩家在地图中的像素 x 为 玩家X/1.806 + 6506 //对于GTA,右上为XY正方向 //玩家在地图中的像素 y 为 6277 + 玩家Y/1.806 //地图左上为0,右下为正 //经修正,修正为6502,6275 if (zoom == 3) { return [x/coord_k + 6502, (0-y)/coord_k+6275]; } else if (zoom == 2) { return [(x/coord_k + 6502)/2, (6277 + (0-y)/coord_k)/2]; } else if (zoom == 1) { return [(x/coord_k + 6502)/4, (6277 + (0-y)/coord_k)/4]; } else { return [(x/coord_k + 6502)/8, (6277 + (0-y)/coord_k)/8]; } } var playersData = []; var labelsoffset = {}; function getgamecoord (x,y){ return [(x - 6506) * coord_k, -((y - 6277) * coord_k)]; } function db_refresh() { //通过websocket发送消息 websocket.send(JSON.stringify({type: 'refresh'})); } var mapContainer = document.getElementById('mapContainer'); // 获取地图容器 var center = [0, 0]; let playerlist = {}; websocket.onmessage = function (evt) { let data = JSON.parse(evt.data); if (data.type == 'ident'){ // console.log(data); ident(data.playername, data.playerserverid); } if (!Array.isArray(data)) { data = [data]; } // console.log(data); playersData = data; //更新玩家数据表,给