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