semicircle.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Semicircle extension for L.Circle.
  3. * Jan Pieter Waagmeester <jieter@jieter.nl>
  4. */
  5. /*jshint browser:true, strict:false, globalstrict:false, indent:4, white:true, smarttabs:true*/
  6. /*global L:true*/
  7. (function (L) {
  8. // save original getPathString function to draw a full circle.
  9. var original_getPathString = L.Circle.prototype.getPathString;
  10. L.Circle = L.Circle.extend({
  11. options: {
  12. startAngle: 0,
  13. stopAngle: 359.9999
  14. },
  15. // make sure 0 degrees is up (North) and convert to radians.
  16. _fixAngle: function (angle) {
  17. return (angle - 90) * L.LatLng.DEG_TO_RAD;
  18. },
  19. startAngle: function () {
  20. return this._fixAngle(this.options.startAngle);
  21. },
  22. stopAngle: function () {
  23. return this._fixAngle(this.options.stopAngle);
  24. },
  25. //rotate point x,y+r around x,y with angle.
  26. rotated: function (angle, r) {
  27. return this._point.add(
  28. L.point(Math.cos(angle), Math.sin(angle)).multiplyBy(r)
  29. ).round();
  30. },
  31. getPathString: function () {
  32. var center = this._point,
  33. r = this._radius;
  34. // If we want a circle, we use the original function
  35. if (this.options.startAngle === 0 && this.options.stopAngle > 359) {
  36. return original_getPathString.call(this);
  37. }
  38. var start = this.rotated(this.startAngle(), r),
  39. end = this.rotated(this.stopAngle(), r);
  40. if (this._checkIfEmpty()) {
  41. return '';
  42. }
  43. if (L.Browser.svg) {
  44. var largeArc = (this.options.stopAngle - this.options.startAngle >= 180) ? '1' : '0';
  45. //move to center
  46. var ret = "M" + center.x + "," + center.y;
  47. //lineTo point on circle startangle from center
  48. ret += "L " + start.x + "," + start.y;
  49. //make circle from point start - end:
  50. ret += "A " + r + "," + r + ",0," + largeArc + ",1," + end.x + "," + end.y + " z";
  51. return ret;
  52. } else {
  53. //TODO: fix this for semicircle...
  54. center._round();
  55. r = Math.round(r);
  56. return "A " + center.x + "," + center.y + " " + r + "," + r + " 0," + (65535 * 360);
  57. }
  58. },
  59. setStartAngle: function (angle) {
  60. this.options.startAngle = angle;
  61. return this.redraw();
  62. },
  63. setStopAngle: function (angle) {
  64. this.options.stopAngle = angle;
  65. return this.redraw();
  66. },
  67. setDirection: function (direction, degrees) {
  68. if (degrees === undefined) {
  69. degrees = 10;
  70. }
  71. this.options.startAngle = direction - (degrees / 2);
  72. this.options.stopAngle = direction + (degrees / 2);
  73. return this.redraw();
  74. }
  75. });
  76. L.Circle.include(!L.Path.CANVAS ? {} : {
  77. _drawPath: function () {
  78. var center = this._point,
  79. r = this._radius;
  80. var start = this.rotated(this.startAngle(), r);
  81. this._ctx.beginPath();
  82. this._ctx.moveTo(center.x, center.y);
  83. this._ctx.lineTo(start.x, start.y);
  84. this._ctx.arc(center.x, center.y, this._radius,
  85. this.startAngle(), this.stopAngle(), false);
  86. this._ctx.lineTo(center.x, center.y);
  87. }
  88. // _containsPoint: function (p) {
  89. // TODO: fix for semicircle.
  90. // var center = this._point,
  91. // w2 = this.options.stroke ? this.options.weight / 2 : 0;
  92. // return (p.distanceTo(center) <= this._radius + w2);
  93. // }
  94. });
  95. })(L);