diff --git a/water.js b/water.js index 8e002a8..36a9385 100644 --- a/water.js +++ b/water.js @@ -38,12 +38,23 @@ function follow_mouse(event) { var mouse_x = event.clientX var mouse_y = event.clientY - // convert screen coordinates to grid coordinates - var grid_x = (mouse_x - grid_margin - offset_h).div(grid_size); - var grid_y = (mouse_y - grid_margin - offset_v).div(grid_size); + // find the screen-space coordinates of the nearest grid cell + var mouse_x_within_grid = mouse_x - offset_h + var mouse_y_within_grid = mouse_y - offset_v + var cell_center_x = mouse_x_within_grid - (grid_margin/2) - (grid_size/2) + var cell_center_y = mouse_y_within_grid - (grid_margin/2) - (grid_size/2) + + // divide and round to get the cell coordinates + var grid_x = Math.round(cell_center_x / grid_size) + var grid_y = Math.round(cell_center_y / grid_size) // draw a square there - draw_grid_square(grid_y, grid_x, 'white'); + water_cells.push({ + row: grid_x, + col: grid_y, + energy: 1.0 + }) + //draw_grid_square(grid_y, grid_x, '#6485ef'); } // update and draw all walkers in the array @@ -86,6 +97,7 @@ function add_random_direction(walker) { ****************************************************************************/ // from https://www.sitepoint.com/javascript-generate-lighter-darker-color/ +// lum: -0.1 is 10% darker, 0.2 is 20% brighter function tweak_color_luminance(hex, lum) { // validate hex string hex = String(hex).replace(/[^0-9a-f]/gi, ''); @@ -152,6 +164,12 @@ var n_cols = canvas.width.div(grid_size); var offset_h = (canvas.width - (grid_size * n_cols)).div(2); var offset_v = (canvas.height - (grid_size * n_rows)).div(2); +// array of cells. each one will have +// - row and column of the grid +// - energy (0 to 1) +// - TODO list add others if I make it more complex +var water_cells[]; + // away we go! init();