Guest User

Untitled

a guest
May 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. // snake
  2. var snake = {
  3. // the positions of all the parts
  4. positions: [],
  5. // the radius of the snake
  6. radius: 1,
  7. // the color of the snake
  8. color: '#ffff00',
  9.  
  10. // inits the snake
  11. init: function() {
  12. this.positions.push({
  13. x: canvas.width / 2,
  14. y: canvas.height / 2
  15. });
  16.  
  17. for (let i = 1; i < 200; i++) {
  18. this.positions.push({
  19. x: canvas.width / 2,
  20. y: canvas.height / 2 + 2 * i * this.radius
  21. });
  22. }
  23. },
  24.  
  25. // renders the snake
  26. render: function() {
  27. ctx.clearRect(0, 0, canvas.width, canvas.height);
  28.  
  29. ctx.fillStyle = this.color;
  30.  
  31. for (let i = 0; i < this.positions.length; i++) {
  32. ctx.beginPath();
  33.  
  34. ctx.arc(
  35. this.positions[i].x,
  36. this.positions[i].y,
  37. this.radius,
  38. 0, 2 * Math.PI
  39. );
  40.  
  41. ctx.closePath();
  42. ctx.fill();
  43. }
  44. },
  45.  
  46. // moves the snake to a new point
  47. moveTo: function(x, y) {
  48. // positions[0] is the head of the snake
  49. this.positions[0].x = x;
  50. this.positions[0].y = y;
  51.  
  52. this.join();
  53. },
  54.  
  55. join: function() {
  56. // loop through each position
  57. for (let i = 1; i < this.positions.length; i++) {
  58. // get last and current position
  59. var last = this.positions[i - 1];
  60. var curr = this.positions[i];
  61.  
  62. // get difference in x and y of each position
  63. var dx = curr.x - last.x;
  64. var dy = curr.y - last.y;
  65.  
  66. // calculate the angle between the two parts of the snake
  67. var angle = Math.atan2(dy, dx);
  68.  
  69. // get the new x and new y using polar coordinates
  70. var nx = 2 * this.radius * Math.cos(angle);
  71. var ny = 2 * this.radius * Math.sin(angle);
  72.  
  73. // add the new x and new y to the last snake's position to "join" the two
  74. //together without a gap
  75. curr.x = nx + last.x;
  76. curr.y = ny + last.y;
  77. }
  78. },
  79.  
  80. // slithers the snake smoothly to a new point
  81. slither (x, y) {
  82. var head = snake.positions[0];
  83.  
  84. // smooth movement
  85. var nx = head.x + (x - head.x)/10 ;
  86. var ny = head.y + (y - head.y) /10;
  87.  
  88. this.moveTo(nx, ny);
  89. }
  90. };
  91.  
  92. // inits the snake
  93. snake.init();
  94.  
  95. var mouse = false;
  96. window.addEventListener('mousemove', function(e) {
  97. mouse = {
  98. x: e.clientX,
  99. y: e.clientY
  100. };
  101. });
  102.  
  103. window.requestAnimationFrame(function loop() {
  104. window.requestAnimationFrame(loop);
  105.  
  106. // slithers the snake to a new point
  107. if (mouse) {
  108. snake.slither(mouse.x, mouse.y);
  109. }
  110.  
  111. // renders the snake
  112. snake.render();
Add Comment
Please, Sign In to add comment