canvas_particles_random.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - 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. 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="js/RequestAnimationFrame.js"></script>
  21. <script type="text/javascript" src="js/Stats.js"></script>
  22. <script type="text/javascript">
  23. var container, stats;
  24. var camera, scene, renderer, particle;
  25. var mouseX = 0, mouseY = 0;
  26. var windowHalfX = window.innerWidth / 2;
  27. var windowHalfY = window.innerHeight / 2;
  28. init();
  29. animate();
  30. function init() {
  31. container = document.createElement( 'div' );
  32. document.body.appendChild( container );
  33. camera = new THREE.Camera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
  34. camera.position.z = 1000;
  35. scene = new THREE.Scene();
  36. var PI2 = Math.PI * 2;
  37. var program = function ( context, color ) {
  38. context.fillStyle = color.__styleString;
  39. context.beginPath();
  40. context.arc( 0, 0, 1, 0, PI2, true );
  41. context.closePath();
  42. context.fill();
  43. }
  44. for ( var i = 0; i < 1000; i++ ) {
  45. particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  46. particle.position.x = Math.random() * 2000 - 1000;
  47. particle.position.y = Math.random() * 2000 - 1000;
  48. particle.position.z = Math.random() * 2000 - 1000;
  49. particle.scale.x = particle.scale.y = Math.random() * 10 + 5;
  50. scene.addObject( particle );
  51. }
  52. renderer = new THREE.CanvasRenderer();
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. container.appendChild( renderer.domElement );
  55. stats = new Stats();
  56. stats.domElement.style.position = 'absolute';
  57. stats.domElement.style.top = '0px';
  58. container.appendChild( stats.domElement );
  59. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  60. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  61. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  62. }
  63. //
  64. function onDocumentMouseMove( event ) {
  65. mouseX = event.clientX - windowHalfX;
  66. mouseY = event.clientY - windowHalfY;
  67. }
  68. function onDocumentTouchStart( event ) {
  69. if ( event.touches.length == 1 ) {
  70. event.preventDefault();
  71. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  72. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  73. }
  74. }
  75. function onDocumentTouchMove( 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. //
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. render();
  86. stats.update();
  87. }
  88. function render() {
  89. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  90. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  91. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>