如你所見,上面的cube的旋轉、加速、減速停止都是通過AlloyTouch去實現的。
演示
代碼
<script src="asset/three.js"></script>
<script src="../../alloy_touch.js"></script>
<script>
var camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 500;
var scene = new THREE.Scene();
var texture = new THREE.TextureLoader().load( 'asset/crate.gif' );
//幾何體
var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
//材質
var material = new THREE.MeshBasicMaterial( { map: texture } );
var mesh = new THREE.Mesh( geometry, material );
//添加到舞台
scene.add( mesh );
var renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
animate();
new AlloyTouch({
touch: document, //觸摸整個文檔
vertical: false, //監聽橫向觸摸
target: mesh.rotation, //運動 mesh.rotation
property: "y", //被運動的屬性 y
factor: 0.08, //運動期間的摩擦力
moveFactor: 0.2 //拖拽期間的摩擦力
})
</script>
factor需要自己不斷去調試出最佳的值,讓松手之后的慣性運動的速率和時間達到最佳的效果。
moveFactor需要自己不斷去調試出最佳的值,就是讓橫向拖拽的距離映射到旋轉的角度上達到最跟手的效果。
如果,不需要慣性運動。比如像王者榮耀里的任務旋轉就是沒有慣性的,手指離開屏幕就會立馬停止運動。如:
你只需要在new AlloyTouch設置inertia為false便可。
無慣性演示
無慣性代碼
<script src="asset/three.js"></script>
<script src="../../alloy_touch.js"></script>
<script>
...
...
...
animate();
new AlloyTouch({
touch: document, //觸摸整個文檔
vertical: false, //監聽橫向觸摸
target: mesh.rotation, //運動 mesh.rotation
property: "y", //被運動的屬性 y
factor: 0.08, //運動期間的摩擦力
moveFactor: 0.2 , //拖拽期間的摩擦力
inertia: false //禁止慣性運動
})
</script>
開始AlloyTouch吧
Github地址:https://github.com/AlloyTeam/AlloyTouch
歡迎issues:https://github.com/AlloyTeam/AlloyTouch/issues