I have some code written in a-frame (https://aframe.io) that can be found at https://jsfiddle.net/AidanYoung/h0cb1nq8/ And I'm trying to find a way to add an onclick event to the code below:
<a-entity
id="lettersEntity"
onclick='console.log("Hello")'
geometry="primitive: plane; width: 2; height: 2"
rotation="-30 -30 0"
position="-0.2 1.5 -2.5"
material="shader: html; target: #target; transparent: true; ratio: width; fps: 1.5"
></a-entity>
I haven't been able to find a way to make it so when I click on the object a function is triggered. I have tried adding a simple onclick
function to the html element but I've been unsuccessful. Does anybody know how I can add an onclick
function to the element above?
Actually It was not working because
Update your code with the below one. I hope this will work. I enclosed your a-frame tag within p tag and add event listner on it after page load.
window.addEventListener('load', function()
{
var plane = document.getElementById("lettersEntity_2");
plane.addEventListener("click", myFunction);
function myFunction()
{
console.log ("hi")
}
});
function myFunction()
{
console.log("hi")
}
#target {
width: 512px;
height: 256px;
position: absolute;
background: rgba(255,255,0,0.3);
}
/* Hide the HTML using z-index. */
/* Can't use display: none because that would hide the HTML in the rendered material. */
#htmlTarget.hide {
z-index: -1;
}
#target h1 {
font-family: Arial;
font-size: 110px;
margin: 0;
vertical-align: top;
color: white;
}
#target h1 span {
color: tomato;
}
#emoji {
position: absolute;
font-size: 512px;
color: mediumTurquoise;
font-style: serif;
}
#pre {
font-family: monospace;
margin: 0;
padding: 0;
height: 50px;
font-size: 50px;
background: royalblue;
color: tomato;
}
#htmlTarget {
position: absolute;
z-index: 1;
height: 100%;
width: 100%;
overflow: hidden;
position: fixed;
top: 0;
}
#debug {
position: absolute;
bottom: 0;
left: 0;
}
/* Even works with media queries. */
@media (max-width: 768px) {
#target {
width: 256px;
height: 126px;
}
#target h1 {
font-size: 55px;
}
#pre {
height: 50px;
font-size: 25px;
}
}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1,width=device-width">
<title>A-Frame HTML Shader - Dynamic</title>
<script src="https://aframe.io/releases/0.5.0/aframe.min.js"></script>
<script src="https://unpkg.com/aframe-html-shader@0.2.0/dist/aframe-html-shader.min.js"></script>
</head>
<body>
<p id="lettersEntity_2">
<a-scene update-html>
<a-entity id="lettersEntity" onclick='console.log("Hello")' geometry="primitive: plane; width: 2; height: 2" rotation="-30 -30 0" position="-0.2 1.5 -2.5" material="shader: html; target: #target; transparent: true; ratio: width; fps: 1.5"></a-entity>
<a-sky color="pink"></a-sky>
</a-scene>
</p>
<div id="htmlTarget" class="hide">
<div id="emoji"></div>
<div id="target">
<h1>HELLO</h1>
<button>
i1
</button>
</div>
</div>
</body>
</html>