canvas_interactive_particles.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - interactive particles</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/RequestAnimationFrame.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, scene, projector, renderer;
  23. var PI2 = Math.PI * 2;
  24. var programFill = function ( context ) {
  25. context.beginPath();
  26. context.arc( 0, 0, 1, 0, PI2, true );
  27. context.closePath();
  28. context.fill();
  29. }
  30. var programStroke = function ( context ) {
  31. context.lineWidth = 0.05;
  32. context.beginPath();
  33. context.arc( 0, 0, 1, 0, PI2, true );
  34. context.closePath();
  35. context.stroke();
  36. }
  37. var mouse = { x: 0, y: 0 }, INTERSECTED;
  38. init();
  39. animate();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. var info = document.createElement( 'div' );
  44. info.style.position = 'absolute';
  45. info.style.top = '10px';
  46. info.style.width = '100%';
  47. info.style.textAlign = 'center';
  48. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> canvas - interactive particles';
  49. container.appendChild( info );
  50. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  51. camera.position.y = 300;
  52. camera.position.z = 500;
  53. scene = new THREE.Scene();
  54. for ( var i = 0; i < 100; i ++ ) {
  55. var particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808080 + 0x808080, program: programStroke } ) );
  56. particle.position.x = Math.random() * 800 - 400;
  57. particle.position.y = Math.random() * 800 - 400;
  58. particle.position.z = Math.random() * 800 - 400;
  59. particle.scale.x = particle.scale.y = Math.random() * 10 + 10;
  60. scene.add( particle );
  61. }
  62. projector = new THREE.Projector();
  63. renderer = new THREE.CanvasRenderer();
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. container.appendChild( renderer.domElement );
  66. stats = new Stats();
  67. stats.domElement.style.position = 'absolute';
  68. stats.domElement.style.top = '0px';
  69. container.appendChild( stats.domElement );
  70. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  71. }
  72. function onDocumentMouseMove( event ) {
  73. event.preventDefault();
  74. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  75. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  76. }
  77. //
  78. function animate() {
  79. requestAnimationFrame( animate );
  80. render();
  81. stats.update();
  82. }
  83. var radius = 600;
  84. var theta = 0;
  85. function render() {
  86. // rotate camera
  87. theta += 0.2;
  88. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  89. camera.position.y = radius * Math.sin( theta * Math.PI / 360 );
  90. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  91. camera.lookAt( scene.position );
  92. // find intersections
  93. camera.updateMatrixWorld();
  94. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  95. projector.unprojectVector( vector, camera );
  96. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  97. var intersects = ray.intersectScene( scene );
  98. if ( intersects.length > 0 ) {
  99. if ( INTERSECTED != intersects[ 0 ].object ) {
  100. if ( INTERSECTED ) INTERSECTED.materials[ 0 ].program = programStroke;
  101. INTERSECTED = intersects[ 0 ].object;
  102. INTERSECTED.materials[ 0 ].program = programFill;
  103. }
  104. } else {
  105. if ( INTERSECTED ) INTERSECTED.materials[ 0 ].program = programStroke;
  106. INTERSECTED = null;
  107. }
  108. renderer.render( scene, camera );
  109. }
  110. </script>
  111. </body>
  112. </html>