如何使用React VR创建注视按钮?

9
我正在使用React VR编写VR应用,希望制作注视按钮,并添加进度条(或其他东西)以告诉用户他需要看多久才能按下该按钮。我该怎么做呢?
我考虑使用以下伪代码(可能存在一些错误):
constructor(props) {
    super(props);
    this.state = {
        watchTime: 3,
        progress: 0,
        watching: true
    };
}

render() {
    return (
        <VrButton onEnter={ () => this.animateProgress() }
                  onExit={ () => this.stopProgress() }
                  onClick={ ()=> this.click() }></VrButton>
    );
}

animateProgress() {
    this.setState({watching: true});
    while (this.state.watchTime >== this.state.progress && this.state.watching === true) {
        // after a timeout of one second add 1 to `this.state.progress`
    }

    this.click();
}

stopProgress() {
    this.setState({
        progress: 0,
        watching: false
    });
}

click() {
    // Handels the click event
}

有没有更简单的方法来完成这个任务?

1个回答

6
你需要在你的项目中添加一些内容。
  1. Install a simple raycaster using

    npm install --save simple-raycaster
    

    Inside vr/client.js add this code:

    import { VRInstance } from "react-vr-web";
    import * as SimpleRaycaster from "simple-raycaster";
    
    function init(bundle, parent, options) {
      const vr = new VRInstance(bundle, "librarytests", parent, {
        raycasters: [
          SimpleRaycaster // Add SimpleRaycaster to the options 
        ],
        cursorVisibility: "auto", // Add cursorVisibility 
        ...options
      });
      vr.start();
      return vr;
    }
    
    window.ReactVR = { init }; 
    

    Source: npm simple-raycaster

  2. Inside the index.vr.js use this code:

    constructor(props) {
      super(props);
      this.click = this.click.bind(this); // make sure this.click is in the right context when the timeout is called
    }
    
    render() {
      return (
        <VrButton onEnter={ () => this.animateProgress() }
                  onExit={ () => this.stopProgress() }
                  onClick={ ()=> this.click() }></VrButton>
      );
    }
    
    animateProgress() {
      this.timeout = this.setTimeout(this.click, 1000); // or however many milliseconds you want to wait
      // begin animation
    }
    
    stopProgress() {
      clearTimeout(this.timeout);
      this.timeout = null;
      // end animation
    }
    
    click() {
      // ...
    }
    

    Source: andrewimm at GitHub facebook/react-vr


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接