Wiceng404

html

Oct 4th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. <style type='text/css'>
  2. body {
  3. background-color: #333;
  4. margin: 0;
  5. padding: 0;
  6. }
  7. canvas {
  8. background-color: #000;
  9. display: block;
  10. margin: 50px auto 0;
  11. }
  12.  
  13. p {
  14. color: #666;
  15. font-family: Futura, Arial, sans-serif;
  16. margin-top: 10px;
  17. text-align: center;
  18. }
  19. </style>
  20. <canvas width="500px" height="500px"></canvas>
  21.  
  22. <p>move your mouse | click and hold</p>
  23. <script type='text/javascript'>//<![CDATA[
  24. (function( $ ) {
  25.  
  26. // Setup canvas
  27. var animate,
  28. refreshRate = 50, // How fast (smooth) it animates
  29. click = false,
  30. clickIncrement = 0.5,
  31. clickDelta = 0,
  32. hueDelta = 0,
  33. $win = $( window ),
  34. $canvas = $( 'canvas' ),
  35. canvas = $canvas[0],
  36. ctx = canvas.getContext( '2d' ),
  37. points = [],
  38. interval = 10,
  39. canvasWidth = $canvas.width(),
  40. canvasHeight = $canvas.height(),
  41. mouseX = 200,
  42. mouseY = 200,
  43. // Our workhorse draw function
  44. drawPoint = function( x, y, radius, color ) {
  45. ctx.fillStyle = color;
  46. ctx.beginPath();
  47. ctx.arc( x, y, radius, 0, Math.PI * 2, false );
  48. ctx.closePath();
  49. ctx.fill();
  50. },
  51. render = function( points ) {
  52. var hue;
  53.  
  54. // Each time this renders, we clear the previous content
  55. ctx.clearRect( 0, 0, canvasWidth, canvasHeight );
  56.  
  57. // If the mousebutton is currently clicked, keep growing the clickDelta
  58. // modifier (which makes the points bigger) by clickIncrement
  59. if ( click ) {
  60. // But only to a certain point
  61. if ( clickDelta < 5 ) {
  62. clickDelta += clickIncrement;
  63. }
  64. } else {
  65. // And don't let it fall below zero
  66. if ( clickDelta > 0 ) {
  67. clickDelta -= clickIncrement;
  68. }
  69. }
  70.  
  71. // Strobe colors for maximum chillaxment
  72. if ( click === false ) {
  73. // If the mousebutton isn't clicked, strobe inward
  74. hueDelta += 10;
  75. } else {
  76. // If it is, strobe outward
  77. hueDelta -= 10;
  78. }
  79.  
  80. // Reset the hue once it reaches the end of the color wheel
  81. if ( hueDelta > 360 ) {
  82. hueDelta = 0;
  83. } else if ( hueDelta < 0 ) {
  84. // Or the beginning
  85. hueDelta = 360;
  86. }
  87.  
  88. // Iterate through all the points in our array
  89. for ( var i = 0, iLength = points.length; i < iLength; i += 1 ) {
  90. var x = points[i][0],
  91. y = points[i][1],
  92. x2,
  93. y2,
  94. radius = 1,
  95. radiusDelta = 0,
  96. // The distance formula:
  97. // http://www.mathwarehouse.com/algebra/distance_formula/index.php
  98. distance = Math.sqrt( Math.pow( mouseX - x, 2 ) + Math.pow( mouseY - y, 2 ) ),
  99. step = ( canvasWidth ) / 3;
  100.  
  101. radiusDelta = ( distance / step );
  102.  
  103. // Set the hue of points by their distance to the mouse
  104. if ( mouseX && mouseY ) {
  105. // Add randomization to the edge to make it more ragged / textured
  106. distance += ( Math.random() * 50 ) - 25;
  107. hue = ( distance > 360 ) ? distance % 360 : distance;
  108. hue = ( hue + hueDelta ) % 360;
  109. } else {
  110. hue = 360;
  111. }
  112.  
  113. // Set the size of the point
  114. radius = radius + radiusDelta + clickDelta + points[i][2];
  115. radius = ( radius < 1 ) ? 1 : radius;
  116.  
  117. // Randomize the opacity of each point to create attractive variance
  118. points[i][4] += ( Math.random() * 0.1 ) - 0.05;
  119.  
  120. // Ensure opacity is between 1 and 0
  121. if ( points[i][4] > 1 ) {
  122. points[i][4] = 1;
  123. } else if ( points[i][4] < 0 ) {
  124. points[i][4] = 0;
  125. }
  126.  
  127. // We're done calculating -- draw the point!
  128. drawPoint( x, y, radius, 'hsla(' + hue + ', 50%, 50%, ' + points[i][4] + ')' );
  129. }
  130. };
  131.  
  132. // Set up an array of points to iterate over with each rendering
  133. for ( var i = interval; i < canvasHeight; i += interval ) {
  134.  
  135. for ( var j = interval; j < canvasWidth; j += interval ) {
  136. var rand = ( Math.random() * 2 ) - 1,
  137. randDelta = Math.floor( Math.random() * 2 ),
  138. opacity = Math.random();
  139.  
  140. // This is an atavism - disregard
  141. if ( randDelta !== 1 ) {
  142. randDelta = -1;
  143. }
  144.  
  145. points.push( [ j, i, rand, randDelta, opacity ] );
  146. }
  147. }
  148.  
  149. // Set the mouseX and mouseY variables on mouse move so that
  150. // the visualization responds to mouse movement
  151. $win.bind( 'mousemove', function( event ) {
  152. mouseX = event.pageX - $canvas.offset().left;
  153. mouseY = event.pageY - $canvas.offset().top;
  154. });
  155.  
  156. $win.bind( 'mousedown', function( event ) {
  157. click = true;
  158. });
  159.  
  160. $win.bind( 'mouseup', function( event ) {
  161. click = false;
  162. });
  163.  
  164. // Animation happens by re-rendering everything every refreshRate milliseconds
  165. animate = setInterval( (function() { render( points ); }), refreshRate );
  166.  
  167. })( jQuery );
  168. //]]>
  169. </script>
Advertisement
Add Comment
Please, Sign In to add comment