Circle.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. this.radius = 10.0;
  19. /**
  20. * Style of the object border line.
  21. *
  22. * If set null it is ignored.
  23. *
  24. * @type {Style}
  25. */
  26. this.strokeStyle = new ColorStyle("#000000");
  27. /**
  28. * Line width, only used if a valid strokeStyle is defined.
  29. *
  30. * @type {number}
  31. */
  32. this.lineWidth = 1;
  33. /**
  34. * Background color of the circle.
  35. *
  36. * If set null it is ignored.
  37. *
  38. * @type {Style}
  39. */
  40. this.fillStyle = new ColorStyle("#FFFFFF");
  41. }
  42. Circle.prototype = Object.create(Object2D.prototype);
  43. Circle.prototype.constructor = Circle;
  44. Circle.prototype.type = "Circle";
  45. Object2D.register(Circle, "Circle");
  46. Circle.prototype.isInside = function(point)
  47. {
  48. return point.length() <= this.radius;
  49. };
  50. Circle.prototype.onPointerEnter = function(pointer, viewport)
  51. {
  52. this.fillStyle = new ColorStyle("#CCCCCC");
  53. };
  54. Circle.prototype.onPointerLeave = function(pointer, viewport)
  55. {
  56. this.fillStyle = new ColorStyle("#FFFFFF");
  57. };
  58. Circle.prototype.draw = function(context, viewport, canvas)
  59. {
  60. context.beginPath();
  61. context.arc(0, 0, this.radius, 0, 2 * Math.PI);
  62. if(this.fillStyle !== null)
  63. {
  64. context.fillStyle = this.fillStyle.get();
  65. context.fill();
  66. }
  67. if(this.strokeStyle !== null)
  68. {
  69. context.lineWidth = this.lineWidth;
  70. context.strokeStyle = this.strokeStyle.get();
  71. context.stroke();
  72. }
  73. };
  74. Circle.prototype.serialize = function(recursive)
  75. {
  76. var data = Object2D.prototype.serialize.call(this, recursive);
  77. data.radius = this.radius;
  78. data.strokeStyle = this.strokeStyle !== null ? this.strokeStyle.serialize() : null;
  79. data.lineWidth = this.lineWidth;
  80. data.fillStyle = this.fillStyle !== null ? this.fillStyle.serialize() : null;
  81. return data;
  82. };
  83. Circle.prototype.parse = function(data, root)
  84. {
  85. Object2D.prototype.parse.call(this, data, root);
  86. this.radius = data.radius;
  87. this.strokeStyle = data.strokeStyle !== null ? Style.parse(data.strokeStyle) : null;
  88. this.lineWidth = data.lineWidth;
  89. this.fillStyle = data.fillStyle !== null ? Style.parse(data.fillStyle) : null;
  90. };
  91. export {Circle};