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

问题描述 投票:9回答:1

我正在使用React VR编写VR应用程序,并使用进度条(或其他东西)制作注视按钮,向用户显示他必须在该按钮上观看多长时间。我怎么能这样做?

我想使用这个伪代码(可能是这段代码中有一些bug):

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
}

有更简单的方法吗?

webvr react-360 gaze-buttons
1个回答
6
投票

您需要为项目添加一些内容。

  1. 使用安装simple raycaster npm install --save simple-raycaster vr/client.js里面添加以下代码: 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 }; 资料来源:npm simple-raycaster
  2. index.vr.js内使用此代码: 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() { // ... } 资料来源:andrewimm at GitHub facebook/react-vr
© www.soinside.com 2019 - 2024. All rights reserved.