94c111c7e5fe57664e6666e8723c3af1b945f3716bc6b743e5cf13d547633888cec1264a7a89603273b6f8e7c78dd545cc1ab80210afcc60be55faca0199ab 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * 圆弧
  3. */
  4. import Path, { PathProps } from '../Path';
  5. export class ArcShape {
  6. cx = 0;
  7. cy = 0;
  8. r = 0;
  9. startAngle = 0;
  10. endAngle = Math.PI * 2
  11. clockwise? = true
  12. }
  13. export interface ArcProps extends PathProps {
  14. shape?: Partial<ArcShape>
  15. }
  16. class Arc extends Path<ArcProps> {
  17. shape: ArcShape
  18. constructor(opts?: ArcProps) {
  19. super(opts);
  20. }
  21. getDefaultStyle() {
  22. return {
  23. stroke: '#000',
  24. fill: null as string
  25. };
  26. }
  27. getDefaultShape() {
  28. return new ArcShape();
  29. }
  30. buildPath(ctx: CanvasRenderingContext2D, shape: ArcShape) {
  31. const x = shape.cx;
  32. const y = shape.cy;
  33. const r = Math.max(shape.r, 0);
  34. const startAngle = shape.startAngle;
  35. const endAngle = shape.endAngle;
  36. const clockwise = shape.clockwise;
  37. const unitX = Math.cos(startAngle);
  38. const unitY = Math.sin(startAngle);
  39. ctx.moveTo(unitX * r + x, unitY * r + y);
  40. ctx.arc(x, y, r, startAngle, endAngle, !clockwise);
  41. }
  42. }
  43. Arc.prototype.type = 'arc';
  44. export default Arc;