random.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - particles - random</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 stats;
  27. var container;
  28. var particle;
  29. var camera;
  30. var scene;
  31. var renderer;
  32. var mouseX = 0;
  33. var mouseY = 0;
  34. var windowHalfX = window.innerWidth >> 1;
  35. var windowHalfY = window.innerHeight >> 1;
  36. document.addEventListener('mousemove', onDocumentMouseMove, false);
  37. document.addEventListener('touchstart', onDocumentTouchStart, false);
  38. init();
  39. setInterval(loop, 1000 / 60);
  40. function init()
  41. {
  42. container = document.createElement('div');
  43. document.body.appendChild(container);
  44. camera = new Camera(0, 0, 1000);
  45. camera.focus = 200;
  46. scene = new Scene();
  47. renderer = new CanvasRenderer();
  48. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  49. for (var i = 0; i < 1000; i++)
  50. {
  51. particle = new Particle( new ColorMaterial(Math.random() * 0x808008 + 0x808080, 1) );
  52. particle.size = Math.random() * 10 + 5;
  53. particle.position.x = Math.random() * 2000 - 1000;
  54. particle.position.y = Math.random() * 2000 - 1000;
  55. particle.position.z = Math.random() * 2000 - 1000;
  56. particle.updateMatrix();
  57. scene.add( particle );
  58. }
  59. container.appendChild(renderer.viewport);
  60. stats = new Stats();
  61. container.appendChild( stats.getDisplayElement() );
  62. }
  63. //
  64. function onDocumentMouseMove(event)
  65. {
  66. mouseX = event.clientX - windowHalfX;
  67. mouseY = event.clientY - windowHalfY;
  68. }
  69. function onDocumentTouchStart( event )
  70. {
  71. if(event.touches.length == 1)
  72. {
  73. event.preventDefault();
  74. mouseX = event.touches[0].pageX - windowHalfX;
  75. mouseY = event.touches[0].pageY - windowHalfY;
  76. document.addEventListener('touchmove', onDocumentTouchMove, false);
  77. document.addEventListener('touchend', onDocumentTouchEnd, false);
  78. }
  79. }
  80. function onDocumentTouchMove( 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 onDocumentTouchEnd( event )
  90. {
  91. if(event.touches.length == 0)
  92. {
  93. event.preventDefault();
  94. mouseX = event.touches[0].pageX - windowHalfX;
  95. mouseY = event.touches[0].pageY - windowHalfY;
  96. document.removeEventListener('touchmove', onDocumentTouchMove, false);
  97. document.removeEventListener('touchend', onDocumentTouchEnd, false);
  98. }
  99. }
  100. //
  101. function loop()
  102. {
  103. camera.x += (mouseX - camera.x) * .05;
  104. camera.y += (-mouseY - camera.y) * .05;
  105. camera.updateMatrix();
  106. renderer.render(scene, camera);
  107. stats.update();
  108. }
  109. </script>
  110. </body>
  111. </html>