Star.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { __extends } from "tslib";
  2. import Path from '../Path.js';
  3. var PI = Math.PI;
  4. var cos = Math.cos;
  5. var sin = Math.sin;
  6. var StarShape = (function () {
  7. function StarShape() {
  8. this.cx = 0;
  9. this.cy = 0;
  10. this.n = 3;
  11. this.r = 0;
  12. }
  13. return StarShape;
  14. }());
  15. export { StarShape };
  16. var Star = (function (_super) {
  17. __extends(Star, _super);
  18. function Star(opts) {
  19. return _super.call(this, opts) || this;
  20. }
  21. Star.prototype.getDefaultShape = function () {
  22. return new StarShape();
  23. };
  24. Star.prototype.buildPath = function (ctx, shape) {
  25. var n = shape.n;
  26. if (!n || n < 2) {
  27. return;
  28. }
  29. var x = shape.cx;
  30. var y = shape.cy;
  31. var r = shape.r;
  32. var r0 = shape.r0;
  33. if (r0 == null) {
  34. r0 = n > 4
  35. ? r * cos(2 * PI / n) / cos(PI / n)
  36. : r / 3;
  37. }
  38. var dStep = PI / n;
  39. var deg = -PI / 2;
  40. var xStart = x + r * cos(deg);
  41. var yStart = y + r * sin(deg);
  42. deg += dStep;
  43. ctx.moveTo(xStart, yStart);
  44. for (var i = 0, end = n * 2 - 1, ri = void 0; i < end; i++) {
  45. ri = i % 2 === 0 ? r0 : r;
  46. ctx.lineTo(x + ri * cos(deg), y + ri * sin(deg));
  47. deg += dStep;
  48. }
  49. ctx.closePath();
  50. };
  51. return Star;
  52. }(Path));
  53. Star.prototype.type = 'star';
  54. export default Star;