A-frame 添加 onclick 函数到着色器
A-frame add onclick function to a shader
我有一些代码是用框架写的(https://aframe.io) that can be found at https://jsfiddle.net/AidanYoung/h0cb1nq8/
我正在尝试找到一种方法将 onclick 事件添加到以下代码中:
<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>
我一直没能找到实现它的方法,所以当我单击该对象时会触发一个函数。我曾尝试向 html 元素添加一个简单的 onclick
函数,但没有成功。有人知道如何向上面的元素添加 onclick
函数吗?
实际上它没有用,因为
- onclick事件监听器不支持这种方式在a-frame上。
- 您在页面加载完成之前添加了事件侦听器。
使用以下代码更新您的代码。我希望这会奏效。我将您的 a-frame 标签包含在 p 标签中,并在页面加载后在其上添加事件监听器。
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>
我有一些代码是用框架写的(https://aframe.io) that can be found at https://jsfiddle.net/AidanYoung/h0cb1nq8/ 我正在尝试找到一种方法将 onclick 事件添加到以下代码中:
<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>
我一直没能找到实现它的方法,所以当我单击该对象时会触发一个函数。我曾尝试向 html 元素添加一个简单的 onclick
函数,但没有成功。有人知道如何向上面的元素添加 onclick
函数吗?
实际上它没有用,因为
- onclick事件监听器不支持这种方式在a-frame上。
- 您在页面加载完成之前添加了事件侦听器。
使用以下代码更新您的代码。我希望这会奏效。我将您的 a-frame 标签包含在 p 标签中,并在页面加载后在其上添加事件监听器。
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>