canvas_interactive_cubes_tween.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.js"></script>
  18. <script src="js/Tween.js"></script>
  19. <script src="js/RequestAnimationFrame.js"></script>
  20. <script src="js/Stats.js"></script>
  21. <script>
  22. var container, stats;
  23. var camera, scene, projector, renderer;
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. var info = document.createElement( 'div' );
  30. info.style.position = 'absolute';
  31. info.style.top = '10px';
  32. info.style.width = '100%';
  33. info.style.textAlign = 'center';
  34. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - clickable objects';
  35. container.appendChild( info );
  36. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.y = 300;
  38. camera.position.z = 500;
  39. camera.useTarget = true;
  40. scene = new THREE.Scene();
  41. var geometry = new THREE.CubeGeometry( 100, 100, 100 );
  42. for ( var i = 0; i < 20; i ++ ) {
  43. var object = new THREE.Mesh( geometry, [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5 } ), new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, wireframe: true } ) ] );
  44. object.position.x = Math.random() * 800 - 400;
  45. object.position.y = Math.random() * 800 - 400;
  46. object.position.z = Math.random() * 800 - 400;
  47. object.scale.x = Math.random() * 2 + 1;
  48. object.scale.y = Math.random() * 2 + 1;
  49. object.scale.z = Math.random() * 2 + 1;
  50. object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
  51. object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
  52. object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
  53. scene.add( object );
  54. }
  55. projector = new THREE.Projector();
  56. renderer = new THREE.CanvasRenderer();
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. container.appendChild(renderer.domElement);
  59. stats = new Stats();
  60. stats.domElement.style.position = 'absolute';
  61. stats.domElement.style.top = '0px';
  62. container.appendChild( stats.domElement );
  63. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  64. }
  65. function onDocumentMouseDown( event ) {
  66. event.preventDefault();
  67. var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
  68. projector.unprojectVector( vector, camera );
  69. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  70. var intersects = ray.intersectScene( scene );
  71. if ( intersects.length > 0 ) {
  72. new TWEEN.Tween( intersects[ 0 ].object.position ).to( {
  73. x: Math.random() * 800 - 400,
  74. y: Math.random() * 800 - 400,
  75. z: Math.random() * 800 - 400 }, 2000 )
  76. .easing( TWEEN.Easing.Elastic.EaseOut).start();
  77. new TWEEN.Tween( intersects[ 0 ].object.rotation ).to( {
  78. x: ( Math.random() * 360 ) * Math.PI / 180,
  79. y: ( Math.random() * 360 ) * Math.PI / 180,
  80. z: ( Math.random() * 360 ) * Math.PI / 180 }, 2000 )
  81. .easing( TWEEN.Easing.Elastic.EaseOut).start();
  82. }
  83. /*
  84. // Parse all the faces
  85. for ( var i in intersects ) {
  86. intersects[ i ].face.material[ 0 ].color.setHex( Math.random() * 0xffffff | 0x80000000 );
  87. }
  88. */
  89. }
  90. //
  91. function animate() {
  92. requestAnimationFrame( animate );
  93. render();
  94. stats.update();
  95. }
  96. var radius = 600;
  97. var theta = 0;
  98. function render() {
  99. TWEEN.update();
  100. theta += 0.2;
  101. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  102. camera.position.y = radius * Math.sin( theta * Math.PI / 360 );
  103. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>