Javascript 获取摄像机位置
我想在我的Aframe应用程序中获取相机的位置,但它始终为0,尽管我移动了相机 我尝试了事件侦听器和位置的常规检索,但没有得到更新的值 现场如下:Javascript 获取摄像机位置,javascript,aframe,webvr,Javascript,Aframe,Webvr,我想在我的Aframe应用程序中获取相机的位置,但它始终为0,尽管我移动了相机 我尝试了事件侦听器和位置的常规检索,但没有得到更新的值 现场如下: <a-scene> <a-entity id="cameraWrapper" position="0 2 10" rotation="0 0 0"> <a-camera near="0.1" user-height="0" id="camera"></a-camera> </a-e
<a-scene>
<a-entity id="cameraWrapper" position="0 2 10" rotation="0 0 0">
<a-camera near="0.1" user-height="0" id="camera"></a-camera>
</a-entity>
<a-plane position="0 4 4" rotation="-90 0 -90" width="4" height="4" color="#7BC8A4"></a-plane>
<a-sky color="#ECECEC"></a-sky>
</a-scene>
以下是监控摄像头位置的javascript代码:
<script>
window.onload = function () {
var pos =
document.querySelector('#camera').getAttribute('position');
document.querySelector('[camera]').addEventListener('componentchanged', function (evt) {
console.log('CAMERA x/y:', pos.x, pos.y);
if (evt.name === 'position') {
console.log('Entity has moved from POSITION', evt.oldData, 'to', evt.newData, '!');
return;
}
return;
});
};
</script>
window.onload=函数(){
var pos=
document.querySelector('#camera').getAttribute('position');
document.querySelector(“[camera]”)。addEventListener('componentchanged',function(evt){
控制台日志('摄像头x/y:',位置x,位置y);
如果(evt.name=='position'){
log('实体已从'evt.oldData'位置移动到'evt.newData'!');
回来
}
回来
});
};
但是如果(evt.name=='position')条件永远不会为真,并且相机位置保持为0,即使在使用键盘输入移动相机位置时也是如此。如何连续获取相机位置?我认为它是
evt.detail.name=='position'
我还建议使用A-Frame组件,而不是松散的JavaScript:
AFRAME.registerComponent('listener', {
tick: function () {
console.log(this.el.getAttribute('position'));
}
});
HTML:
我想它是
evt.detail.name=='position'
我还建议使用A-Frame组件,而不是松散的JavaScript:
AFRAME.registerComponent('listener', {
tick: function () {
console.log(this.el.getAttribute('position'));
}
});
HTML: