Circle.js 1.4 KB

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