``There seems to be an issue with the click handler function not properly functioning on aframe

After incorporating a click handler on primitive geometry from A-Frame, the setup looks like this:

<a-scene>
        <a-sphere id="ball" cursor-listener material="color: red" position="-3 3 -5" onclick="showplane('#box1')" >
        </a-sphere>
      </a-scene>
      

My goal is to animate a plane geometry when the sphere is clicked. Once the plane is visible to the user, I need to attach a click handler for the plane.

Check out the JSFiddle demonstration here: https://jsfiddle.net/bhupi1011/9ptLqa32/

Unfortunately, the click handlers are not working on either of the geometries. Any help would be appreciated.

Thank you.

Answer №1

To incorporate a cursor into the scene, you must follow these steps for mouse click interaction:

<a-scene cursor="rayOrigin: mouse">

Implement a component to manage click events:

AFRAME.registerComponent('select', {
  init: function () {
    var originalColor = this.el.getAttribute('material').color;
    var self = this;
    this.el.sceneEl.addEventListener('click', function (evt) {
      var color;
      if (evt.detail.intersectedEl === undefined) { return; }
      var color = evt.detail.intersectedEl === self.el ? 'green' : originalColor;
      self.el.setAttribute('color', color);
    }, false);
  }
});

Check out the demo here:

View the code on Glitch:

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

What is the correct method for iterating through this array of objects and eliminating those with either null or empty item.text values?

Currently, I am working with the following Array: const contactInfo = computed(() => { return [ { id: 'address', icon: 'detail/clubLocation', text: `${props.data.contactInfo.address}` ...

Tips for acquiring offspring who are exclusively not descendants of a particular node

Currently, I am utilizing jQuery and my goal is to access all elements of a specific type that are not children of a particular node type. For example, my Document Object Model (DOM) structure looks like this: <div id='idthatiknow'> & ...

Is it a common issue that sound.stop() doesn't seem to function properly, while sound.play() operates successfully within Howler

I am having trouble utilizing Howler.js in Reactjs with typescript. While I can successfully play the sound, pausing or stopping it seems to be an issue. Below is the code snippet: This component takes all the audio details as props. I used console.log() ...

Adjusting the size of an image to be responsive once the document has fully

Struggling with creating a custom carousel/slider element and running into an issue that doesn't seem to make any sense. Attempting to retrieve image size using jQuery on document load results in incorrect width/height calculations. Initially thought ...

When working with data in Angular, make sure to use any[] instead of any in app.component.html and app.component.ts to avoid causing overload errors

I'm new to working with Angular, specifically using Angular 15. I have a Rest API response that I need to parse and display in the UI using Angular. To achieve this, I employed the use of HttpClient for making GET requests and parsing the responses. ...

Exporting modules from Node.js using Express framework is a common

Encountering an issue with this error message Error: app.get is not a function This section shows my configuration in config/express.js var express = require('express'); module.exports = function(){ var app = express(); app.set(&apo ...

Passing console.log as an argument in a JavaScript abstraction

Hey amazing minds of the coding world! I've embarked on a journey to learn JavaScript and I'm currently immersing myself in the Eloquent JavaScript book! At the moment, I am working on abstracting a for loop action with this code snippet... fu ...

What causes a never-ending loading cycle on a website when a 404 error occurs?

My Express.js/Node.js website is hosted on Heroku. I am facing an issue where the server does not properly send a 404 error when a file cannot be found. Instead, the page keeps loading endlessly. How can I make the server stop loading when it encounters a ...

The destroy method of Chart.js does not appear to have any impact on the

Hello, I've come across this issue in my react app that I need help with: this.chart = new Chart(node, options); // adding data to the chart ... this.chart.destroy(); this.chart = null; this.chart = new Chart(node, options); // adding data to the cha ...

How can I save a value in JavaScript once the page has finished loading?

$("#divid1").click(function(){ $("#divid1").hide(); //this should remain hidden even after refreshing the page $("#hideid1").hide(); //this should remain hidden even after refreshing the page $("#id1").show(); //this should remain visible even ...

What is the best way to pass and save information across different routes in Express using Node.js?

Let's delve into the specific functionalities I envision for my server. Essentially, my project involves user registration and login processes. The URLs for these routes are as follows - localhost:3000/login and localhost:3000/register Upon successf ...

Attempting to modify the contenteditable div HTML and shift the cursor to the end is not functioning as expected

I'm struggling with a function that is supposed to change the content of my contenteditable div. While it initially works fine, I encountered an issue where the cursor jumps to the start unexpectedly. In an attempt to resolve this problem, I came acro ...

Utilizing Lodash's uniqWith function, it allows us to specify how we want to

Currently, I am utilizing the lodash uniqWith method to eliminate duplicate items with the same id from my array. However, the lodash method retains the first duplicated item, whereas I actually want to keep the last duplicated item. Is there a way to ac ...

An error was encountered: Unexpected token 'var'

Upon loading my website, I encountered the following error: Uncaught SyntaxError: Unexpected token 'var' This issue seems to be related to Elementor and possibly the admin-ajax.php file. https://i.sstatic.net/OM2al.jpg If anyone has insight o ...

Steps for altering the public folder in a Next.js project

In my monorepo, the structure is as follows: apps/ web/ - next.config.js - pages/ public/ The Next.js application is located in the apps/web/ directory. How can I modify the next.config.js file to instruct it to serve public assets from the root ...

JavaScript implementation for text smearing visual effect

While I have a strong understanding of Javascript, I haven't explored its advanced graphics features like canvas, webGL, and three.js. I'm interested in creating a distortion effect similar to the one on this website, but instead of applying it t ...

What causes the discrepancy in calculating marginTop on a desktop browser compared to a mobile browser?

In the top screenshot, you can see a representation of my Pixel 6XL connected to my laptop in USB debug mode. The checkered area represents the URL bar on the Chrome browser displayed on my Pixel device. Below that, the second screenshot shows the view fr ...

Can you explain the distinction between x++ and ++x for me?

Similar Question: Incrementing in C++ - x++ or ++x? Can you explain the distinction between using x++ and ++x in programming? ...

Pressing the non-responsive button automatically chooses the following item

This example demonstrates how to create a disabled button: import { Grid, IconButton } from "@material-ui/core"; import ArrowBackIosIcon from "@material-ui/icons/ArrowBackIos"; export default function App() { const handleClick = (e) ...

Can you provide me with a variable that will give me the total size of the scrollbar?

How can I determine the maximum number of pixels scrolled on a webpage? I attempted using window.pageXOffset, but it only gives the current scrolled amount. I require the total size at all times. ...