Three.JS鼠标移动元素

时间:2022-05-06 04:22:40

添加引用

<script src="TrackballControls.js"></script>
<script src="DragControls.js"></script>

var controls;
controls = new THREE.TrackballControls( camera );
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;

var objects = [];//将需要拖拽的元素放进这个集合中
objects.push(元素);

创建拖拽控制并添加事件监听
var dragControls = new THREE.DragControls( objects, camera, renderer.domElement );
dragControls.addEventListener( 'dragstart', function ( event ) { controls.enabled = false; } );
dragControls.addEventListener( 'dragend', function ( event ) { controls.enabled = true; } );

渲染

function render() {

controls.update();

renderer.render( scene, camera );

}