Advertisement
BrU32

JS Fade Spiral SRC

Dec 20th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1.  
  2. <canvas id="canvas"></canvas>
  3.  
  4. <script>
  5. var canvas = document.getElementById('canvas'),
  6. ctx = canvas.getContext('2d'),
  7. HEIGHT = window.innerHeight,
  8. WIDTH = window.innerWidth,
  9. TO_RADIANS = Math.PI / 360;
  10.  
  11. var raf = (function(){
  12. return window.requestAnimationFrame ||
  13. window.webkitRequestAnimationFrame ||
  14. window.mozRequestAnimationFrame ||
  15. window.oRequestAnimationFrame ||
  16. window.msRequestAnimationFrame ||
  17. function( callback ){
  18. window.setTimeout(callback, 1000 / 60);
  19. };
  20. })();
  21.  
  22. function clear(style) {
  23. ctx.save();
  24. ctx.fillStyle = style || 'rgba(0,0,0,0.01)';
  25. ctx.fillRect(0, 0, WIDTH, HEIGHT);
  26. ctx.restore();
  27. }
  28.  
  29. function square(x, y, w, h) {
  30. ctx.beginPath();
  31. ctx.rect(WIDTH/2 + x, HEIGHT/2 + y, w, h);
  32. ctx.closePath();
  33. ctx.fill();
  34. }
  35.  
  36. function circle(x, y, w, h) {
  37. ctx.beginPath();
  38. ctx.arc(WIDTH/2 + x, HEIGHT/2+ y, w, 0, Math.PI*2, true);
  39. ctx.closePath();
  40. ctx.fill();
  41. }
  42.  
  43. canvas.width = WIDTH;
  44. canvas.height = HEIGHT;
  45.  
  46. var offsetX = 0,
  47. offsetY = 0;
  48.  
  49. function draw() {
  50. clear();
  51.  
  52. offsetX+= 2;
  53. offsetY++;
  54. var x = Math.sin(WIDTH + offsetX * TO_RADIANS) * WIDTH/4;
  55. var y = Math.sin(HEIGHT + offsetY * TO_RADIANS) * HEIGHT/4;
  56.  
  57. ctx.fillStyle = 'cyan';
  58. circle(x, y, 2, 10);
  59. offsetX += 2;
  60.  
  61.  
  62. raf(draw, 16);
  63. }
  64.  
  65. clear('black');
  66. draw();
  67. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement