canvas_interactive_cubes_tween.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - interactive - cubes tween</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.min.js"></script>
  18. <script src="js/libs/stats.min.js"></script>
  19. <script src="js/libs/tween.min.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, scene, projector, renderer;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. var info = document.createElement( 'div' );
  29. info.style.position = 'absolute';
  30. info.style.top = '10px';
  31. info.style.width = '100%';
  32. info.style.textAlign = 'center';
  33. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> - clickable objects';
  34. container.appendChild( info );
  35. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  36. camera.position.y = 300;
  37. camera.position.z = 500;
  38. scene = new THREE.Scene();
  39. var geometry = new THREE.BoxGeometry( 100, 100, 100 );
  40. for ( var i = 0; i < 20; i ++ ) {
  41. var object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5 } ) );
  42. object.position.x = Math.random() * 800 - 400;
  43. object.position.y = Math.random() * 800 - 400;
  44. object.position.z = Math.random() * 800 - 400;
  45. object.scale.x = Math.random() * 2 + 1;
  46. object.scale.y = Math.random() * 2 + 1;
  47. object.scale.z = Math.random() * 2 + 1;
  48. object.rotation.x = Math.random() * 2 * Math.PI;
  49. object.rotation.y = Math.random() * 2 * Math.PI;
  50. object.rotation.z = Math.random() * 2 * Math.PI;
  51. scene.add( object );
  52. }
  53. projector = new THREE.Projector();
  54. renderer = new THREE.CanvasRenderer();
  55. renderer.setClearColor( 0xf0f0f0 );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. container.appendChild(renderer.domElement);
  58. stats = new Stats();
  59. stats.domElement.style.position = 'absolute';
  60. stats.domElement.style.top = '0px';
  61. container.appendChild( stats.domElement );
  62. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  63. //
  64. window.addEventListener( 'resize', onWindowResize, false );
  65. }
  66. function onWindowResize() {
  67. camera.aspect = window.innerWidth / window.innerHeight;
  68. camera.updateProjectionMatrix();
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. }
  71. function onDocumentMouseDown( event ) {
  72. event.preventDefault();
  73. var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
  74. projector.unprojectVector( vector, camera );
  75. var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
  76. var intersects = raycaster.intersectObjects( scene.children );
  77. if ( intersects.length > 0 ) {
  78. new TWEEN.Tween( intersects[ 0 ].object.position ).to( {
  79. x: Math.random() * 800 - 400,
  80. y: Math.random() * 800 - 400,
  81. z: Math.random() * 800 - 400 }, 2000 )
  82. .easing( TWEEN.Easing.Elastic.Out).start();
  83. new TWEEN.Tween( intersects[ 0 ].object.rotation ).to( {
  84. x: Math.random() * 2 * Math.PI,
  85. y: Math.random() * 2 * Math.PI,
  86. z: Math.random() * 2 * Math.PI }, 2000 )
  87. .easing( TWEEN.Easing.Elastic.Out).start();
  88. }
  89. /*
  90. // Parse all the faces
  91. for ( var i in intersects ) {
  92. intersects[ i ].face.material[ 0 ].color.setHex( Math.random() * 0xffffff | 0x80000000 );
  93. }
  94. */
  95. }
  96. //
  97. function animate() {
  98. requestAnimationFrame( animate );
  99. render();
  100. stats.update();
  101. }
  102. var radius = 600;
  103. var theta = 0;
  104. function render() {
  105. TWEEN.update();
  106. theta += 0.1;
  107. camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
  108. camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
  109. camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
  110. camera.lookAt( scene.position );
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>