canvas_particles_floor.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/RequestAnimationFrame.js"></script>
  21. <script src="js/Stats.js"></script>
  22. <script>
  23. var SEPARATION = 100;
  24. var AMOUNTX = 50;
  25. var AMOUNTY = 50;
  26. var container, stats;
  27. var camera, scene, renderer, particle;
  28. var mouseX = 0, mouseY = 0;
  29. var windowHalfX = window.innerWidth / 2;
  30. var windowHalfY = window.innerHeight / 2;
  31. init();
  32. animate();
  33. function init() {
  34. container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. camera = new THREE.Camera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.z = 1000;
  38. camera.useTarget = true;
  39. scene = new THREE.Scene();
  40. var PI2 = Math.PI * 2;
  41. var material = new THREE.ParticleCanvasMaterial( {
  42. color: 0xffffff,
  43. program: function ( context ) {
  44. context.beginPath();
  45. context.arc( 0, 0, 1, 0, PI2, true );
  46. context.closePath();
  47. context.fill();
  48. }
  49. } );
  50. for ( var ix = 0; ix < AMOUNTX; ix++ ) {
  51. for ( var iy = 0; iy < AMOUNTY; iy++ ) {
  52. particle = new THREE.Particle( material );
  53. particle.position.x = ix * SEPARATION - ( ( AMOUNTX * SEPARATION ) / 2 );
  54. particle.position.z = iy * SEPARATION - ( ( AMOUNTY * SEPARATION ) / 2 );
  55. scene.add( particle );
  56. }
  57. }
  58. renderer = new THREE.CanvasRenderer();
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. container.appendChild( renderer.domElement );
  61. stats = new Stats();
  62. stats.domElement.style.position = 'absolute';
  63. stats.domElement.style.top = '0px';
  64. container.appendChild( stats.domElement );
  65. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  66. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  67. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  68. }
  69. //
  70. function onDocumentMouseMove(event) {
  71. mouseX = event.clientX - windowHalfX;
  72. mouseY = event.clientY - windowHalfY;
  73. }
  74. function onDocumentTouchStart( event ) {
  75. if ( event.touches.length > 1 ) {
  76. event.preventDefault();
  77. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  78. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  79. }
  80. }
  81. function onDocumentTouchMove( event ) {
  82. if ( event.touches.length == 1 ) {
  83. event.preventDefault();
  84. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  85. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  86. }
  87. }
  88. //
  89. function animate() {
  90. requestAnimationFrame( animate );
  91. render();
  92. stats.update();
  93. }
  94. function render() {
  95. camera.position.x += ( mouseX - camera.position.x ) * .05;
  96. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  97. renderer.render( scene, camera );
  98. }
  99. </script>
  100. </body>
  101. </html>