Then we set img.onclick to a function that gets the x and y coordinates of the mouse click in the page with e.pageX - e.target.offsetLeft and e.pageY - e.target.offsetTop. I looked at Raycasting but I’m not entirely sure if that is the path I should follow. to select the img element with querySelector. I’m not focusing on mouse events against children in the scene (a new design would have no children anyway), only the (theoretical infinite +X and -Y) coordinates in the scene where the mouse is hovering/clicked. I have tried several different examples to get the +X and -Y position of the mouse (with respect to the origin of the scene) as 3D coordinates and not 2D DOM coordinates since some design elements would have a position X,Y (CSS top,left) greater than the current view-port of the device they were on. This way my end users would not have to re-learn or re-design their projects that were based on the 0,0 index always being in the top-left, rather than the center. Then I would just multiply the -Y by -1 to get a +Y and divide +X and +Y by 12 and floor each to get the corresponding cell index as if it were still an HTML table. My goal is to confine designs to the 4th quadrant (+X,-Y).
Mouse coordinates javascript series#
The “table” is drawn as a series of THREE.Lines spaced 12 points apart from one another. The user would be shown the current “cell ” for reference. In picture below, the camera and orbit controls are shifted so that the origin (0,0,0 ) is in the top-left (DOM style), rather than the center. Note that Processing can only track the mouse position. Some designs have hundreds, if not a thousand individual 12x12 pixel elements all being rendered at the same time. The system variable mouseX always contains the current horizontal coordinate of the mouse. In the other application that used these designs, the table was not rendered which helped increase performance but still took its toll on mobile devices when the designs became very large (sometimes extending folds beyond the view-port).
If isDrawing is true, the event handler calls the drawLine function to draw a line from the stored x and y values to the. As the mouse moves over the page, the mousemove event fires. First we store the x and y coordinates of the mouse pointer in the variables x and y, and then set isDrawing to true. The table was only used for position referencing. Drawing begins when the mousedown event fires. I’m re-writing an existing application where a user would click cells in an HTML table and place predefined design items in them and then save that design to be used in another application. Coords.js is a tiny jQuery script that tracks mouse movement events and gets the current cursor position (horizontal and vertical coordinates) relative to Window using the JavaScript MouseEvent API.