Circle.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.type = "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};