获取相机位置 javascript
Get camera position javascript
我想在我的 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-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>
但是条件 if (evt.name === 'position') 永远不会成立,并且相机位置保持为 0,即使在周围移动时也是如此带键盘输入的相机位置。如何连续获取相机位置?
我认为是evt.detail.name === 'position'
。
我还建议使用 A 型框架组件而不是松散 JavaScript:
AFRAME.registerComponent('listener', {
tick: function () {
console.log(this.el.getAttribute('position'));
}
});
HTML:
<a-camera listener></a-camera>
我想在我的 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-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>
但是条件 if (evt.name === 'position') 永远不会成立,并且相机位置保持为 0,即使在周围移动时也是如此带键盘输入的相机位置。如何连续获取相机位置?
我认为是evt.detail.name === 'position'
。
我还建议使用 A 型框架组件而不是松散 JavaScript:
AFRAME.registerComponent('listener', {
tick: function () {
console.log(this.el.getAttribute('position'));
}
});
HTML:
<a-camera listener></a-camera>