Javascript Mouse Events Not Working on Three.JS Scene Elements - javascript

Ok, I'm just confused (and probably too tired for my own good to be working on this now...). I'm trying to get my three.js application to execute different functions for different mouse events when the mouse is over a particular type of entity in my scene. Events of type "mousemove" are working perfectly, and the
console.log( 'INTERSECTED.isGraphElement: ', INTERSECTED.isGraphElement, 'MouseEvent: ', mouse.type );
statement registers all the mouseEvents that I'm listening for, which include "mousemove", "click", "dblclick", "wheel" and "oncontextmenu". It's also detecting INTERSECTED.isGraphElement objects as intented.
Nevertheless, when transformGraphElement() runs, only the "mousemove" events are registered inside the transformGraphElement function. Even the commented-out test line of code that should console out "Got the dblclick!" doesn't run. It appears that no other mouse events that I'm listening for are being detected here.
Things I've tried inside the transformGraphElement function:
Swap out the dblclick event for one of the other types I'm listening for. No dice.
Comment out the line for handling mousemove events. No dice.
Test to see if there's a bug in my obj.referent.transformOnDblClick() function. I only learned that it wasn't being called at all (and runs perfectly when I associate it to the "mousemove" event).
Trying different valid syntaxes for my 'if' statements. No dice.
Trying a bit of refactoring on the mouseEventHandler() function. No dice.
Here's my relevant code:
function render() {
mouseEventHandler( transformGraphElement, unTransformGraphElement );
requestAnimationFrame( render );
renderer.render(scene, entities.cameras.perspCamera );
}
function mouseEventHandler( fn, revFn ){
// update the picking ray with the camera and mouse position
ray.setFromCamera( mouse, entities.cameras.perspCamera );
// calculate objects intersecting the picking ray
var intersects = ray.intersectObjects( scene.children );
if ( intersects && intersects[0] && intersects[0].object ){
if ( intersects[ 0 ].object != INTERSECTED ){ // if there's an intersected object
if ( INTERSECTED ) { // and if a previous INTERSECTED object exists:
revFn( INTERSECTED, mouse ); // restore the previous intersected object to its non-intersected state.
}
INTERSECTED = intersects[ 0 ].object; // set the currently intersected object to INTERSECTED
fn( INTERSECTED, mouse ); // transform the currentlY INTERSECTED object.
}
console.log( 'INTERSECTED.isGraphElement: ', INTERSECTED.isGraphElement, 'MouseEvent: ', mouse.type );
}
}
function transformGraphElement( obj, mouse ){
// Check if INTERSECTED is a Graph Element, and if so, invoke it's transform function.
if ( mouse.type === "mousemove" && obj.isGraphElement ) { obj.referent.transformOnMouseOver(); }
//if ( mouse.type === 'dblclick' ) { console.log('Got the dblclick Inside!') }
if ( mouse.type === 'dblclick' && obj.isGraphElement ) { obj.referent.transformOnDblClick(); ) }
}
function unTransformGraphElement( obj, mouse ){
// Check if INTERSECTED is a Graph Element, and if so, revert it to it's pre-mouseEvent state.
if ( mouse.type === "mousemove" && obj.isGraphElement ) { obj.referent.transformOnMouseOut(); }
if ( mouse.type === 'dblclick' ) { console.log('Got the dblclick Out!') }
}
I'm wondering if its some sort of default behavior or override that I'm running into, but shouldn't the event.preventDefault() line be handling that? (This code below runs before the code above):
var ray = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var INTERSECTED; // Object closest to the camera
function onMouse( event ) {
event.preventDefault();
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
mouse.type = ( event.type );
}
function listenFor(){
document.addEventListener( 'click', onMouse, false );
document.addEventListener( 'mousemove', onMouse, false );
document.addEventListener( 'mousedown', onMouse, false );
document.addEventListener( 'dblclick', onMouse, false )
document.addEventListener( 'wheel', onMouse, false );
document.addEventListener( 'contextmenu', onMouse, false );
}
listenFor();
Console.log( mouse.type ) from inside the onMouse() function registers all the mouse events I'm listening for perfectly.
I've been banging my head against this for 3 hours. I'm hoping it's something stupid simple that I'm missing because I'm in a bad mood. All help is welcome, and let me know if there's any missing code important to providing a useful answer... I don't think so but again, in a bad mood...
Thanks for your help!

Well the next morning I got up and figured out the problem. The issue was with the logic in the code:
if ( intersects[ 0 ].object != INTERSECTED ){ // if there's an intersected object
if ( INTERSECTED ) { // and if a previous INTERSECTED object exists:
revFn( INTERSECTED, mouse ); // restore the previous intersected object to its non-intersected state.
}
INTERSECTED = intersects[ 0 ].object; // set the currently intersected object to INTERSECTED
fn( INTERSECTED, mouse ); // transform the currentlY INTERSECTED object.
}
console.log( 'INTERSECTED.isGraphElement: ', INTERSECTED.isGraphElement, 'MouseEvent: ', mouse.type );
}
Non-mousemove events could not be passed. My solution was to wrap this section with another conditional if (mouse.type === 'mousemove') and then have additional conditionals for other event types. Here's the whole, with a bit of refactoring to make the whole easier to reason about:
var ray = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var INTERSECTED; // Object closest to the camera
function onMouse( event ) {
event.preventDefault();
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
mouseEventHandler( event /*, transformGraphElement, unTransformGraphElement */ );
}
function listenFor(){
document.addEventListener( 'click', onMouse, false );
document.addEventListener( 'mousemove', onMouse, false );
document.addEventListener( 'mousedown', onMouse, false );
document.addEventListener( 'dblclick', onMouse, false )
document.addEventListener( 'wheel', onMouse, false );
document.addEventListener( 'contextmenu', onMouse, false );
}
listenFor();
/* ... */
function render() {
requestAnimationFrame( render );
renderer.render(scene, entities.cameras.perspCamera );
}
function mouseEventHandler( event /* , fn, revFn */ ){
// update the picking ray with the camera and mouse position
ray.setFromCamera( mouse, entities.cameras.perspCamera );
// calculate objects intersecting the picking ray
var intersects = ray.intersectObjects( scene.children );
// if there's at least one intersected object...
if ( intersects && intersects[0] && intersects[0].object ){
// Check if the event is a mouse move, INTERSECTED exists and we're sitting on the same INTERSECTED object as the last time this function ran...
if ( event.type === 'mousemove' ){
// Check if the current top-level intersected object is the previous INTERSECTED
if ( intersects[ 0 ].object != INTERSECTED ){
// ... if there is a previous INTERSECTED
if ( INTERSECTED ) {
// restore the previous INTERSECTED to it's previous state.
unTransformGraphElementOnMouseOut( INTERSECTED, event );
}
// set the currently intersected object to INTERSECTED
INTERSECTED = intersects[ 0 ].object;
// and transform it accordingly.
transformGraphElementOnMouseOver( INTERSECTED, event );
}
}
// Check if the mouse event is a doubble click
if ( event.type === 'dblclick' ){
// If the currently intersected object is INTERSECTED
if ( intersects[ 0 ].object === INTERSECTED ){
// select it.
transformGraphElementOnSelect( INTERSECTED, event );
}
// If the currently intersected object is not INTERSECTED
if ( intersects[ 0 ].object !== INTERSECTED ){
// If there is a previous INTERSECTED
if ( INTERSECTED )
// restore it to its unselected state.
unTransformGraphElementOnUnselect( INTERSECTED, event );
}
}
INTERSECTED && console.log( 'INTERSECTED.isGraphElement: ', INTERSECTED.isGraphElement, 'MouseEvent: ', event.type );
}
}
function transformGraphElementOnMouseOver( obj, event ){
if ( obj.isGraphElement ) { obj.referent.transformOnMouseOver(); }
}
function unTransformGraphElementOnMouseOut( obj, event ){
if ( obj.isGraphElement ) { obj.referent.transformOnMouseOut(); }
}
function transformGraphElementOnSelect( obj, event ){
if ( obj.isGraphElement ) { obj.referent.transformOnDblClick(); }
}
function unTransformGraphElementOnUnselect( obj, event ){
if ( obj.isGraphElement ) { obj.referent.unTransformOnDblClick(); }
}
The logic of mouseEventHandler() still has some issues, but the core frustration is handled. Some additional refactoring worth mentioning:
It wasn't necessary to add the event.type to mouse.
I moved the mouseEventHandler() call from render() to onMouse(). That ensures that mouse events are registered only once.
I got rid of the 'fn'/'revFn' callbacks in mouseEventHandler() as they just made things more confusing than they need to be.
Event doesn't need to be a parameter for the transform functions, that'll be coming out shortly.
Hope this helps someone.

Related

To find who's binded to an event and use if and else if with javascript

I have a function called in multiple ways.
function func1(){
if(){ //called by an onclick event
alert("!");
}else if(){ //called by another function -
alert("?"); //ex) - $(document).ready(function(){ func1()}
}
is there any way to determine if the function is bound by which event and use it with my if and else if lines? Please help.
There's several options. I use both depending on the situation.
1) Seperate the event handlers from the other function and use a parameter.
<button id="button_validate_form">Validate</button>
<button id="button_submit_form">Sumbit</button>
const doWork = function doWork( event, context ) {
if ( context === 'event_validate_form' ) alert( '!' );
else if ( context === 'event_submit_form' ) alert( '?' );
else alert( 'undefined event' );
};
document.querySelector( '#button_validate_form' ).addEventListener( 'click', event => doWork( event, 'event_validate_form' ) );
document.querySelector( '#button_submit_form' ).addEventListener( 'click', event => doWork( event, 'event_submit_form' ) );
2) Same story, but instead of adding a parameter to the function, use a data attribute on the actual DOM node.
<button id="button_validate_form" data-context="event_validate_form">Validate</button>
<button id="button_submit_form" data-context="event_validate_form">Sumbit</button>
const doWork = function doWork( event ) {
const context = event.target.getAttribute( 'data-context' );
if ( context === 'event_validate_form' ) alert( '!' );
else if ( context === 'event_submit_form' ) alert( '?' );
else alert( 'undefined event' );
};
document.querySelector( '#button_validate_form' ).addEventListener( 'click', event => doWork( event, 'event_validate_form' ) );
document.querySelector( '#button_submit_form' ).addEventListener( 'click', event => doWork( event, 'event_submit_form' ) );

Autocomplete: How to focus on first item after pressing arrow down key on last item?

Go through each option of the dropdown by using arrow down key. When focusing on the last option, and pressing arrow down key the cursor move to textbox instead of the first option. How make the cursor go to the first option?
You can use below code block to fix that issue.
.autocomplete('instance')._move = function( direction, event ) {
if ( !this.menu.element.is( ":visible" ) ) {
this.search( null, event );
return;
}
if ( this.menu.isFirstItem() && /^previous/.test( direction ) ||
this.menu.isLastItem() && /^next/.test( direction ) ) {
if ( !this.isMultiLine ) {
this._value( this.term );
}
this.menu[ direction ]( event );
// this.menu.blur();
return;
}
this.menu[ direction ]( event );
}

Impress.js + Firefox: How to disable advance slide on mouse click

Impress.js works great in Chrome, but unfortunately, it's somewhat buggy in Firefox. The biggest problem that I am having is that, in Firefox, the slideshow advances to the next slide on every mouse click. Does anybody know of a way to disable this functionality?
Go to js/impress.js, delete the code below (about line 783~797) , it listen the click event:
// Delegated handler for clicking on step elements
document.addEventListener( "click", function( event ) {
var target = event.target;
// Find closest step element that is not active
while ( !( target.classList.contains( "step" ) &&
!target.classList.contains( "active" ) ) &&
( target !== document.documentElement ) ) {
target = target.parentNode;
}
if ( api.goto( target ) ) {
event.preventDefault();
}
}, false );

Where to insert preventDefault in jquery code

I am using the snippet found at to http://tympanus.net/Development/MorphingSearch/ enlarge a search box when it is clicked..
<script>
(function() {
var morphSearch = document.getElementById( 'morphsearch' ),
searchlink = document.getElementById( 'mybtn' ),
input = morphSearch.querySelector( 'input.morphsearch-input' ),
ctrlClose = morphSearch.querySelector( 'span.morphsearch-close' ),
isOpen = isAnimating = false,
// show/hide search area
toggleSearch = function(evt) {
// return if open and the input gets focused
if( evt.type.toLowerCase() === 'focus' && isOpen ) return false;
if( isOpen ) {
classie.remove( morphSearch, 'open' );
// trick to hide input text once the search overlay closes
// todo: hardcoded times, should be done after transition ends
if( input.value !== '' ) {
setTimeout(function() {
classie.add( morphSearch, 'hideInput' );
setTimeout(function() {
classie.remove( morphSearch, 'hideInput' );
input.value = '';
}, 300 );
}, 500);
}
input.blur();
}
else {
classie.add( morphSearch, 'open' );
}
isOpen = !isOpen;
};
// events
searchlink.addEventListener( 'click', toggleSearch );
ctrlClose.addEventListener( 'click', toggleSearch );
// esc key closes search overlay
// keyboard navigation events
document.addEventListener( 'keydown', function( ev ) {
var keyCode = ev.keyCode || ev.which;
if( keyCode === 27 && isOpen ) {
toggleSearch(ev);
}
} );
/***** for demo purposes only: don't allow to submit the form *****/
morphSearch.querySelector( 'button[type="submit"]' ).addEventListener( 'click', function(ev) { ev.preventDefault(); } );
})();
</script>
All works great but as the search box is in a fixed header the page content jumps to the top when the link is clicked.
Previously when this has happened i have inserted the following...
event.preventDefault();
I can't work out where to insert this in this code though, can anyone help?
You insert it in the function that is called by the click event handlers
toggleSearch = function(evt) {
if (evt.type === 'click') {
evt.preventDefault();
}
// rest of code
}
searchlink.addEventListener( 'click', toggleSearch );
ctrlClose.addEventListener( 'click', toggleSearch );
Note the condition, it's to make sure the default action isn't prevented when the keydown handler is calling the same function

Seems something is wrong when trying to set requestPointerLock()

It seems something is wrong in my code, but I cannot figure what. I am trying to call pointerlockchange api to disable pointer and use mouse as fps controller. The problem is that always pointerlockerror is triggered and I get a Error setting pointer lock! message.
The function is called inside $(document).ready. Here's the code.:
function initLock() {
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
if ( havePointerLock ) {
var element = document.body;
console.log('Cathing element', element);
var pointerlockchange = function ( event ) {
if ( document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element ) {
controls.enabled = true;
console.log('Pointer lock enabled!')
}
else {
controls.enabled = false;
}
}
var pointerlockerror = function ( event ) {
console.log('Error setting pointer lock!');
}
// Hook pointer lock state change events
document.addEventListener( 'pointerlockchange', pointerlockchange, false );
document.addEventListener( 'mozpointerlockchange', pointerlockchange, false );
document.addEventListener( 'webkitpointerlockchange', pointerlockchange, false );
document.addEventListener( 'pointerlockerror', pointerlockerror, false );
document.addEventListener( 'mozpointerlockerror', pointerlockerror, false );
document.addEventListener( 'webkitpointerlockerror', pointerlockerror, false );
// Ask the browser to lock the pointer
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
if ( /Firefox/i.test( navigator.userAgent ) ) {
var fullscreenchange = function ( event ) {
if ( document.fullscreenElement === element || document.mozFullscreenElement === element || document.mozFullScreenElement === element ) {
document.removeEventListener( 'fullscreenchange', fullscreenchange );
document.removeEventListener( 'mozfullscreenchange', fullscreenchange );
element.requestPointerLock();
}
}
document.addEventListener( 'fullscreenchange', fullscreenchange, false );
document.addEventListener( 'mozfullscreenchange', fullscreenchange, false );
element.requestFullscreen = element.requestFullscreen || element.mozRequestFullscreen || element.mozRequestFullScreen || element.webkitRequestFullscreen;
element.requestFullscreen();
}
else {
element.requestPointerLock();
}
}
else {
console.log('Sorry, pointer lock cannot be set.');
}
}
Found solution by myself. The problem is that requestPointerLock cannot be called automatically, it should be called from the user's callback, for example, when clicking on something.

Categories