SGraphyLineItem.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * 线条
  3. */
  4. import SGraphyItem from '../SGraphyItem'
  5. import SRect from './../types/SRect';
  6. export default class SGraphyLineItem extends SGraphyItem {
  7. /**
  8. * 构造函数
  9. *
  10. * @param startX 线的起始x坐标
  11. * @param startY 线的起始y坐标
  12. * @param endX 线的终止x坐标
  13. * @param endY 线的终止y坐标
  14. * @param width 线的宽度
  15. *
  16. * @param color 线的颜色
  17. * @param isVirtual 是否为虚线
  18. */
  19. constructor(startX, startY, endX, endY, color, width, isVirtual, parent = null) {
  20. super(parent)
  21. this.startX = startX
  22. this.startY = startY
  23. this.endX = endX
  24. this.endY = endY
  25. this.color = color
  26. this.width = width
  27. this.isVirtual = isVirtual
  28. }
  29. /**
  30. * Item对象边界区域
  31. *
  32. * @return SRect
  33. */
  34. boundingRect() {
  35. return new SRect(0, 0, this.width, this.height)
  36. }
  37. /**
  38. * 绘制线条
  39. *
  40. * @param canvas 画布
  41. * @param rect 绘制区域
  42. */
  43. onDraw(canvas, rect) {
  44. if (this.isVirtual) {
  45. canvas.setLineDash([15, 5])
  46. }
  47. canvas.lineWidth = this.width || 1
  48. canvas.strokeStyle = this.color || '#000'
  49. canvas.beginPath();
  50. canvas.moveTo(this.startX, this.startY)
  51. canvas.lineTo(this.endX, this.endY)
  52. canvas.stroke()
  53. }
  54. }