首页 > 解决方案 > A-frame 将 onclick 函数添加到着色器

问题描述

我有一些用 a-frame ( https://aframe.io )编写的代码,可以在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>

我一直无法找到一种方法来实现它,因此当我单击对象时会触发一个功能。我尝试向onclickhtml 元素添加一个简单的函数,但没有成功。有人知道我如何onclick向上面的元素添加函数吗?

标签: javascripthtmljquerycssaframe

解决方案


实际上它不起作用,因为

  1. onclick事件监听器不支持以这种方式在 a-frame 上。
  2. 您在完成页面加载之前添加了事件侦听器。

使用以下代码更新您的代码。我希望这会奏效。我将您的 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>


推荐阅读