Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>mouse spin cube</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <style>
- body {
- background-color: white;
- margin: 0px;
- overflow: hidden;
- }
- </style>
- </head>
- <body>
- <script src="../build/three.min.js"></script>
- <script>
- var container;
- var camera, scene, renderer;
- var cube, plane;
- var targetRotation = 0;
- var targetRotationOnMouseDown = 0;
- var mouseX = 0;
- var mouseXOnMouseDown = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.y = 150;
- camera.position.z = 500;
- scene = new THREE.Scene();
- // Cube
- var geometry = new THREE.CubeGeometry( 200, 200, 200 );
- for ( var i = 0; i < geometry.faces.length; i ++ ) {
- geometry.faces[ i ].color.setHex( Math.random() * 0xffffff );
- }
- var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors } );
- cube = new THREE.Mesh( geometry, material );
- cube.position.y = 150;
- scene.add( cube );
- // Plane
- var geometry = new THREE.PlaneGeometry( 200, 200 );
- geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
- var material = new THREE.MeshBasicMaterial( { color: 0xC0C0C0 } );
- plane = new THREE.Mesh( geometry, material );
- scene.add( plane );
- renderer = new THREE.CanvasRenderer();
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- document.addEventListener( 'mousedown', onDocumentMouseDown, false );
- document.addEventListener( 'touchstart', onDocumentTouchStart, false );
- document.addEventListener( 'touchmove', onDocumentTouchMove, false );
- //
- }
- function onDocumentMouseDown( event ) {
- event.preventDefault();
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'mouseup', onDocumentMouseUp, false );
- document.addEventListener( 'mouseout', onDocumentMouseOut, false );
- mouseXOnMouseDown = event.clientX - windowHalfX;
- targetRotationOnMouseDown = targetRotation;
- }
- function onDocumentMouseMove( event ) {
- mouseX = event.clientX - windowHalfX;
- targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
- }
- function onDocumentMouseUp( event ) {
- document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
- document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
- document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
- }
- function onDocumentMouseOut( event ) {
- document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
- document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
- document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
- }
- function onDocumentTouchStart( event ) {
- if ( event.touches.length === 1 ) {
- event.preventDefault();
- mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
- targetRotationOnMouseDown = targetRotation;
- }
- }
- function onDocumentTouchMove( event ) {
- if ( event.touches.length === 1 ) {
- event.preventDefault();
- mouseX = event.touches[ 0 ].pageX - windowHalfX;
- targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
- }
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- plane.rotation.y = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.02;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement