Advertisement
Guest User

Untitled

a guest
May 24th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. body {
  8. margin: 0;
  9. }
  10.  
  11. canvas {
  12. width: 99%;
  13. }
  14.  
  15. .canvas {
  16. position: fixed;
  17. width: 100%;
  18. height: 100%;
  19. top: 0;
  20. right: 0;
  21. bottom: 0;
  22. left: 0;
  23. z-index: -1;
  24. }
  25.  
  26. form {
  27. width: 260px;
  28. background-color: rgba(255, 255, 255, 0.9);
  29. z-index: 1;
  30. }
  31.  
  32. fieldset {
  33. padding: 1mm;
  34. }
  35.  
  36. label {
  37. display: block;
  38. margin: 1mm;
  39. }
  40.  
  41. input {
  42. display: block;
  43. float: right;
  44. margin-left: 1mm;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <form>
  50. <label>Time speed<input id="speed" value="0.1" /></label>
  51. <fieldset>
  52. <legend>Spiral</legend>
  53. <label>Segments<input id="segments" value="300" /></label>
  54. <label>Speed<input id="spiral_speed" value="0.001" /></label>
  55. <label>Gravity<input id="gravity" value="0.01" /></label>
  56. </fieldset>
  57. </form>
  58. <canvas id="canvas"></canvas>
  59. <script>
  60. var width;
  61. var height;
  62. var time = 0;
  63. var play = true;
  64. var g = canvas.getContext('2d');
  65. var mouseX = 0;
  66. var mouseY = 0;
  67. var move = false;
  68. var last = Date.now();
  69.  
  70. canvas.onmousedown = function(e) {
  71. move = true;
  72. mouseX = e.clientX;
  73. mouseY = e.clientY;
  74. };
  75.  
  76. canvas.onmouseup = function() {
  77. move = false;
  78. vx = vy = 0;
  79. };
  80.  
  81. canvas.onmousemove = function(e) {
  82. if (move) {
  83. mouseX = e.clientX;
  84. mouseY = e.clientY;
  85. }
  86. };
  87.  
  88. function resize() {
  89. canvas.setAttribute('class', 'canvas');
  90. var style = getComputedStyle(canvas);
  91. width = parseFloat(style.width);
  92. height = parseFloat(style.height);
  93. canvas.width = width;
  94. canvas.height = height;
  95. var g = canvas.getContext('2d');
  96. //g.translate(width/2, height/2);
  97. //g.scale(0.2, 0.2);
  98. play = true;
  99. centerX = width/2;
  100. centerY = height/2;
  101. if (play)
  102. requestAnimationFrame(draw);
  103. }
  104.  
  105. var centerX;
  106. var centerY;
  107. var vx = 0;
  108. var vy = 0;
  109.  
  110. function spiral() {
  111. var segments_number = parseFloat(segments.value);
  112. var speed = parseFloat(spiral_speed.value);
  113. for(var i = 0; i < segments_number; i++) {
  114. var p = [i, i];
  115. p[1] *= time * speed;
  116. g.lineTo(centerX + p[0] * Math.sin(p[1]), centerY - p[0] * Math.cos(p[1]));
  117. }
  118.  
  119. if (move) {
  120. var gravity_value = parseFloat(gravity.value);
  121. vx += (mouseX - centerX) * gravity_value;
  122. vy += (mouseY - centerY) * gravity_value;
  123. centerX += vx;
  124. centerY += vy;
  125. }
  126. }
  127.  
  128. function draw() {
  129. var now = Date.now();
  130. time += (now - last) * speed.value;
  131. last = now;
  132. g.imageSmoothingEnabled= false;
  133. g.clearRect(0, 0, width, height);
  134. g.beginPath();
  135. spiral();
  136. g.stroke();
  137. if (play)
  138. requestAnimationFrame(draw);
  139. }
  140.  
  141. onresize = resize;
  142.  
  143. onload = function() {
  144. resize();
  145. draw();
  146. //setInterval(draw, 20);
  147. };
  148.  
  149. onkeypress = function(e) {
  150. if (32 == e.keyCode || 32 == e.charCode) {
  151. play = !play;
  152. if (play)
  153. requestAnimationFrame(draw);
  154. return false;
  155. }
  156. }
  157. </script>
  158. </body>
  159. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement