canvas_particles_waves.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - particles - waves</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, AMOUNTX = 50, AMOUNTY = 50;
  23. var container, stats;
  24. var camera, scene, renderer;
  25. var particles, particle, count = 0;
  26. var mouseX = 0, mouseY = 0;
  27. var windowHalfX = window.innerWidth / 2;
  28. var windowHalfY = window.innerHeight / 2;
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  35. camera.position.z = 1000;
  36. scene = new THREE.Scene();
  37. particles = new Array();
  38. var PI2 = Math.PI * 2;
  39. var material = new THREE.SpriteCanvasMaterial( {
  40. color: 0xffffff,
  41. program: function ( context ) {
  42. context.beginPath();
  43. context.arc( 0, 0, 0.5, 0, PI2, true );
  44. context.fill();
  45. }
  46. } );
  47. var i = 0;
  48. for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
  49. for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
  50. particle = particles[ i ++ ] = new THREE.Sprite( material );
  51. particle.position.x = ix * SEPARATION - ( ( AMOUNTX * SEPARATION ) / 2 );
  52. particle.position.z = iy * SEPARATION - ( ( AMOUNTY * SEPARATION ) / 2 );
  53. scene.add( particle );
  54. }
  55. }
  56. renderer = new THREE.CanvasRenderer();
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. container.appendChild( renderer.domElement );
  59. stats = new Stats();
  60. stats.domElement.style.position = 'absolute';
  61. stats.domElement.style.top = '0px';
  62. container.appendChild( stats.domElement );
  63. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  64. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  65. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  66. //
  67. window.addEventListener( 'resize', onWindowResize, false );
  68. }
  69. function onWindowResize() {
  70. windowHalfX = window.innerWidth / 2;
  71. windowHalfY = window.innerHeight / 2;
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. //
  77. function onDocumentMouseMove( event ) {
  78. mouseX = event.clientX - windowHalfX;
  79. mouseY = event.clientY - windowHalfY;
  80. }
  81. function onDocumentTouchStart( 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. function onDocumentTouchMove( event ) {
  89. if ( event.touches.length === 1 ) {
  90. event.preventDefault();
  91. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  92. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  93. }
  94. }
  95. //
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. render();
  99. stats.update();
  100. }
  101. function render() {
  102. camera.position.x += ( mouseX - camera.position.x ) * .05;
  103. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  104. camera.lookAt( scene.position );
  105. var i = 0;
  106. for ( var ix = 0; ix < AMOUNTX; ix ++ ) {
  107. for ( var iy = 0; iy < AMOUNTY; iy ++ ) {
  108. particle = particles[ i++ ];
  109. particle.position.y = ( Math.sin( ( ix + count ) * 0.3 ) * 50 ) +
  110. ( Math.sin( ( iy + count ) * 0.5 ) * 50 );
  111. particle.scale.x = particle.scale.y = ( Math.sin( ( ix + count ) * 0.3 ) + 1 ) * 4 +
  112. ( Math.sin( ( iy + count ) * 0.5 ) + 1 ) * 4;
  113. }
  114. }
  115. renderer.render( scene, camera );
  116. count += 0.1;
  117. }
  118. </script>
  119. </body>
  120. </html>