circle.js 970 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. function Circle() {
  2. var x = 10, y = 10, radius = 5;
  3. var velocity = {x: 1, y: 1};
  4. this.init = function(canvas) {
  5. x = Math.floor(canvas.width * Math.random());
  6. y = Math.floor(canvas.height * Math.random());
  7. velocity.x = Math.floor(10 * Math.random() - 5);
  8. velocity.y = Math.floor(10 * Math.random() - 5);
  9. };
  10. this.update = function(canvas) {
  11. x += velocity.x;
  12. y += velocity.y;
  13. if(x + radius > canvas.width) {
  14. x = canvas.width - radius;
  15. velocity.x *= -1.1;
  16. }
  17. if(x < radius) {
  18. x = radius;
  19. velocity.x *= -1.1;
  20. }
  21. if(y + radius > canvas.height) {
  22. y = canvas.height - radius;
  23. velocity.y *= -1.1;
  24. }
  25. if(y < radius) {
  26. y = radius;
  27. velocity.y *= -1.1;
  28. }
  29. velocity.x = Math.max(-5, Math.min(5, velocity.x));
  30. velocity.y = Math.max(-5, Math.min(5, velocity.y));
  31. },
  32. this.draw = function(context) {
  33. context.strokeStyle = "#000000";
  34. context.beginPath();
  35. context.arc(x, y, radius, 0, 2 * Math.PI);
  36. context.stroke();
  37. }
  38. }