Circle.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {Object2D} from "../Object2D.js";
  2. /**
  3. * Circle object draw a circular object, into the canvas.
  4. *
  5. * Can be used as a base to implement other circular objects, already implements the circle collision for pointer events.
  6. *
  7. * @class
  8. * @extends {Object2D}
  9. */
  10. function Circle()
  11. {
  12. Object2D.call(this);
  13. /**
  14. * Radius of the circle.
  15. */
  16. this.radius = 10.0;
  17. /**
  18. * Style of the object border line.
  19. *
  20. * If set null it is ignored.
  21. */
  22. this.strokeStyle = "#000000";
  23. /**
  24. * Line width, only used if a valid strokeStyle is defined.
  25. */
  26. this.lineWidth = 1;
  27. /**
  28. * Background color of the circle.
  29. *
  30. * If set null it is ignored.
  31. */
  32. this.fillStyle = "#FFFFFF";
  33. }
  34. Circle.prototype = Object.create(Object2D.prototype);
  35. Circle.prototype.isInside = function(point)
  36. {
  37. return point.length() <= this.radius;
  38. };
  39. Circle.prototype.onPointerEnter = function(pointer, viewport)
  40. {
  41. this.fillStyle = "#CCCCCC";
  42. };
  43. Circle.prototype.onPointerLeave = function(pointer, viewport)
  44. {
  45. this.fillStyle = "#FFFFFF";
  46. };
  47. Circle.prototype.draw = function(context, viewport, canvas)
  48. {
  49. context.beginPath();
  50. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  51. if(this.fillStyle !== null)
  52. {
  53. context.fillStyle = this.fillStyle;
  54. context.fill();
  55. }
  56. if(this.strokeStyle !== null)
  57. {
  58. context.lineWidth = this.lineWidth;
  59. context.strokeStyle = this.strokeStyle;
  60. context.stroke();
  61. }
  62. };
  63. export {Circle};