canvas_particles_random.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  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 src="../build/three.min.js"></script>
  20. <script src="js/Stats.js"></script>
  21. <script>
  22. var container, stats;
  23. var camera, scene, renderer, group, particle;
  24. var mouseX = 0, mouseY = 0;
  25. var windowHalfX = window.innerWidth / 2;
  26. var windowHalfY = window.innerHeight / 2;
  27. init();
  28. animate();
  29. function init() {
  30. container = document.createElement( 'div' );
  31. document.body.appendChild( container );
  32. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
  33. camera.position.z = 1000;
  34. scene = new THREE.Scene();
  35. var PI2 = Math.PI * 2;
  36. var program = function ( context ) {
  37. context.beginPath();
  38. context.arc( 0, 0, 1, 0, PI2, true );
  39. context.closePath();
  40. context.fill();
  41. }
  42. group = new THREE.Object3D();
  43. scene.add( group );
  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. group.add( 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. window.addEventListener( 'resize', onWindowResize, false );
  64. }
  65. function onWindowResize() {
  66. windowHalfX = window.innerWidth / 2;
  67. windowHalfY = window.innerHeight / 2;
  68. camera.aspect = window.innerWidth / window.innerHeight;
  69. camera.updateProjectionMatrix();
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  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 animate() {
  93. requestAnimationFrame( animate );
  94. render();
  95. stats.update();
  96. }
  97. function render() {
  98. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  99. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  100. camera.lookAt( scene.position );
  101. group.rotation.x += 0.01;
  102. group.rotation.y += 0.02;
  103. renderer.render( scene, camera );
  104. }
  105. </script>
  106. </body>
  107. </html>