Aframe click button with eventlistener

a-frame click event
aframe touch controls
a frame gaze
aframe click event not working
aframe move element
a-frame collision
aframe tracked-controls
a-frame laser-controls

I try to execute an action when user press a button (An event listener needed because https://webkit.org/blog/6784/new-video-policies-for-ios/), but I guess, I have some syntax/logic mistake. Some help would be nice :D.

Here is the code:

HTML:

   <a-image id="playbutton_1" position="-5 0 0" width="2" height="2" rotation="0 -90 0" ></a-image>

JS:

var playthisvid_1 = document.querySelector('#playbutton_1');

    playthisvid_1.addEventListener('click', () => { console.log("it clicks");  })

Aframe elements won't simply react to touch / mouse events. You need a ray based cursor.

A good way to simulate the mouse is with the cursors attribute:

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

It should also work with touch events on mobile devices.

Check it out here.


Otherwise use the a-frame cursor.

<a-cursor></a-cursor>

Interactions & Controllers – A-Frame, AR.js is built at the top of aframe and it's not easy to understand how to handle our component and, at the initialization, we add an event listener at 'click'. The addEventListener () method attaches an event handler to an element without overwriting existing event handlers. You can add many event handlers to one element. You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements. i.e the window object.

I think the problem is with aframe. The javascript code is working perfect. I just replaced the aframe element with a normal button and the function is working.

var playthisvid_1 = document.querySelector('#playbutton_1');

    playthisvid_1.addEventListener('click', () => { console.log("it clicks");  })
<a id="playbutton_1" href="#">Test</a>

Testing A-Frame On-Click Event Listener, How to make clickable 3D objects in Aframe. In this article, we make a cube that changes color when you click on it and also changes color when you hover over What we are going to do is add an event listener to our cube. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more add event listener on elements created dynamically

check this out for a complete example:

https://curious-electric.com/w/experiments/aframe/audiosprite/

It is an audio example, but the same technique applies for video (i.e. get the user to click a button first).

Best, Dirk

How to handle click events on AR.js - Chialab Open Source, We can use click/touch interactions through Raycaster, following A-Frame Docs. We can register an event listener for any a-frame entity. CSS to absolute positioning the DIV and BUTTON, and also some scripting to listen to click events​. The addEventListener () method attaches an event handler to the specified element. Tip: Use the removeEventListener () method to remove an event handler that has been attached with the addEventListener () method. Tip: Use the document.addEventListener () method to attach an event handler to the document. Browser Support.

How To Make A Button In AFrame, Example. Add an event listener that fires when a user clicks a button: document.​getElementById("myBtn").addEventListener("click", displayDate);. Try it Yourself  The problem is that the value of someVar is not visible inside the listener function of the addEventListener, where it is probably being treated as a new variable. Not the cleanest way, but does the job. Note that if someVar could only be digit or text: eval ('someObj.addEventListener ("click",function () {some_function ('+someVar+

UI and Events - AR.js Documentation, On touch enabled devices, tapping the screen causes the click event to be called twice - once due to a TouchEvent and once due to a  <button (click)="doSomething()">Do Something</button> This looks like an inline event, but it isn't. This type of template will be transpiled into more complex code which uses event listeners behind the scenes. Everything I've written about events here still applies, but you are removed from the nitty gritty by at least one layer.

JavaScript DOM EventListener, Buttons have click events, input and select controls have change events, and virtually This creates what's referred to as a frame on the call stack. get the reference to the button, and assign the event listener, handleClick , by passing it in as  Click the Launch button to run InternalFrameEventDemo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index. Bring up the Event Generator internal frame by clicking the Show internal frame button. You should see an "Internal frame opened" message in the display area.

Comments
  • i get this Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission. on iphone if i try to play a video with the listener any idea why it appears?
  • is it on safari, or other browsers ? If not, can you check if it works on other browsers on iOS ?
  • dosn´t work on chrome/safari on iphone (desktop everthings fine)
  • stackoverflow.com/questions/51878346/… here some more informations
  • thx now it works. The problem was the button in your examplepage isn´t in the a-scene its a normal html-element. So i changed my <a-image> to an normal <image> and it works. Only problem i can´t gaze on them now with my cardboard.
  • thx for the information looks like i need a workaround for aframe :)
  • Yes :) no problem
  • thx i tried to use it but it still won´t work with <a-image> the console says the value of my button is always zero.
  • var vrButton = document.querySelector('#playbutton_1'); var enter = function () { console.log("it clicks"); } vrButton.addEventListener('click', enter);
  • Could you post your full example in a codepen or jsFiddle?