2017-04-04 57 views
1

我有一个nodejs应用程序在当前使用for循环的画布内创建网格。我需要帮助编写一个算法来确定我的光标当前位于x和y明智的哪个单元格。网格目前是25x25,我正在跟踪画布内的鼠标移动。而不是给我一个原始的x和y鼠标坐标,我想要一个单元坐标。任何帮助?谢谢!确定一个画布网格的单元格x和y

//START {EXPRESS} 
 
var express = require('express'); 
 
var app = express(); 
 
var serv = require('http').Server(app); 
 
    
 
app.get('/',function(req, res) { 
 
    res.sendFile(__dirname + '/client/index.html'); 
 
}); 
 
app.use('/client',express.static(__dirname + '/client')); 
 
    
 
serv.listen(2000); 
 
console.log("Server started."); 
 

 

 
var io = require('socket.io')(serv,{}); 
 
io.sockets.on('connection', function(socket){ 
 
\t //RETRIEVING THE MOUSE X AND Y COORDINATES 
 
\t socket.on('mouseMove', function(data){ 
 
\t \t console.log(data.x + ", " + data.y); 
 
\t }); 
 

 
});
<div id="gameDiv"> 
 
\t <center> 
 
\t <canvas id="ctx" width="750" height="750" style="border:1px solid #000000;"></canvas> 
 
\t </center> 
 
</div> 
 
\t 
 
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> 
 
\t 
 
<script> 
 
\t //NETWORK VARIABLES 
 
\t var socket = io(); 
 
\t //GETTING THE CANVAS BY ID 
 
\t var canvas = document.getElementById("ctx"); 
 
\t context = canvas.getContext("2d"); 
 
\t 
 
\t //GRID VARIABLES 
 
\t var w=30, 
 
\t h=30, 
 
\t row=25, 
 
\t col=25, 
 
\t x=0, 
 
\t y=0; 
 
\t 
 
\t //LOOP TO DRAW THE GRID 
 
\t for(x=0; x<row; x++){ 
 
\t \t for(y=0; y<col; y++){ 
 
\t \t \t context.strokeRect(w*x,h*y,w,h); 
 
\t \t } 
 
\t } 
 
\t 
 
\t //GETTING THE MOUSE X AND Y 
 
\t function getMousePos(canvas, event){ 
 
\t \t var rect = canvas.getBoundingClientRect(); 
 
\t \t return { 
 
\t \t \t x:event.clientX - rect.left, 
 
\t \t \t y:event.clientY - rect.top 
 
\t \t }; 
 
\t } 
 
\t 
 
\t canvas.addEventListener('mousemove', function(event){ 
 
\t \t var pos = getMousePos(canvas, event); 
 
\t \t var posx = pos.x; 
 
\t \t var posy = pos.y; 
 
\t \t 
 
     socket.emit('mouseMove',{x:x, y:y}); 
 
\t }, false); 
 
\t 
 
\t 
 
\t 
 
</script> 
 

 
</html>

+1

除法x/y坐标由单元格的宽度/高度,然后向下取整以获取单元坐标 – weirdan

回答

0

AH我最终计算出来自己。

该算法是POSX /高度四舍五入 POSY /宽度四舍五入

下面是HTML

<div id="gameDiv"> 
 
\t <center> 
 
\t <canvas id="ctx" width="750" height="750" style="border:1px solid #000000;"></canvas> 
 
\t </center> 
 
</div> 
 
\t 
 
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> 
 
\t 
 
<script> 
 
\t //NETWORK VARIABLES 
 
\t var socket = io(); 
 
\t //GETTING THE CANVAS BY ID 
 
\t var canvas = document.getElementById("ctx"); 
 
\t context = canvas.getContext("2d"); 
 
\t 
 
\t //GRID VARIABLES 
 
\t var w=30, 
 
\t h=30, 
 
\t row=25, 
 
\t col=25, 
 
\t x=0, 
 
\t y=0, 
 
\t cX=0, 
 
\t cY=0; 
 
\t 
 
\t //LOOP TO DRAW THE GRID 
 
\t for(x=0; x<row; x++){ 
 
\t \t for(y=0; y<col; y++){ 
 
\t \t \t context.strokeRect(w*x,h*y,w,h); 
 
\t \t } 
 
\t } 
 
\t 
 
\t //GETTING THE MOUSE X AND Y 
 
\t function getMousePos(canvas, event){ 
 
\t \t var rect = canvas.getBoundingClientRect(); 
 
\t \t return { 
 
\t \t \t x:event.clientX - rect.left, 
 
\t \t \t y:event.clientY - rect.top 
 
\t \t }; 
 
\t } 
 
\t 
 
\t canvas.addEventListener('mousemove', function(event){ 
 
\t \t 
 
\t \t var pos = getMousePos(canvas, event); 
 
\t \t var posx = pos.x; 
 
\t \t var posy = pos.y; 
 
\t \t 
 
\t \t **cX = Math.ceil(posx/30); 
 
\t \t cY = Math.ceil(posy/30);** 
 
\t \t 
 
     socket.emit('mouseMove',{x:cX, y:cY}); 
 
\t }, false); 
 
\t