Line.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var Path = require("../Path");
  2. var _subPixelOptimize = require("../helper/subPixelOptimize");
  3. var subPixelOptimizeLine = _subPixelOptimize.subPixelOptimizeLine;
  4. /**
  5. * 直线
  6. * @module zrender/graphic/shape/Line
  7. */
  8. // Avoid create repeatly.
  9. var subPixelOptimizeOutputShape = {};
  10. var _default = Path.extend({
  11. type: 'line',
  12. shape: {
  13. // Start point
  14. x1: 0,
  15. y1: 0,
  16. // End point
  17. x2: 0,
  18. y2: 0,
  19. percent: 1
  20. },
  21. style: {
  22. stroke: '#000',
  23. fill: null
  24. },
  25. buildPath: function (ctx, shape) {
  26. var x1;
  27. var y1;
  28. var x2;
  29. var y2;
  30. if (this.subPixelOptimize) {
  31. subPixelOptimizeLine(subPixelOptimizeOutputShape, shape, this.style);
  32. x1 = subPixelOptimizeOutputShape.x1;
  33. y1 = subPixelOptimizeOutputShape.y1;
  34. x2 = subPixelOptimizeOutputShape.x2;
  35. y2 = subPixelOptimizeOutputShape.y2;
  36. } else {
  37. x1 = shape.x1;
  38. y1 = shape.y1;
  39. x2 = shape.x2;
  40. y2 = shape.y2;
  41. }
  42. var percent = shape.percent;
  43. if (percent === 0) {
  44. return;
  45. }
  46. ctx.moveTo(x1, y1);
  47. if (percent < 1) {
  48. x2 = x1 * (1 - percent) + x2 * percent;
  49. y2 = y1 * (1 - percent) + y2 * percent;
  50. }
  51. ctx.lineTo(x2, y2);
  52. },
  53. /**
  54. * Get point at percent
  55. * @param {number} percent
  56. * @return {Array.<number>}
  57. */
  58. pointAt: function (p) {
  59. var shape = this.shape;
  60. return [shape.x1 * (1 - p) + shape.x2 * p, shape.y1 * (1 - p) + shape.y2 * p];
  61. }
  62. });
  63. module.exports = _default;