EDIT: From two very good answers I have decided to use isometric projection instead of my silly grid of cordinates.
I am doing a game entirely in canvas, and I find it quite troublesome right now.
I felt silly trying to post the grid. Here is the code with the grid on hastebin.com
Since I have all the grid cordinates, I am iterating the 2D array and turn each iteration into a vertex that I compare to the mouse vertex. My goal is to find the vertex closest to the mouse vertex. Instead all I am returning right now is (17,0) every time, no matter where I click. Do you have any idea why?
function mouse_click(ev) {
var x = ev.clientX - c.offsetLeft;
var y = ev.clientY - c.offsetTop;
// Run click_grid
alert(click_grid(x,y));
}
// Find the grid that is nearest the clicked cordinates.
function click_grid(xx,yy){
var result = [];
var result2 = [];
var best = [0,0];
var lastNode = [];
var currentNode = 9000;
for(var y = 0;y < 18; y++){
for(var x = 0; x < 18; x++){
// Distance = |P-E| = |(3,3)-(1,2)| = |(2,1)| = sqrt(2'2+1'2) = sqrt(5) = 2.23
result = [grid.grid_x[y][x] - yy,grid.grid_y[y][x] - xx];
result2 = [result[0] * result[0], result[1] * result[1]];
currentNode = Math.sqrt(result2[0] + result2[1]);
if(currentNode < lastNode){
best = [y,x];
}
lastNode = currentNode;
}
}
return([best]);
}

currentNodeneeds to start "OVER 9000!", it's currently equal to 9000. But seriously, I think this question is a bit too localized for the site. Since it's just a debugging issue with your code. You may want to ask a new question about what the best way to go about this might be, since your current method looks a little overkill. – Byte56 Mar 6 at 22:08