canvas_particles_floor.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - particles - floor</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 SEPARATION = 100;
  23. var AMOUNTX = 50;
  24. var AMOUNTY = 50;
  25. var container, stats;
  26. var camera, scene, renderer, particle;
  27. var mouseX = 0, mouseY = 0;
  28. var windowHalfX = window.innerWidth / 2;
  29. var windowHalfY = window.innerHeight / 2;
  30. init();
  31. animate();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  36. camera.position.z = 1000;
  37. scene = new THREE.Scene();
  38. var material = new THREE.SpriteMaterial();
  39. for ( var ix = 0; ix < AMOUNTX; ix++ ) {
  40. for ( var iy = 0; iy < AMOUNTY; iy++ ) {
  41. particle = new THREE.Sprite( material );
  42. particle.scale.y = 20;
  43. particle.position.x = ix * SEPARATION - ( ( AMOUNTX * SEPARATION ) / 2 );
  44. particle.position.z = iy * SEPARATION - ( ( AMOUNTY * SEPARATION ) / 2 );
  45. scene.add( particle );
  46. }
  47. }
  48. renderer = new THREE.CanvasRenderer();
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. container.appendChild( renderer.domElement );
  51. stats = new Stats();
  52. stats.domElement.style.position = 'absolute';
  53. stats.domElement.style.top = '0px';
  54. container.appendChild( stats.domElement );
  55. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  56. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  57. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  58. //
  59. window.addEventListener( 'resize', onWindowResize, false );
  60. }
  61. function onWindowResize() {
  62. windowHalfX = window.innerWidth / 2;
  63. windowHalfY = window.innerHeight / 2;
  64. camera.aspect = window.innerWidth / window.innerHeight;
  65. camera.updateProjectionMatrix();
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. }
  68. //
  69. function onDocumentMouseMove( event ) {
  70. mouseX = event.clientX - windowHalfX;
  71. mouseY = event.clientY - windowHalfY;
  72. }
  73. function onDocumentTouchStart( event ) {
  74. if ( event.touches.length > 1 ) {
  75. event.preventDefault();
  76. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  77. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  78. }
  79. }
  80. function onDocumentTouchMove( 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. //
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. render();
  91. stats.update();
  92. }
  93. function render() {
  94. camera.position.x += ( mouseX - camera.position.x ) * .05;
  95. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  96. camera.lookAt( scene.position );
  97. renderer.render( scene, camera );
  98. }
  99. </script>
  100. </body>
  101. </html>