canvas_geometry_cube.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. //
  69. function onDocumentMouseDown( event ) {
  70. event.preventDefault();
  71. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  72. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  73. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  74. mouseXOnMouseDown = event.clientX - windowHalfX;
  75. targetRotationOnMouseDown = targetRotation;
  76. }
  77. function onDocumentMouseMove( event ) {
  78. mouseX = event.clientX - windowHalfX;
  79. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  80. }
  81. function onDocumentMouseUp( event ) {
  82. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  83. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  84. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  85. }
  86. function onDocumentMouseOut( event ) {
  87. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  88. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  89. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  90. }
  91. function onDocumentTouchStart( event ) {
  92. if ( event.touches.length == 1 ) {
  93. event.preventDefault();
  94. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  95. targetRotationOnMouseDown = targetRotation;
  96. }
  97. }
  98. function onDocumentTouchMove( event ) {
  99. if ( event.touches.length == 1 ) {
  100. event.preventDefault();
  101. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  102. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  103. }
  104. }
  105. //
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. render();
  109. stats.update();
  110. }
  111. function render() {
  112. plane.rotation.y = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
  113. renderer.render( scene, camera );
  114. }
  115. </script>
  116. </body>
  117. </html>