Star.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var Path = require("../Path");
  2. /**
  3. * n角星(n>3)
  4. * @module zrender/graphic/shape/Star
  5. */
  6. var PI = Math.PI;
  7. var cos = Math.cos;
  8. var sin = Math.sin;
  9. var _default = Path.extend({
  10. type: 'star',
  11. shape: {
  12. cx: 0,
  13. cy: 0,
  14. n: 3,
  15. r0: null,
  16. r: 0
  17. },
  18. buildPath: function (ctx, shape) {
  19. var n = shape.n;
  20. if (!n || n < 2) {
  21. return;
  22. }
  23. var x = shape.cx;
  24. var y = shape.cy;
  25. var r = shape.r;
  26. var r0 = shape.r0; // 如果未指定内部顶点外接圆半径,则自动计算
  27. if (r0 == null) {
  28. r0 = n > 4 // 相隔的外部顶点的连线的交点,
  29. // 被取为内部交点,以此计算r0
  30. ? r * cos(2 * PI / n) / cos(PI / n) // 二三四角星的特殊处理
  31. : r / 3;
  32. }
  33. var dStep = PI / n;
  34. var deg = -PI / 2;
  35. var xStart = x + r * cos(deg);
  36. var yStart = y + r * sin(deg);
  37. deg += dStep; // 记录边界点,用于判断inside
  38. ctx.moveTo(xStart, yStart);
  39. for (var i = 0, end = n * 2 - 1, ri; i < end; i++) {
  40. ri = i % 2 === 0 ? r0 : r;
  41. ctx.lineTo(x + ri * cos(deg), y + ri * sin(deg));
  42. deg += dStep;
  43. }
  44. ctx.closePath();
  45. }
  46. });
  47. module.exports = _default;