The visibility of the Three.js plane is dependent on the mouse being within the boundaries of the browser

Just starting out with three.js and I encountered a strange issue. Whenever the mouse cursor is outside of the content area (for example, if I reload the page and the cursor is on the browser's reload button or outside of the browser window), the object becomes invisible. Any suggestions on what might be causing this problem?

Answer №1

An issue arises when trying to use the variables mouseX and mouseY in the render() function without initializing them first. Since these variables are tied to mouse movement, they remain undefined until the cursor actually enters the screen.

function render() {
    ....

    mesh4.rotation.x = mouseY;  //undefined here
    mesh4.rotation.y = mouseX;  //undefined here

    ...
}

The values for mouseX and mouseY are only set within the onDocumentMouseMove() function:

function onDocumentMouseMove( event ) { 
    var windowHalfX = window.innerWidth/2;
    var windowHalfY = window.innerHeight;
    mouseX = ( event.clientX - windowHalfX ) / 11000; // <-- gets a value here
    mouseY = ( event.clientY - windowHalfY ) / 11000; // <-- and here
}

To fix this issue, it is recommended to initialize both variables to zero in the init() function so that they are not undefined during rendering:

function init() {
    mouseX = 0;
    mouseY = 0;

    ...
}

You can view the code example on jsfiddle: https://jsfiddle.net/L6c8dhxd/3/

I hope this explanation clarifies the problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

D3 Chart: What is the best way to insert images into a node?

Within the jsFiddle demo provided in this query, there is code displayed. I am curious about how I can assign images to each node. var graph = { "nodes":[ {"name":"1","rating":90,"id":2951}, ] } You can access my jsFiddle Demo through this ...

The issue of AngularJS directives not populating correctly with their associated templates

I'm encountering some challenges with directives and their scope. I've developed a directive to fetch all the members of a site and display them within a div, here's the code snippet: EngagementApp.directive('siteMembers', [' ...

Display only the initial item in the ng-repeat loop

Is there a way to display only the first element in Angular without using ng-repeat? I have been trying to achieve this using ng-repeat as shown below: <div ng-repeat="product in products" ng-show="$first"> <div>{{ product.price }}</di ...

Utilize Express to Refine HTTP Responses

I am currently in the process of developing a polls application using express. The structure of my data is as follows: var responseSchema = new mongoose.Schema({ responseText: String, voters: [] }) module.exports = mongoose.model('Responses& ...

I am looking to utilize jQuery to dynamically update a selected checkbox through AJAX

I've been struggling to make my ajax json script update my HTML checked box with the value from the database, but so far I haven't had any luck. Any assistance would be greatly appreciated! Here's the JS code snippet: setInterval(functi ...

Create a notification that appears no matter where I am browsing

Is it possible to create a notification system in ReactJS/JS where messages are displayed at the bottom of the screen, even if the page is not active or minimized? I have set up a timer and would like to be able to move to another browser tab while the t ...

unresolved string constant issue with a django template command

I encountered an issue with the code snippet below, which is resulting in an unterminated string literal error: $(function() { $('#addDropdown').click(function() { var $d = $('{{ form |bootstrap }}').fadeIn(). ...

Looking to decrease Cumulative Layout Shift (CLS) on the NextJS website for enhanced performance

I have chosen to use NextJS with Mantine for my website development. Utilizing createStyles, I have added custom stylings and incorporated various mantine components into the design. After deploying the site on Vercel, I discovered that all performance me ...

Is it possible to use JavaScript to scrape content from another website and incorporate it into my HTML document?

Attempting to web scrape another website in order to display its content on my own site. I have written JavaScript code that works, but only when triggered on the website's onClick event. var button = document.getElementById("scrape-website"); button ...

Fixing a scrolling element within a div located below the screen is my goal, ensuring it remains fixed in place as the user scrolls

I'm facing a challenge that I need help with: My HTML/CSS layout currently looks like this: Screenshot of how my CSS/HTML appears on the screen upon page load: As I scroll down, I encounter a green element: While scrolling down -> Upon further s ...

What are the steps to implement server side routing using react-router 2 and expressjs?

If you're looking to delve into server side rendering with react-router, the documentation linked here might fall short in providing a comprehensive understanding. In particular, it may not offer enough insights on how to leverage react-router 2 for r ...

Is `send()` considered an anonymous function?

I am testing ajax on a local server, but encountering an issue where the console is showing that send() is being identified as an anonymous function and I am receiving a blank document. The console displays: XHR finished loading: GET "http://localhost/js ...

Unusual behavior observed with ES5 filter functionality in Node.js

My goal is to use ES5 : filter to refine the data provided below. { "EmailAddress": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="167c797356736e777b667a73e98175d3faf7">[email protected]</a> ...

Displaying JavaScript array contents in an HTML page without using the .innerHTML property

As someone new to the world of JavaScript and web development, I find myself faced with a challenge. I want to showcase the contents of two different JavaScript arrays in an HTML format. However, my research has led me to believe that utilizing .innerHTML ...

Mongoose throws a "Possibly unhandled rejection" error when trying to use ContactList.insert as it is not a recognized function

Currently working on a small project using the MEAN stack, but encountering an issue with sending a post request where console.log displays an error. https://i.sstatic.net/7nUXH.jpg Error Message: Possibly unhandled rejection: {"data":"TypeError: Contac ...

Error occurs when attempting to reload a page while utilizing Angular Ui Router with Html5 mode activated

I've implemented Angular UI Router in my Angular application and configured HTML5 mode to remove the # from the URL using $locationProvider in the config. var app = angular.module('openIDC', ['ui.router']); app.config(function($ur ...

Creating a React component in Typescript to utilize lodash helper functions

I have a component that looks like this: import React, { Component } from 'react'; import throttle from 'lodash.throttle'; interface Props { withScroll: boolean; } class Image extends Component<Props, {}> { throttledWindowS ...

Loading an OBJ file from a blob using three.js

I am currently attempting to load an OBJ file from a blob using Three.js. After referring to this resource and successfully loading STL files, I encountered an issue with loading OBJ files. The error message I received is as follows: TypeError: text.indexO ...

Configurable NodeJS modules can be instantiated either through the require function or by using module.exports

I'm currently developing a node.js module that includes multiple functions. To initiate the module, some configuration is required (with default options that can be overridden - such as file paths). What would be the best approach for exporting a f ...

What is the process for installing the paper.js library?

Recently, I've delved into the world of Paper.js Following a tutorial's instructions, but unfortunately, I'm not getting any output. Here's the code I've used. Can anyone assist in solving this issue? <!DOCTYPE html> &l ...