canvas_particles_random.html 3.6 KB

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