Updating the Three.js Raycaster following CSS transformation

I am new to diving into the world of WebGL with Three.js. As a beginner, I wanted to experiment with something similar to this. I've managed to get most of it working, but I'm currently struggling with updating the raycaster and object after shifting the canvas to the left. When I hover over the sphere after moving the canvas, the interaction doesn't reflect on the sphere unless I move the mouse further eastward, away from the sphere. I've explored various resources, attempted to adjust the camera and sphere positions, but haven't had any luck.

Here's the code snippet:


let scene, camera, renderer;
var raycaster, mouse, INTERSECTED;
let SCREEN_WIDTH = window.innerWidth
let SCREEN_HEIGHT = window.innerHeight
let canvas = document.getElementById('scene')
let objects = []

init();
animate();

$(".hamburger").on("click", function () {
  $(".hamburger").toggleClass("active");
  $("#scene").toggleClass("slide-left");;
});

function init() {
  // Initialization code here
  
}

// Additional functions go here

              

Answer №1

1) It is important to provide the full code of your fiddle to ensure the context of your question and any answers remain clear, as fiddles can disappear over time.

2) Instead of attaching mouse events to the document, attach them to the moving part using the following code:

canvas.addEventListener('mousemove', onDocumentMouseMove, false);
canvas.addEventListener('mousemove', onHover, false);

3) The behavior of clientX/clientY may not be as expected. Consider using offsetX/offsetY for coordinates relative to the canvas if you followed step 2, despite MDN labeling it as experimental (it works well in WebGL-compatible browsers).

mouse.x = (event.offsetX / SCREEN_WIDTH) * 2 - 1;
mouse.y = - (event.offsetY / SCREEN_HEIGHT) * 2 + 1;

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

Issues with returning a response in AWS Lambda using NodeJs

I have been experimenting with a basic Node Js example on AWS Lambda that includes the Async library. The code seems to be functioning correctly, however, for some reason, the Lambda Function is returning a null response. As a newcomer to Node, I could use ...

Having trouble accessing the selected item in the $scope when using ng-options inside ng-repeat?

I am looking to achieve the following: I have an array called 'all' that contains all possible items. I want to create a subset of that array and store it in 'subset': JavaScript (js.js): var app = angular.module('myApp', [ ...

The art of blending different inheritance (Styled Elements)

Can components be based on other styled components? Take a look at the code snippets below. I am interested in creating a component like this: const HeaderDropDownLi = styled(DropDownLi, HeaderItem) Both DropDownLi and HeaderItem are derived from a style ...

Eslint requires that return values are used effectively

I am seeking a TypeScript or ESLint rule that enforces the usage of return values from functions. For example: const getNum = () => 12 This rule should allow me to call the function and store the returned value like this: const a = getNum() However, i ...

Connecting an admin dashboard to a MERN e-commerce application: A step-by-step guide

In the process of developing an e-commerce platform, I find myself using React.js for the frontend and Node.js/Express.js for the backend. My current challenge lies in creating a seamless dashboard to manage items within the app. One possible solution wo ...

If the children contain multiple div elements in jQuery

Is there a way to determine the number of divs inside a li element? Below is the code I've tried, but it's not functioning as expected. (The issue is that every img receives the class, even when there's only 1 div inside the li.) if($(&apo ...

What is the significance of having nodejs installed in order to run typescript?

What is the reason behind needing Node.js installed before installing TypeScript if we transpile typescript into JavaScript using tsc and run our code in the browser, not locally? ...

Is it possible to achieve the lower link set in a pure CSS horizontal menu where the horizontal link set is positioned below horizontal text tabs?

I am attempting to design a pure CSS on-hover horizontal menu. I have made some progress on my design, as shown in http://jsfiddle.net/dq8z192L/11/ The goal is to have a menu that expands horizontally when hovered over. Each tab reveals a set of links be ...

Exploring the capabilities of automation testing with charts.js and the latest version of Angular

While working on my testing automation for charts.js, I utilized the ngContext object to retrieve data with this code snippet: document.getElementsByTagName('chart-dataset')[0].__ngContext__. However, since upgrading to angular 14, it seems that ...

Troubleshooting React-Redux: Button click not triggering action dispatch

I am facing an issue where my action is not being dispatched on button click. I have checked all the relevant files including action, reducer, root reducer, configStore, Index, and Component to find the problem. If anyone can help me troubleshoot why my a ...

Using TypeScript's reference function within an HTML document

It feels like ages since my early days of web development. Back when I first started coding, we would reference a script using a <script> tag: <html> <head> <script src="lealet.js"></script> <!-- I know the path isn´t c ...

Strange glitch in the rendering of rectangles on HTML5 canvas?

While experimenting with pre-rendering sprites using HTML5 canvas, I encountered some strange behavior that appears to be a rendering bug. The issue can be seen in the following minimal example: var CT = document.getElementById("myCanvas").getContext("2 ...

JavaScript is unable to identify the operating system that is running underneath

Even though I am on a Windows system, the browser console is showing that I am using Linux. function detectOS() { const userAgent = navigator.userAgent.toLowerCase(); if (userAgent.includes('win')) { return 'Windows' ...

Enhance JQuery functionality using Typescript

Currently, I am in the process of developing a Typescript plugin that generates a DOM for Header and attaches it to the page. This particular project utilizes JQuery for handling DOM operations. To customize the plugin further, I aim to transmit config Opt ...

Storing user data on the client side of a website is most effectively accomplished by

I am looking to incorporate the following features: I would like to personalize the user experience by greeting them with their login name on the webpage (e.g. 'Hello, Fred!'). I also want to display or hide certain content based on the user&apo ...

What is the maximum character limit for the JQuery validation plugin?

Currently, I am utilizing the JQuery validation plugin in my project. My goal is to set a maxlength for one of the fields. Here's an example of how it can be done by defining rules externally: rules: { Message: { required: false, maxLe ...

Setting the image path using setAttribute in JavaScript: A step-by-step guide

Trying to establish a pathway to an image in my local storage using a setAttribute approach seems to be resulting in an error indicating that the image cannot be found. Here is a snippet of the code I am working with: if (shipLocation[0][0] == shipLocatio ...

A template literal will not recognize an imported function

import {createTasks} from './app.js'; document.addEventListener("DOMContentLoaded", function () { function showNewTaskModal () { newTaskModal.classList.add('show'); const html = ` <d ...

Utilizing a mutual RxJS subject for seamless two-way data binding in Angular 2

I have a unique service dedicated to managing app configurations class Configuration { get setting() { return dataStore.fetchSetting(); } set setting(value) { dataStore.saveSetting(value); } } This configuration is linked to components t ...

The Access-Control-Allow-Origin error is preventing the Angularjs post request from going through

I am encountering an issue with my index.html file that is sending a post request to localhost:3000/SetUser. The error I keep receiving states XMLHttpRequest cannot load https://localhost:3000/SetUser. No 'Access-Control-Allow-Origin' header is p ...