HTML DOM drag-n-drop: what about when mouse leaves window? - html

I'm working on a web app where I support dragging on the page, by simply watching for mousedown/mousemove/mouseup events. That part works great.
The problem is that I'm only handling dragging inside one particular element, by design, and if a user (accidentally or not) drags outside, it gets "stuck" in drag mode. That is, mouse-down, then mouse-leaves-window, then mouse-up, then mouse-returns to window looks like it's still dragging, to my app.
I haven't figured out any way to solve this -- even something simple like "when the mouse re-enters the window, is the mouse button down?" would work.
Does such functionality exist, and I'm just missing it? Or is there some clever workaround I can employ here?
Legacy support has no importance to me -- if it's an HTML5 solution that only works in FF3.5/Chr4/Sf4, I'm happy with that.

In IE7/IE8 you can detect if the mouse was released outside the window by using the following code:
document.onmousemove = function(event) {
event = event || window.event;
if (document.all && event.button != 1) {
canceldragging();
}
}
In Firefox and Webkit the mouseup event is fired on the document when the mouse is released even if mouse pointer outside the browser window. You can see this using http://www.quirksmode.org/dom/events/tests/click.html
For IE8 the document onmouseup event is fired when the mouse button is released outside the browser window only if you allow document selection to occur (as the link above does). That is, in IE8 you don't get the mouseup event if you use document.onselectstart and cancel the selection, or if you use unselectable="on" on the starting element, or if you called document.selection.clear() combined with document.selection.empty() while the mouse was down.

What if you had the onmouseout event of the element fire the mouseup event?
If you're just using inline handlers, something along the lines of:
<div id='dragElement' onmouseup='alert("stop dragging!")' onblur='this.onmouseup();'></div>
added to whatever event handling code you're already using. This would 'release' the drag whenever the element loses focus. Not the cleanest code, but you get the idea.

Related

React state based on mouse/keyboard press/release

i'm creating a custom button with React.
The appearance based on the press/release of the mouse/keyboard.
Sometimes it cannot be done by css using :active pseudo, so i need the javascript solution.
So i created a state that listen the onMouseDown/Up & onKeyDown/Up.
The problem is, if the user click the button then drag the mouse out of the button then release the mouse,
i miss the onMouseUp event thus causing the activeState locked up forever.
Do you have a solution for the equivalent of the :active & :not(:active) pair in javascript way?
note:
watching event window.addEventListener('mouseup',...) is still not satisfying me. When the mouse click hold & move out from the browser's window, i still missing the mouseup event.

How can I monitor focus events in Chrome?

I'm trying to track which element gets focus in a web app. I came across the monitorEvents API, but I'm having difficulty using it for control or focus events. Other events on body are working as expected, but not the control events. Any advice?
I'm not sure how exactly you want to "monitor" control events, but you can set event listener breakpoints on the entire category, or individual events like focus. Whenever a focus listener runs for any node on the page, DevTools pauses on the first line of the listener.
I was trying to monitor focus and blur events on the entire document today.
When using monitorEvents(document.body, 'control') I would only see the event fired when I left the tab entirely and then refocused. I believe what's happening is that once you're focused on the body, it'll never change when you focus on child elements since the listener is at a high level and the events just bubble up to the body with no change in focus.
I tried monitoring events on a few specific child elements and saw that those triggered how I expected. So I decided to add an event listener to each element in the document and that worked well. Here's the one-liner for that.
document.querySelectorAll("*").forEach((el)=> monitorEvents(el, 'control'))

Mouseover event not triggered when data updated in Chrome

Every element in my svg listens to a mouseover event, and when an element is clicked the chart would be updated to show another set of elements.
However, if I click an element and a new element occurs right under the mouse, the mouseover event is not triggered unless I move the mouse a bit.
Then I found that this problem only occurred in Chrome...
This is a Chrome issue, no workaround.

LibGDX Input from previous screen registering

I am using isTouched to setScreen from my menu screen to my main game screen. (Tap to continue).
In the constructor of the main game screen, I set the input processor. The input processor then immediately fires from the touch on the previous screen.
What is the proper way to handle this?
EDIT: If I tap my finger on an Android device, the tap triggers the isTouched/justTouched. Then the next screen loads faster than I lift my finger and the finger up event triggers my input processor.
I don't think there is any built-in way to prevent this sort of event leakage. One way to avoid the problem is to trigger your transition on release, not press.
Switch your main menu to use an InputProcessor. Use the end-of-touch event to trigger your transition, so that event won't be around to pollute your new InputProcessor. This will avoid mixing polling and event-based input, which seems cleaner, too.
Set a flag when isTouched is true, then in later a render iteration when isTouched is false, and the flag is true, you know it is safe to proceed (this is a hacky polling version of waiting for the touch-up event).
In many UIs button events trigger on the touch-up (or its equivalent). E.g., in this stackoverflow UI, click down on the "Post Your Answer" button, then drag the mouse off the button and release. The button doesn't "click". (Similarly if you click outside the button, drag into it, and then release, it still doesn't "click".)

Page flip effect on button press in html5

I'm looking at this:
http://www.netmagazine.com/tutorials/create-page-flip-effect-html5-canvas
However, I have one problem with that - I need to be able to click on the pages, even the edges, without triggering the page turn. I want the pages to turn when a button outside of the canvas is pressed. Is this possible using the base they provided, or do I need to go an entirely different direction?
Yes that can be done.
From what i can see, you need a click event that doesnt trigger the page drag. You need to assign a flag for this.
Let Drag = mouse drag/mouse move, down = mouse down, release = mouse release events respectively.
Initialize your flag variable as false. When a drag event is encountered it becomes true. Otherwise it remains false. As long as it is false when the mouse release event occurs it can be treated as a click. Thats the basic principle behind using mousedown and mouseup as a click event.
You will have to use e.srcElement or e.target to give you the element your cursor is currently positioned over inorder to trigger click functions relative to that element.
If you want a more detailed explanation on the page flip technique then check this out. Helped me lot.