繁体   English   中英

通过JavaScript进行A帧动画

[英]A-Frame animations via JavaScript

我从A-Frame VR框架开始。 我有一个想要动画的简单盒子。

此框将是浮动的,当查看该框时,它将放大,单击时将旋转:

<a-box id="boxID" position="0 2 -5" rotation="0 45 45" scale="2 2 2" src="#boxTexture">
            <a-animation attribute="position" to="0 2.2 -5" direction="alternate" dur="2000" repeat="indefinite"></a-animation>

<!-- These animations will start when the box is looked at. -->
<a-animation attribute="scale" begin="mouseenter" dur="300" to="2.3 2.3 2.3"></a-animation>
<a-animation attribute="scale" begin="mouseleave" dur="300" to="2 2 2"></a-animation>
<a-animation attribute="rotation" begin="click" dur="2000" to="360 405 45"></a-animation>

我想在上面用Javascript创建。 我从以下内容开始:

AFRAME.registerComponent('scale-on-interact', {
    schema: {
        to: {default: '2.5 2.5 2.5'}
    }, 

    init: function () {
        var data = this.data;

    }, 

    update: function() {
        var data = this.data;
        console.log(data.color);

        // MOUSE ENTER EVENT
        this.el.addEventListener('mouseenter', function() {
            console.log("enter");
            this.setAttribute('to', data.to);
        });

        // CLICK EVENT
        this.el.addEventListener('click', function() {
            console.log("click");
        });

        // MOUSE LEAVE EVENT
        this.el.addEventListener('mouseleave', function() {
            console.log("leave");
        });
    }
});

我收到了日志,但是例如在鼠标输入时,该框没有扩大。 我也不知道,也找不到如何创建多个模式,因此我可以在鼠标输入时创建一个“ to”属性,在点击时创建一个“ to”属性。

首先, <a-animation>Animation Component都具有beginstartEvents属性, startEvents属性指定实体上将自动触发动画的事件。 <a-animation begin="mouseenter"><a-entity animation__1="property: scale; to: 2 2 2; startEvents: click">

其次,有一个event-set组件可以执行您想要的操作,设置属性以响应事件。 <a-entity event-set__1="_event: mouseenter; scale: 2 2 2">

第三,在组件中,如果要设置比例,则应该setAttribute('scale', {x: 2, y: 2, z: 2}) ,而不是to 此外,还要确保该组件已附加到您的实体<a-box id="boxID" scale-on-interact>

最后,如果您希望一个组件能够具有多个实例,请在您的组件中设置multiple: true 然后,您可以使用__分隔符设置多个组件: <a-box id="boxID" component__1="foo: 1; bar: 2" component__two="foo: 2; bar: 3">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM