b9537b365f0863e109bf3ceed655b6d509c23fbc25c48d19f8add4bbcc6b32025874bf58515c57be85eeebee61e3d0c828f4295d7dd1808fcefbc44e3e989b 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * 直线
  3. * @module zrender/graphic/shape/Line
  4. */
  5. import Path, { PathProps } from '../Path';
  6. import {subPixelOptimizeLine} from '../helper/subPixelOptimize';
  7. import { VectorArray } from '../../core/vector';
  8. // Avoid create repeatly.
  9. const subPixelOptimizeOutputShape = {};
  10. export class LineShape {
  11. // Start point
  12. x1 = 0
  13. y1 = 0
  14. // End point
  15. x2 = 0
  16. y2 = 0
  17. percent = 1
  18. }
  19. export interface LineProps extends PathProps {
  20. shape?: Partial<LineShape>
  21. }
  22. class Line extends Path<LineProps> {
  23. shape: LineShape
  24. constructor(opts?: LineProps) {
  25. super(opts);
  26. }
  27. getDefaultStyle() {
  28. return {
  29. stroke: '#000',
  30. fill: null as string
  31. };
  32. }
  33. getDefaultShape() {
  34. return new LineShape();
  35. }
  36. buildPath(ctx: CanvasRenderingContext2D, shape: LineShape) {
  37. let x1;
  38. let y1;
  39. let x2;
  40. let y2;
  41. if (this.subPixelOptimize) {
  42. const optimizedShape = subPixelOptimizeLine(
  43. subPixelOptimizeOutputShape, shape, this.style
  44. );
  45. x1 = optimizedShape.x1;
  46. y1 = optimizedShape.y1;
  47. x2 = optimizedShape.x2;
  48. y2 = optimizedShape.y2;
  49. }
  50. else {
  51. x1 = shape.x1;
  52. y1 = shape.y1;
  53. x2 = shape.x2;
  54. y2 = shape.y2;
  55. }
  56. const percent = shape.percent;
  57. if (percent === 0) {
  58. return;
  59. }
  60. ctx.moveTo(x1, y1);
  61. if (percent < 1) {
  62. x2 = x1 * (1 - percent) + x2 * percent;
  63. y2 = y1 * (1 - percent) + y2 * percent;
  64. }
  65. ctx.lineTo(x2, y2);
  66. }
  67. /**
  68. * Get point at percent
  69. */
  70. pointAt(p: number): VectorArray {
  71. const shape = this.shape;
  72. return [
  73. shape.x1 * (1 - p) + shape.x2 * p,
  74. shape.y1 * (1 - p) + shape.y2 * p
  75. ];
  76. }
  77. }
  78. Line.prototype.type = 'line';
  79. export default Line;