A Simple Discrete-Event Simulation: Part 82

Today I added the ability to use the mouse to drag the 2D display around within its canvas. I had to update the various pan functions to allow specification of an arbitrary distance increment, though there are a number of ways I could have done that. I may change things up going forward. In the meantime, the code sets up events for mousedown, mousemove, and mouseup events and, when the dragFlag is set to true, goes ahead and moves the 2D display by the requested increments in both the x and y directions.

I was a little bit stunned when it worked perfectly the first time I tried it, and it also works smoothly when the simulation is running full blast. Fun!

Here’s the new code for handling the mouse events:

And here’s the updated code for the pan functions. I left the limits unchanged for now.

I found it necessary to handle the mouseleave event because it was possible to mouse out of the canvas, release the mouse, return to the canvas, and still be dragging the 2D display. In order to get out of that the user would have to do a new mousedown and mouseup to properly clear the state. Perhaps it would be better to only snap back to the drag origin when the user releases the mouse when outside the canvas, when a drag sequence is in progress.

The drag mouse event code to eat that event, or prevent it from bubbling up through further elements, does run when expected, but I can’t be sure it’s doing anything. It runs when using the keystrokes as well, but the entire window will still scroll up or down when the 2D display reaches its upper or lower limits. It’s bad enough when running this page standalone but when its embedded on a WordPress page with ten other posts it wants to scroll the whole window down rather than ever scrolling the 2D display back up. This means either that the event that scrolls the larger window up and down is being processed at a lower level that the canvas and is being allowed to bubble up to the canvas, or that it isn’t actually being stopped from propagating.

I need to get a much, much better feel for how the whole event mechanism works in JavaScript, HTML, and the DOM. It’s amazing how far you can get without going into depth in some really important areas. The explanations I’ve read seem simple enough, and of course I’ve been using event-driven frameworks forever, but my work didn’t usually require me to dig into the details. I suspect this is going to keep me hopping.

In the longer run there’s a lot more to add here. For example, the canvas should only allow the user to drag when the entire image is not in view, and even then perhaps in certain modes, which themselves must be controllable. Anyway, one piece at a time, especially when I’m busy with other things.

Last but not least I ended up learning a lot more about how WebGL runs — or fails to run — on different browsers at different times. I’ll relate that saga tomorrow.

This entry was posted in Software and tagged , , , , . Bookmark the permalink.

Leave a Reply