Circle.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.constructor = Circle;
  36. Circle.prototype.type = "Circle";
  37. Object2D.register(Circle, "Circle");
  38. Circle.prototype.isInside = function(point)
  39. {
  40. return point.length() <= this.radius;
  41. };
  42. Circle.prototype.onPointerEnter = function(pointer, viewport)
  43. {
  44. this.fillStyle = "#CCCCCC";
  45. };
  46. Circle.prototype.onPointerLeave = function(pointer, viewport)
  47. {
  48. this.fillStyle = "#FFFFFF";
  49. };
  50. Circle.prototype.draw = function(context, viewport, canvas)
  51. {
  52. context.beginPath();
  53. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  54. if(this.fillStyle !== null)
  55. {
  56. context.fillStyle = this.fillStyle;
  57. context.fill();
  58. }
  59. if(this.strokeStyle !== null)
  60. {
  61. context.lineWidth = this.lineWidth;
  62. context.strokeStyle = this.strokeStyle;
  63. context.stroke();
  64. }
  65. };
  66. Circle.prototype.serialize = function(recursive)
  67. {
  68. var data = Object2D.prototype.serialize.call(this, recursive);
  69. data.radius = this.radius;
  70. data.strokeStyle = this.strokeStyle;
  71. data.lineWidth = this.lineWidth;
  72. data.fillStyle = this.fillStyle;
  73. return data;
  74. };
  75. Circle.prototype.parse = function(data)
  76. {
  77. Object2D.prototype.parse.call(this, data);
  78. this.radius = data.radius;
  79. this.strokeStyle = data.strokeStyle;
  80. this.lineWidth = data.lineWidth;
  81. this.fillStyle = data.fillStyle;
  82. };
  83. export {Circle};