line.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. export default class drawLine {
  2. constructor(params) {
  3. const { Context, commonUtilMethods, commonDrawMethods } = params
  4. this.Context = Context
  5. this.commonUtilMethods = commonUtilMethods
  6. this.commonDrawMethods = commonDrawMethods
  7. }
  8. /**
  9. * 获取绘制参数
  10. * @param { Object } params 参数
  11. * @param { Boolean } conversion 是否需要转换单位
  12. */
  13. getDrawParams(params = {}, conversion = true) {
  14. const globalDataParams = this.commonUtilMethods.getGlobalDataDrawParams(params)
  15. // 绘制线的参数
  16. const { algin = 'right', lineType = 'solid', pattern = [6, 6], offset = 6, lineCap = 'butt' } = params
  17. const lineParams = {
  18. ...globalDataParams,
  19. algin,
  20. // dashed 虚线 solid 实线
  21. lineType,
  22. // 详看CanvasContext.setLineDash文档
  23. // 一组描述交替绘制线段和间距(坐标空间单位)长度的数字
  24. pattern,
  25. // 虚线偏移量
  26. offset,
  27. lineCap
  28. }
  29. return conversion ? this.commonUtilMethods.conversionUnit(lineParams) : lineParams
  30. }
  31. /**
  32. * 绘制线条
  33. * @param { Object } params 文字参数
  34. * @param { Boolean } conversion 是否需要转换单位
  35. */
  36. draw(params = {}, conversion = true) {
  37. const { Context, commonUtilMethods, commonDrawMethods } = this
  38. const { canvasWidth, is2d } = commonUtilMethods
  39. let {
  40. x, y, color, w, algin, alpha, lineType, pattern,
  41. offset, lineCap, lineWidth, windowAlign, offsetRight
  42. } = this.getDrawParams(params, conversion)
  43. Context.beginPath()
  44. commonDrawMethods.setAlpha(alpha)
  45. if (lineType === 'dashed') {
  46. if (!is2d) {
  47. Context.setLineDash(pattern, offset)
  48. } else {
  49. Context.setLineDash(pattern)
  50. Context.lineDashOffset = offset
  51. }
  52. }
  53. if (!is2d) {
  54. Context.setLineCap(lineCap)
  55. } else {
  56. Context.lineCap = lineCap
  57. }
  58. commonDrawMethods.setLineWidth(lineWidth)
  59. commonDrawMethods.setStrokeStyle(color)
  60. switch (algin) {
  61. case 'right':
  62. if (windowAlign !== 'none') {
  63. x = commonDrawMethods.computedCenterX(canvasWidth, w, windowAlign, offsetRight)
  64. }
  65. Context.moveTo(x, y)
  66. Context.lineTo(w + x, y)
  67. break
  68. case 'left':
  69. if (windowAlign !== 'none') {
  70. x = commonDrawMethods.computedCenterX(canvasWidth, w, windowAlign, offsetRight)
  71. }
  72. Context.moveTo(x, y)
  73. Context.lineTo(windowAlign == 'none' ? x - w : x + w, y)
  74. break
  75. case 'top':
  76. Context.moveTo(x, y)
  77. Context.lineTo(x, -(y + w))
  78. break
  79. case 'bottom':
  80. Context.moveTo(x, y)
  81. Context.lineTo(x, y + w)
  82. break
  83. }
  84. Context.stroke()
  85. Context.closePath()
  86. if (!is2d) {
  87. Context.setLineDash([0, 0])
  88. } else {
  89. Context.setLineDash([0, 0])
  90. Context.lineDashOffset = 0
  91. }
  92. commonDrawMethods.setAlpha(1)
  93. }
  94. }