waves.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. {
  10. background-color: #000000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. a
  15. {
  16. color:#0078ff;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <script type="text/javascript" src="http://github.com/mrdoob/three.js/raw/master/build/three.js"></script>
  22. <script type="text/javascript" src="http://github.com/mrdoob/stats.js/raw/master/build/stats.js"></script>
  23. <script type="text/javascript">
  24. var SCREEN_WIDTH = window.innerWidth;
  25. var SCREEN_HEIGHT = window.innerHeight;
  26. var SEPARATION = 100;
  27. var AMOUNTX = 50;
  28. var AMOUNTY = 50;
  29. var stats;
  30. var container;
  31. var particles, particle;
  32. var count;
  33. var camera;
  34. var scene;
  35. var renderer;
  36. var mouseX = 0;
  37. var mouseY = 0;
  38. var windowHalfX = window.innerWidth >> 1;
  39. var windowHalfY = window.innerHeight >> 1;
  40. document.addEventListener('mousemove', onDocumentMouseMove, false);
  41. document.addEventListener('touchstart', onDocumentTouchStart, false);
  42. init();
  43. setInterval(loop, 1000 / 60);
  44. function init()
  45. {
  46. container = document.createElement('div');
  47. document.body.appendChild(container);
  48. camera = new Camera(0, 0, 1000);
  49. camera.focus = 200;
  50. scene = new Scene();
  51. renderer = new CanvasRenderer();
  52. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  53. particles = new Array();
  54. var i = 0;
  55. var material = new ColorMaterial(0xffffff, 1);
  56. for (var ix = 0; ix < AMOUNTX; ix++)
  57. {
  58. for(var iy = 0; iy < AMOUNTY; iy++)
  59. {
  60. particle = particles[i++] = new Particle( material );
  61. particle.size = 1;
  62. particle.position.x = ix * SEPARATION - ((AMOUNTX * SEPARATION) / 2);
  63. particle.position.z = iy * SEPARATION - ((AMOUNTY * SEPARATION) / 2);
  64. particle.updateMatrix();
  65. scene.add( particle );
  66. }
  67. }
  68. count = 0;
  69. container.appendChild(renderer.viewport);
  70. stats = new Stats();
  71. container.appendChild( stats.getDisplayElement() );
  72. }
  73. //
  74. function onDocumentMouseMove(event)
  75. {
  76. mouseX = event.clientX - windowHalfX;
  77. mouseY = event.clientY - windowHalfY;
  78. }
  79. function onDocumentTouchStart( event )
  80. {
  81. if(event.touches.length == 1)
  82. {
  83. event.preventDefault();
  84. mouseX = event.touches[0].pageX - windowHalfX;
  85. mouseY = event.touches[0].pageY - windowHalfY;
  86. document.addEventListener('touchmove', onDocumentTouchMove, false);
  87. document.addEventListener('touchend', onDocumentTouchEnd, false);
  88. }
  89. }
  90. function onDocumentTouchMove( event )
  91. {
  92. if(event.touches.length == 1)
  93. {
  94. event.preventDefault();
  95. mouseX = event.touches[0].pageX - windowHalfX;
  96. mouseY = event.touches[0].pageY - windowHalfY;
  97. }
  98. }
  99. function onDocumentTouchEnd( event )
  100. {
  101. if(event.touches.length == 0)
  102. {
  103. event.preventDefault();
  104. mouseX = event.touches[0].pageX - windowHalfX;
  105. mouseY = event.touches[0].pageY - windowHalfY;
  106. document.removeEventListener('touchmove', onDocumentTouchMove, false);
  107. document.removeEventListener('touchend', onDocumentTouchEnd, false);
  108. }
  109. }
  110. //
  111. function loop()
  112. {
  113. camera.x += (mouseX - camera.x) * .05;
  114. camera.y += (-mouseY - camera.y) * .05;
  115. camera.updateMatrix();
  116. var i = 0;
  117. for (var ix = 0; ix < AMOUNTX; ix++)
  118. {
  119. for(var iy = 0; iy < AMOUNTY; iy++)
  120. {
  121. particle = particles[i++];
  122. particle.size = (Math.sin((ix + count) * .3) + 1) * 2 + (Math.sin((iy + count) * .5) + 1) * 2;
  123. particle.position.y = (Math.sin((ix + count) * .3) * 50) + (Math.sin((iy + count) * .5) * 50);
  124. }
  125. }
  126. renderer.render(scene, camera);
  127. stats.update();
  128. count += 0.1;
  129. }
  130. </script>
  131. </body>
  132. </html>