waves.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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="../../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 / 2;
  39. var windowHalfY = window.innerHeight / 2;
  40. init();
  41. setInterval(loop, 1000 / 60);
  42. function init() {
  43. container = document.createElement('div');
  44. document.body.appendChild(container);
  45. camera = new THREE.Camera(0, 0, 1000);
  46. camera.focus = 200;
  47. scene = new THREE.Scene();
  48. renderer = new THREE.CanvasRenderer();
  49. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  50. particles = new Array();
  51. var i = 0;
  52. var material = new THREE.ColorFillMaterial(0xffffff, 1);
  53. for (var ix = 0; ix < AMOUNTX; ix++) {
  54. for(var iy = 0; iy < AMOUNTY; iy++) {
  55. particle = particles[i++] = new THREE.Particle( material );
  56. particle.size = 1;
  57. particle.position.x = ix * SEPARATION - ((AMOUNTX * SEPARATION) / 2);
  58. particle.position.z = iy * SEPARATION - ((AMOUNTY * SEPARATION) / 2);
  59. scene.add( particle );
  60. }
  61. }
  62. count = 0;
  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. document.addEventListener('touchstart', onDocumentTouchStart, false);
  70. document.addEventListener('touchmove', onDocumentTouchMove, false);
  71. }
  72. //
  73. function onDocumentMouseMove(event) {
  74. mouseX = event.clientX - windowHalfX;
  75. mouseY = event.clientY - windowHalfY;
  76. }
  77. function onDocumentTouchStart(event) {
  78. if(event.touches.length == 1) {
  79. event.preventDefault();
  80. mouseX = event.touches[0].pageX - windowHalfX;
  81. mouseY = event.touches[0].pageY - windowHalfY;
  82. }
  83. }
  84. function onDocumentTouchMove(event) {
  85. if(event.touches.length == 1) {
  86. event.preventDefault();
  87. mouseX = event.touches[0].pageX - windowHalfX;
  88. mouseY = event.touches[0].pageY - windowHalfY;
  89. }
  90. }
  91. //
  92. function loop() {
  93. camera.position.x += (mouseX - camera.position.x) * .05;
  94. camera.position.y += (-mouseY - camera.position.y) * .05;
  95. var i = 0;
  96. for (var ix = 0; ix < AMOUNTX; ix++) {
  97. for(var iy = 0; iy < AMOUNTY; iy++) {
  98. particle = particles[i++];
  99. particle.size = (Math.sin((ix + count) * .3) + 1) * 2 + (Math.sin((iy + count) * .5) + 1) * 2;
  100. particle.position.y = (Math.sin((ix + count) * .3) * 50) + (Math.sin((iy + count) * .5) * 50);
  101. }
  102. }
  103. renderer.render(scene, camera);
  104. stats.update();
  105. count += 0.1;
  106. }
  107. </script>
  108. </body>
  109. </html>