a057001f83c9dfd42dd4bdadf22f86930c824a867248030e844dc3ec66597ce862708c3b033e655ed4c856231ee9f55003fcefa13d7a3696392b9e9c147335 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * 正多边形
  3. */
  4. import Path, { PathProps } from '../Path';
  5. const PI = Math.PI;
  6. const sin = Math.sin;
  7. const cos = Math.cos;
  8. export class IsogonShape {
  9. x = 0
  10. y = 0
  11. r = 0
  12. n = 0
  13. }
  14. export interface IsogonProps extends PathProps {
  15. shape?: Partial<IsogonShape>
  16. }
  17. class Isogon extends Path<IsogonProps> {
  18. shape: IsogonShape
  19. constructor(opts?: IsogonProps) {
  20. super(opts);
  21. }
  22. getDefaultShape() {
  23. return new IsogonShape();
  24. }
  25. buildPath(ctx: CanvasRenderingContext2D, shape: IsogonShape) {
  26. const n = shape.n;
  27. if (!n || n < 2) {
  28. return;
  29. }
  30. const x = shape.x;
  31. const y = shape.y;
  32. const r = shape.r;
  33. const dStep = 2 * PI / n;
  34. let deg = -PI / 2;
  35. ctx.moveTo(x + r * cos(deg), y + r * sin(deg));
  36. for (let i = 0, end = n - 1; i < end; i++) {
  37. deg += dStep;
  38. ctx.lineTo(x + r * cos(deg), y + r * sin(deg));
  39. }
  40. ctx.closePath();
  41. return;
  42. }
  43. }
  44. Isogon.prototype.type = 'isogon';
  45. export default Isogon;