当我使用 Leap Motion 进行抓取手势时,我试图在 X3DOM 中模拟鼠标点击。例如,我有两个立方体,当我对它进行抓取手势时,我想选择红色立方体。changeColor() 是 click 函数的事件处理程序,它将立方体的颜色从红色变为绿色,反之亦然(使用虚拟手,如本例所示:http ://examples.x3dom.org/Demos/ClassroomVR/classroom -rift-leap-webvr.html )
现在,抓取手势确实会触发点击事件,但无论何时/无论我在何处进行抓取,都会触发它。我希望它仅在我对该特定红色立方体执行抓取手势时触发。任何建议将不胜感激。谢谢。
<x3d>
<scene>
<Transform id = "boxTrafo1" DEF="boxTrafo1" translation="-1 0 0">
<Shape id = "boxShape1" DEF="boxShape1" onclick = "changeColor(event);">
<Appearance DEF="boxApp1">
<Material id ="boxMat1" diffuseColor="1 0 0" specularColor=".5 .5 .5" ></Material>
</Appearance>
<Box size="4.5 4.5 4.5"></Box>
</Shape>
</Transform>
<Transform translation='-11 0 3' id="boxTrafo2" >
<Shape id = "boxShape2">
<Appearance id = "boxApp2">
<Material id ="boxMat2" diffuseColor='0 1 0' specularColor='.5 .5 .5'></Material>
</Appearance>
<Box size="4.5 4.5 4.5"></Box>
</Shape>
</Transform>
</scene>
</x3d>
<script>
var controller = Leap.loop({enableGestures: true}, function (frame)
{
if (frame.hands.length)
{
var hand_frame = frame.hands[0];
if (hand_frame.grabStrength === 1) {
var box = document.getElementById("boxShape1");
box.click();
}
}
});
function changeColor(event) {
if (document.getElementById("boxMat1").getAttribute("diffuseColor") === "1 0 0")
document.getElementById("boxMat1").setAttribute("diffuseColor", "0 1 0");
else
document.getElementById("boxMat1").setAttribute("diffuseColor", "1 0 0");
}
</script>