【问题标题】:Getting User input with javascript while using three.js?在使用three.js 时使用javascript 获取用户输入?
【发布时间】:2014-12-30 15:16:15
【问题描述】:

我正在处理这个 three.js 示例 (http://threejs.org/examples/#webgl_interactive_cubes),并试图找到一种方法让用户添加具有指定 X、Y 和 Z 位置的框。

我可以使用 javascript 提示符来做到这一点

var boxes = prompt("X Position", 500); // 500 = Default position

但是,我想让用户可以输入多个字段(例如 x、y、z 位置;框的大小等),所以我想添加输入框。我知道如何做到这一点的唯一方法是使用 html/css/javascript 与这样的东西 -

<!-- CSS formating of Input Boxes --> 
<style type = "text/css">
         #x_pos {
            position: absolute;
            bottom: 120px;
            left: 10px;
            width: 130px;
            background-color: #3c4543;
            border-top-left-radius: 5px;
            border-top-right-radius: 5px;
            border: 2px solid #000000;
            font-family: Futura; 
        }
        ... Repeat for #y_pos & #z_pos
</style>

<!-- Adding a div for each Input Box -->
<div id="x_pos">
    <input type="text" id="xpos">
</div>
... Repeat for #y_pos & #z_pos

<h1>Add Node here...</h1>

<!--Allowing user to add input to the Input Box and saving that value as the x, y, & z positions -->
<script text="text/javascript">

var xPosition;
$(document).ready(function(){            
    $('#xpos').val('Longitude');
    $("#xpos").change(function(){
        xPosition = $('#xpos').val();
    })
    ... Repeat for #ypos & #zpos
});

然而,虽然我可以让标题和输入框出现,但它们绝对没有任何功能。我无法在文本框中输入任何内容,也无法向 h1 文本添加 .click(function () ...) 功能。几乎就像我习惯的所有 html 和 javascript 功能都被其余的 three.js 代码禁用了一样。我的最终目标是让 .click 调用一个函数,我可以让我上面定义的 div 像这样 (http://jsfiddle.net/zsfE3/9/) 出现在 h1“在此处添加节点...”下方。

谁能向我解释这里发生了什么以及如何解决它?或者有没有人有更好的方法来做到这一点?感谢大家的帮助!

【问题讨论】:

  • 为什么不使用 DAT.GUI?

标签: javascript jquery html input three.js


【解决方案1】:

试试这样的:

html:

<input type = "text" id = "input1">
<button>ADD BOX</button>
</input>

css:

canvas{z-index:-1;}

#input1{

position:fixed;
right:40px;
top:40px;
z-index:1;    
width:5%;
height:60px;

}

祝你好运!

【讨论】:

    【解决方案2】:

    考虑为您的项目使用 datGUI 库。我和 Three.Js 一起取得了巨大的成功。还有很多例子也在使用它。有关教程,请参阅此 blog post

    【讨论】:

    • 感谢您的建议!您是否遇到过用户无法输入任何内容的问题?我现在有这个问题。我将此作为另一个提供更多详细信息的问题发布 - stackoverflow.com/questions/27240720/…
    • @DavidStreid 不,我从来没有遇到过这样的问题。确保你的 CSS 是正确的。 datGUI 可能是绝对定位的。检查您的 zIndex 是否在 WebGL 画布上方。如果对您有任何帮助,请考虑接受当前问题的答案。
    • 我接受了你的回答。我正在将 DAT.GUI.min.js 中的 css 元素的 z-index 更改为非常积极,希望这会将 gui 放在画布上方。我无法在代码或任何 .js 脚本源文件中找到 WebGL 画布的 zIndex。非常感谢您的指导。
    猜你喜欢
    • 2012-06-27
    • 2013-04-24
    • 2013-07-28
    • 1970-01-01
    • 2015-08-16
    • 2021-06-14
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    相关资源
    最近更新 更多