canvas_particles_floor.html 3.5 KB

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