canvas_particles_random.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.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. scene.add( camera );
  36. var PI2 = Math.PI * 2;
  37. var program = function ( context ) {
  38. context.beginPath();
  39. context.arc( 0, 0, 1, 0, PI2, true );
  40. context.closePath();
  41. context.fill();
  42. }
  43. group = new THREE.Object3D();
  44. scene.add( group );
  45. for ( var i = 0; i < 1000; i++ ) {
  46. particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  47. particle.position.x = Math.random() * 2000 - 1000;
  48. particle.position.y = Math.random() * 2000 - 1000;
  49. particle.position.z = Math.random() * 2000 - 1000;
  50. particle.scale.x = particle.scale.y = Math.random() * 10 + 5;
  51. group.add( particle );
  52. }
  53. renderer = new THREE.CanvasRenderer();
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. container.appendChild( renderer.domElement );
  56. stats = new Stats();
  57. stats.domElement.style.position = 'absolute';
  58. stats.domElement.style.top = '0px';
  59. container.appendChild( stats.domElement );
  60. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  61. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  62. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  63. //
  64. window.addEventListener( 'resize', onWindowResize, false );
  65. }
  66. function onWindowResize() {
  67. windowHalfX = window.innerWidth / 2,
  68. windowHalfY = window.innerHeight / 2,
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. }
  73. //
  74. function onDocumentMouseMove( event ) {
  75. mouseX = event.clientX - windowHalfX;
  76. mouseY = event.clientY - windowHalfY;
  77. }
  78. function onDocumentTouchStart( event ) {
  79. if ( event.touches.length === 1 ) {
  80. event.preventDefault();
  81. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  82. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  83. }
  84. }
  85. function onDocumentTouchMove( event ) {
  86. if ( event.touches.length === 1 ) {
  87. event.preventDefault();
  88. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  89. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  90. }
  91. }
  92. //
  93. function animate() {
  94. requestAnimationFrame( animate );
  95. render();
  96. stats.update();
  97. }
  98. function render() {
  99. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  100. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  101. camera.lookAt( scene.position );
  102. group.rotation.x += 0.01;
  103. group.rotation.y += 0.02;
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>