f131bc5852889ad0ec0f7eb8ca4a84700b9a4db9dfd319d09755562e95fa95031060b0a86540256bd23c6b927b7f79abdb4dd01cdbcfbb6c8eca1359c63309 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * 玫瑰线
  3. * @module zrender/graphic/shape/Rose
  4. */
  5. import Path, { PathProps } from '../Path';
  6. const sin = Math.sin;
  7. const cos = Math.cos;
  8. const radian = Math.PI / 180;
  9. export class RoseShape {
  10. cx = 0
  11. cy = 0
  12. r: number[] = []
  13. k = 0
  14. n = 1
  15. }
  16. export interface RoseProps extends PathProps {
  17. shape?: Partial<RoseShape>
  18. }
  19. class Rose extends Path<RoseProps> {
  20. shape: RoseShape
  21. constructor(opts?: RoseProps) {
  22. super(opts);
  23. }
  24. getDefaultStyle() {
  25. return {
  26. stroke: '#000',
  27. fill: null as string
  28. };
  29. }
  30. getDefaultShape() {
  31. return new RoseShape();
  32. }
  33. buildPath(ctx: CanvasRenderingContext2D, shape: RoseShape) {
  34. const R = shape.r;
  35. const k = shape.k;
  36. const n = shape.n;
  37. const x0 = shape.cx;
  38. const y0 = shape.cy;
  39. let x;
  40. let y;
  41. let r;
  42. ctx.moveTo(x0, y0);
  43. for (let i = 0, len = R.length; i < len; i++) {
  44. r = R[i];
  45. for (let j = 0; j <= 360 * n; j++) {
  46. x = r
  47. * sin(k / n * j % 360 * radian)
  48. * cos(j * radian)
  49. + x0;
  50. y = r
  51. * sin(k / n * j % 360 * radian)
  52. * sin(j * radian)
  53. + y0;
  54. ctx.lineTo(x, y);
  55. }
  56. }
  57. }
  58. }
  59. Rose.prototype.type = 'rose';
  60. export default Rose;