waves.html 3.4 KB

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