canvas_geometry_cube.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry - cube</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/Three.js"></script>
  18. <script src="js/Stats.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, scene, renderer;
  22. var cube, plane;
  23. var targetRotation = 0;
  24. var targetRotationOnMouseDown = 0;
  25. var mouseX = 0;
  26. var mouseXOnMouseDown = 0;
  27. var windowHalfX = window.innerWidth / 2;
  28. var windowHalfY = window.innerHeight / 2;
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. var info = document.createElement( 'div' );
  35. info.style.position = 'absolute';
  36. info.style.top = '10px';
  37. info.style.width = '100%';
  38. info.style.textAlign = 'center';
  39. info.innerHTML = 'Drag to spin the cube';
  40. container.appendChild( info );
  41. scene = new THREE.Scene();
  42. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.position.y = 150;
  44. camera.position.z = 500;
  45. scene.add( camera );
  46. // Cube
  47. var materials = [];
  48. for ( var i = 0; i < 6; i ++ ) {
  49. materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
  50. }
  51. cube = new THREE.Mesh( new THREE.CubeGeometry( 200, 200, 200, 1, 1, 1, materials ), new THREE.MeshFaceMaterial() );
  52. cube.position.y = 150;
  53. scene.add( cube );
  54. // Plane
  55. plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  56. scene.add( plane );
  57. renderer = new THREE.CanvasRenderer();
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. container.appendChild( renderer.domElement );
  60. stats = new Stats();
  61. stats.domElement.style.position = 'absolute';
  62. stats.domElement.style.top = '0px';
  63. container.appendChild( stats.domElement );
  64. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  65. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  66. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  67. //
  68. window.addEventListener( 'resize', onWindowResize, false );
  69. }
  70. function onWindowResize() {
  71. windowHalfX = window.innerWidth / 2;
  72. windowHalfY = window.innerHeight / 2;
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. }
  77. //
  78. function onDocumentMouseDown( event ) {
  79. event.preventDefault();
  80. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  81. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  82. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  83. mouseXOnMouseDown = event.clientX - windowHalfX;
  84. targetRotationOnMouseDown = targetRotation;
  85. }
  86. function onDocumentMouseMove( event ) {
  87. mouseX = event.clientX - windowHalfX;
  88. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  89. }
  90. function onDocumentMouseUp( event ) {
  91. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  92. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  93. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  94. }
  95. function onDocumentMouseOut( event ) {
  96. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  97. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  98. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  99. }
  100. function onDocumentTouchStart( event ) {
  101. if ( event.touches.length === 1 ) {
  102. event.preventDefault();
  103. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  104. targetRotationOnMouseDown = targetRotation;
  105. }
  106. }
  107. function onDocumentTouchMove( event ) {
  108. if ( event.touches.length === 1 ) {
  109. event.preventDefault();
  110. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  111. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  112. }
  113. }
  114. //
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. render();
  118. stats.update();
  119. }
  120. function render() {
  121. plane.rotation.y = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
  122. renderer.render( scene, camera );
  123. }
  124. </script>
  125. </body>
  126. </html>