canvas_interactive_particles.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.min.js"></script>
  18. <script src="js/libs/stats.min.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://threejs.org" 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. for ( var i = 0; i < 100; i ++ ) {
  53. var particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808080 + 0x808080, program: programStroke } ) );
  54. particle.position.x = Math.random() * 800 - 400;
  55. particle.position.y = Math.random() * 800 - 400;
  56. particle.position.z = Math.random() * 800 - 400;
  57. particle.scale.x = particle.scale.y = Math.random() * 10 + 10;
  58. scene.add( particle );
  59. }
  60. projector = new THREE.Projector();
  61. renderer = new THREE.CanvasRenderer();
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. container.appendChild( renderer.domElement );
  64. stats = new Stats();
  65. stats.domElement.style.position = 'absolute';
  66. stats.domElement.style.top = '0px';
  67. container.appendChild( stats.domElement );
  68. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  69. //
  70. window.addEventListener( 'resize', onWindowResize, false );
  71. }
  72. function onWindowResize() {
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. }
  77. function onDocumentMouseMove( event ) {
  78. event.preventDefault();
  79. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  80. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  81. }
  82. //
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. render();
  86. stats.update();
  87. }
  88. var radius = 600;
  89. var theta = 0;
  90. function render() {
  91. // rotate camera
  92. theta += 0.2;
  93. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  94. camera.position.y = radius * Math.sin( theta * Math.PI / 360 );
  95. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  96. camera.lookAt( scene.position );
  97. // find intersections
  98. camera.updateMatrixWorld();
  99. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  100. projector.unprojectVector( vector, camera );
  101. var ray = new THREE.Raycaster( camera.position, vector.subSelf( camera.position ).normalize() );
  102. var intersects = ray.intersectObjects( scene.children );
  103. if ( intersects.length > 0 ) {
  104. if ( INTERSECTED != intersects[ 0 ].object ) {
  105. if ( INTERSECTED ) INTERSECTED.material.program = programStroke;
  106. INTERSECTED = intersects[ 0 ].object;
  107. INTERSECTED.material.program = programFill;
  108. }
  109. } else {
  110. if ( INTERSECTED ) INTERSECTED.material.program = programStroke;
  111. INTERSECTED = null;
  112. }
  113. renderer.render( scene, camera );
  114. }
  115. </script>
  116. </body>
  117. </html>