Circle.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {Object2D} from "../Object2D.js";
  2. import {ColorStyle} from "./style/ColorStyle";
  3. import {Style} from "./style/Style";
  4. /**
  5. * Circle object draw a circular object, into the canvas.
  6. *
  7. * Can be used as a base to implement other circular objects, already implements the circle collision for pointer events.
  8. *
  9. * @class
  10. * @extends {Object2D}
  11. */
  12. function Circle()
  13. {
  14. Object2D.call(this);
  15. /**
  16. * Radius of the circle.
  17. *
  18. * @type {number}
  19. */
  20. this.radius = 10.0;
  21. /**
  22. * Style of the object border line.
  23. *
  24. * If set null it is ignored.
  25. *
  26. * @type {Style}
  27. */
  28. this.strokeStyle = new ColorStyle("#000000");
  29. /**
  30. * Line width, only used if a valid strokeStyle is defined.
  31. *
  32. * @type {number}
  33. */
  34. this.lineWidth = 1;
  35. /**
  36. * Background color of the circle.
  37. *
  38. * If set null it is ignored.
  39. *
  40. * @type {Style}
  41. */
  42. this.fillStyle = new ColorStyle("#FFFFFF");
  43. }
  44. Circle.prototype = Object.create(Object2D.prototype);
  45. Circle.prototype.constructor = Circle;
  46. Circle.prototype.type = "Circle";
  47. Object2D.register(Circle, "Circle");
  48. Circle.prototype.isInside = function(point)
  49. {
  50. return point.length() <= this.radius;
  51. };
  52. Circle.prototype.draw = function(context, viewport, canvas)
  53. {
  54. context.beginPath();
  55. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  56. if(this.fillStyle !== null)
  57. {
  58. context.fillStyle = this.fillStyle.get(context);
  59. context.fill();
  60. }
  61. if(this.strokeStyle !== null)
  62. {
  63. context.lineWidth = this.lineWidth;
  64. context.strokeStyle = this.strokeStyle.get(context);
  65. context.stroke();
  66. }
  67. };
  68. Circle.prototype.serialize = function(recursive)
  69. {
  70. var data = Object2D.prototype.serialize.call(this, recursive);
  71. data.radius = this.radius;
  72. data.strokeStyle = this.strokeStyle !== null ? this.strokeStyle.serialize() : null;
  73. data.lineWidth = this.lineWidth;
  74. data.fillStyle = this.fillStyle !== null ? this.fillStyle.serialize() : null;
  75. return data;
  76. };
  77. Circle.prototype.parse = function(data, root)
  78. {
  79. Object2D.prototype.parse.call(this, data, root);
  80. this.radius = data.radius;
  81. this.strokeStyle = data.strokeStyle !== null ? Style.parse(data.strokeStyle) : null;
  82. this.lineWidth = data.lineWidth;
  83. this.fillStyle = data.fillStyle !== null ? Style.parse(data.fillStyle) : null;
  84. };
  85. export {Circle};