javascript - 合并图像库和基本场景教程时,aframe 光标不触发

标签 javascript html aframe

我是a-frame新手,一直在学习基本场景和图库的教程,当我尝试将两个代码合并在一起时,我无法触发基本场景的a-box动画。该框没有拾取 mouseenter mouseleave 并从我的光标处单击。不过,我的图片库运行良好。请指教。感谢你的帮助。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>360&deg; Image Gallery</title>
    <meta name="description" content="360&deg; Image Gallery - A-Frame">
    <script src="https://aframe.io/releases/0.7.0/aframe.min.js"></script>
    <script src="https://npmcdn.com/aframe-animation-component@3.0.1"></script>
    <script src="https://npmcdn.com/aframe-event-set-component@3.0.1"></script>
    <script src="https://npmcdn.com/aframe-layout-component@3.0.1"></script>
    <script src="https://npmcdn.com/aframe-template-component@3.1.1"></script>
    <script src="components/set-image.js"></script>
    <meta name="apple-mobile-web-app-capable"/>
  </head>
  <body>
    <a-scene>
      <a-assets>
        <img id="city" crossorigin="anonymous" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/city.jpg">
        <img id="city-thumb" crossorigin="anonymous" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/thumb-city.jpg">
        <img id="cubes-thumb" crossorigin="anonymous" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/thumb-cubes.jpg">
        <img id="sechelt-thumb" crossorigin="anonymous" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/thumb-sechelt.jpg">
        <audio id="click-sound" crossorigin="anonymous" src="https://cdn.aframe.io/360-image-gallery-boilerplate/audio/click.ogg"></audio>
        <img id="cubes" crossorigin="anonymous" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/cubes.jpg">
        <img id="sechelt" crossorigin="anonymous" src="https://cdn.aframe.io/360-image-gallery-boilerplate/img/sechelt.jpg">
         <img id="test" crossorigin="anonymous" src="img/1beachdemo.jpg">

        <!--basic scene asset -->
        <img id="boxTexture" src="https://i.imgur.com/mYmmbrp.jpg"/>


        <!--basic scene -->
        <script id="box" type="text/html">
          <a-entity class="box"

            event-set__1="_event: mousedown; scale: 1 1 1"
            event-set__2="_event: mouseup; scale: 1.2 1.2 1"
            event-set__3="_event: mouseenter; scale: 1.2 1.2 1"
            event-set__4="_event: mouseleave; scale: 1 1 1"

            sound="on: click; src: #click-sound"></a-entity>
        </script>

        <!-- Image link template to be reused. -->
        <script id="link" type="text/html">
          <a-entity class="link"
            geometry="primitive: plane; height: 1; width: 1"
            material="shader: flat; src: ${thumb};side:double"
            event-set__1="_event: mousedown; scale: 1 1 1"
            event-set__2="_event: mouseup; scale: 1.2 1.2 1"
            event-set__3="_event: mouseenter; scale: 1.2 1.2 1"
            event-set__4="_event: mouseleave; scale: 1 1 1"
            set-image="on: click; target: #image-360; src: ${src}"
            sound="on: click; src: #click-sound"></a-entity>
        </script>


      </a-assets>

      <!-- 360-degree image. -->
      <a-sky id="image-360" radius="10" src="#city"></a-sky>

      <!-- Image links. -->
      <a-entity id="links" layout="type: line; margin: 1.5" position="-1.5 -1 -4">
        <a-entity template="src: #link" data-src="#cubes" data-thumb="#cubes-thumb"></a-entity>
        <a-entity template="src: #link" data-src="#city" data-thumb="#city-thumb"></a-entity>
        <a-entity template="src: #link" data-src="#sechelt" data-thumb="#sechelt-thumb"></a-entity>
      </a-entity>

      <a-entity id="links" layout="type: line; margin: 1.5" position="-1 -2.5 4" rotation="-85 180 0">
      <a-entity template="src: #link" data-src="#test" data-thumb="#cubes-thumb"></a-entity>
        </a-entity>

    <!--basic scene insert need help here. mouse event here could not be trigger-->
        <a-entity id="box">
            <a-box src="#boxTexture" position="0 2 -5" scale="2 2 2" rotation="0 45 45">
                <a-animation attribute="position" to="0 2.2 -5" direction="alternate" dur="2000" repeat="indefinite"></a-animation>
                <a-animation attribute="scale" begin="mouseenter" dur="200" to="2.3 2.3 2.3"></a-animation>
                <a-animation attribute="scale" begin="mouseleave" dur="200" to="2 2 2"></a-animation>
                <a-animation attribute="rotation" begin="click" dur="2000" to=" 360 405 45"></a-animation>

            </a-box>
        </a-entity>


      <!-- Camera + cursor. -->
      <a-entity camera look-controls  wasd-controls position="0 0 1" rotation="0 0 0">
        <a-cursor id="cursor"
          animation__click="property: scale; startEvents: click; from: 0.1 0.1 0.1; to: 1 1 1; dur: 150"
          animation__fusing="property: fusing; startEvents: fusing; from: 1 1 1; to: 0.1 0.1 0.1; dur: 1500"
          event-set__1="_event: mouseenter; color: springgreen"
          event-set__2="_event: mouseleave; color: black"
          fuse="true"
          raycaster="objects: .link"></a-cursor>

      </a-entity>
      <!--basic scene 
      <a-camera>
                <a-cursor color="lime"></a-cursor>
            </a-camera>
      -->
    </a-scene>
  </body>
</html>

最佳答案

对于初学者来说,您有两个“盒子”id 和两个“链接”id。

id attribute应该是一个独特的签名。 如果您想拥有更“通用”的句柄,请使用 class attribute .

<小时/>

其次,您的光线转换器有一个白名单。它应该适用于具有 .link 类属性的任何实体。该框没有 .link 类,因此不会触发。

要么将“链接”类添加到框中(从长远来看,这可能会产生误导),要么将框添加到白名单中。

<a-box class="link">

<a-box class="clickable">
(.....)
raycaster="objects: .link .clickable"></a-cursor>
<小时/>

尝试分析您正在复制粘贴的内容,通过简单地将两段代码复制到另一段代码来合并两段代码并不是一个好主意。

我已经改变了它,它似乎有效。
直播here .
代码here .

关于javascript - 合并图像库和基本场景教程时,aframe 光标不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47019990/

相关文章:

image - Aframe - 实体前透明的 PNG

javascript - jQuery - 获取屏幕尺寸宽度

jquery - HTML只是无法获得焦点而不是被禁用

javascript - 3D模型的A帧抖动

aframe - 3d.io 查看器中的建筑 Material 预设选择

html - 如何在图像元素上获得绝对定位?

javascript - 如何允许轻松覆盖 JavaScript 函数中的默认选项?

javascript:从匿名函数返回值

javascript - 从外部 SVG 调用全局定义的 JavaScript 函数

jQuery - 单击按钮更改 Div 的高度