【发布时间】:2017-08-16 11:37:14
【问题描述】:
我试图仅调整对象的 Y 坐标,同时保持 X 和 Z 坐标相同。我工作的唯一方法是获取对象的位置并组成一个新的部分修改的位置。它不漂亮,而且似乎效率低下。看起来这可能是因为position is a single-property component,所以这可能是不可能的。
这是我目前的解决方案:
https://glitch.com/edit/#!/aframe-position-example
index.html:
<html>
<head>
<script src="https://aframe.io/releases/0.6.0/aframe.min.js"></script>
</head>
<body>
<a-scene>
<a-camera id="camera"></a-camera>
</a-scene>
<script src="index.js"></script>
</body>
</html>
index.js:
const camera = document.querySelector( '#camera' )
console.log( camera.getAttribute( 'position' ) )
// {x: 0, y: 1.6, z: 0}
// overwrites original position, as expected
camera.setAttribute( 'position', { x: 0, y: 0, z: 5 } )
console.log( camera.getAttribute( 'position' ) )
// {x: 0, y: 0, z: 5}
// overwrites original position (including z, defaults to 0), maybe not expected
camera.setAttribute( 'position', { x: 5, y: 5 } )
console.log( camera.getAttribute( 'position' ) )
// {x: 5, y: 5, z: 0}
// overwrites original position (x and z become 0), maybe not expected
camera.setAttribute( 'position', 'y', 10 )
console.log( camera.getAttribute( 'position' ) )
// {x: 0, y: 10, z: 0}
// how to change some position variables and keep the other ones the same
let oldPos = camera.getAttribute('position')
let newPos = { x: 4, y: oldPos.y, z: oldPos.z }
camera.setAttribute( 'position', newPos )
console.log( camera.getAttribute( 'position' ) )
// {x: 4, y: 10, z: 0}
【问题讨论】:
标签: javascript 3d aframe webvr