particles_waves.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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="http://github.com/mrdoob/stats.js/raw/master/build/stats.js"></script>
  21. <script type="text/javascript">
  22. var SCREEN_WIDTH = window.innerWidth;
  23. var SCREEN_HEIGHT = window.innerHeight;
  24. var SEPARATION = 100;
  25. var AMOUNTX = 50;
  26. var AMOUNTY = 50;
  27. var stats;
  28. var container;
  29. var particles, particle;
  30. var count;
  31. var camera;
  32. var scene;
  33. var renderer;
  34. var mouseX = 0;
  35. var mouseY = 0;
  36. var windowHalfX = window.innerWidth / 2;
  37. var windowHalfY = window.innerHeight / 2;
  38. init();
  39. setInterval(loop, 1000 / 60);
  40. function init() {
  41. container = document.createElement('div');
  42. document.body.appendChild(container);
  43. camera = new THREE.Camera(0, 0, 1000);
  44. camera.focus = 200;
  45. scene = new THREE.Scene();
  46. renderer = new THREE.CanvasRenderer();
  47. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  48. particles = new Array();
  49. var i = 0;
  50. var material = new THREE.ColorFillMaterial(0xffffff, 1);
  51. for (var ix = 0; ix < AMOUNTX; ix++) {
  52. for(var iy = 0; iy < AMOUNTY; iy++) {
  53. particle = particles[i++] = new THREE.Particle( material );
  54. particle.size = 1;
  55. particle.position.x = ix * SEPARATION - ((AMOUNTX * SEPARATION) / 2);
  56. particle.position.z = iy * SEPARATION - ((AMOUNTY * SEPARATION) / 2);
  57. scene.add( particle );
  58. }
  59. }
  60. count = 0;
  61. container.appendChild(renderer.domElement);
  62. stats = new Stats();
  63. stats.domElement.style.position = 'absolute';
  64. stats.domElement.style.top = '0px';
  65. container.appendChild(stats.domElement);
  66. document.addEventListener('mousemove', onDocumentMouseMove, false);
  67. document.addEventListener('touchstart', onDocumentTouchStart, false);
  68. document.addEventListener('touchmove', onDocumentTouchMove, false);
  69. }
  70. //
  71. function onDocumentMouseMove(event) {
  72. mouseX = event.clientX - windowHalfX;
  73. mouseY = event.clientY - windowHalfY;
  74. }
  75. function onDocumentTouchStart(event) {
  76. if(event.touches.length == 1) {
  77. event.preventDefault();
  78. mouseX = event.touches[0].pageX - windowHalfX;
  79. mouseY = event.touches[0].pageY - windowHalfY;
  80. }
  81. }
  82. function onDocumentTouchMove(event) {
  83. if(event.touches.length == 1) {
  84. event.preventDefault();
  85. mouseX = event.touches[0].pageX - windowHalfX;
  86. mouseY = event.touches[0].pageY - windowHalfY;
  87. }
  88. }
  89. //
  90. function loop() {
  91. camera.position.x += (mouseX - camera.position.x) * .05;
  92. camera.position.y += (-mouseY - camera.position.y) * .05;
  93. var i = 0;
  94. for (var ix = 0; ix < AMOUNTX; ix++) {
  95. for(var iy = 0; iy < AMOUNTY; iy++) {
  96. particle = particles[i++];
  97. particle.size = (Math.sin((ix + count) * .3) + 1) * 2 + (Math.sin((iy + count) * .5) + 1) * 2;
  98. particle.position.y = (Math.sin((ix + count) * .3) * 50) + (Math.sin((iy + count) * .5) * 50);
  99. }
  100. }
  101. renderer.render(scene, camera);
  102. stats.update();
  103. count += 0.1;
  104. }
  105. </script>
  106. </body>
  107. </html>