particles_waves.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - particles - waves</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  7. <style type="text/css">
  8. body {
  9. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. a {
  14. color:#0078ff;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <script type="text/javascript" src="../build/Three.js"></script>
  20. <script type="text/javascript" src="js/Stats.js"></script>
  21. <script type="text/javascript">
  22. var SEPARATION = 100, AMOUNTX = 50, AMOUNTY = 50;
  23. var container, stats;
  24. var camera, scene, renderer;
  25. var particles, particle, count;
  26. var mouseX = 0, mouseY = 0;
  27. var windowHalfX = window.innerWidth / 2;
  28. var windowHalfY = window.innerHeight / 2;
  29. init();
  30. setInterval(loop, 1000 / 60);
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.Camera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  35. camera.position.z = 1000;
  36. scene = new THREE.Scene();
  37. renderer = new THREE.CanvasRenderer();
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. particles = new Array();
  40. var i = 0;
  41. var material = new THREE.ParticleCircleMaterial( { color: 0xffffff, opacity: 1 } );
  42. for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
  43. for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
  44. particle = particles[ i ++ ] = new THREE.Particle( material );
  45. particle.position.x = ix * SEPARATION - ( ( AMOUNTX * SEPARATION ) / 2 );
  46. particle.position.z = iy * SEPARATION - ( ( AMOUNTY * SEPARATION ) / 2 );
  47. scene.addObject( particle );
  48. }
  49. }
  50. count = 0;
  51. container.appendChild( renderer.domElement );
  52. stats = new Stats();
  53. stats.domElement.style.position = 'absolute';
  54. stats.domElement.style.top = '0px';
  55. container.appendChild( stats.domElement );
  56. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  57. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  58. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  59. }
  60. //
  61. function onDocumentMouseMove( event ) {
  62. mouseX = event.clientX - windowHalfX;
  63. mouseY = event.clientY - windowHalfY;
  64. }
  65. function onDocumentTouchStart( event ) {
  66. if ( event.touches.length == 1 ) {
  67. event.preventDefault();
  68. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  69. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  70. }
  71. }
  72. function onDocumentTouchMove( event ) {
  73. if ( event.touches.length == 1 ) {
  74. event.preventDefault();
  75. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  76. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  77. }
  78. }
  79. //
  80. function loop() {
  81. camera.position.x += ( mouseX - camera.position.x ) * .05;
  82. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  83. var i = 0;
  84. for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
  85. for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
  86. particle = particles[ i++ ];
  87. particle.position.y = ( Math.sin( ( ix + count ) * 0.3 ) * 50 ) + ( Math.sin( ( iy + count ) * 0.5 ) * 50 );
  88. particle.scale.x = particle.scale.y = ( Math.sin( ( ix + count ) * 0.3 ) + 1 ) * 2 + ( Math.sin( ( iy + count ) * 0.5 ) + 1 ) * 2;
  89. }
  90. }
  91. renderer.render( scene, camera );
  92. stats.update();
  93. count += 0.1;
  94. }
  95. </script>
  96. </body>
  97. </html>