triangle.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * 绘制三角形
  3. */
  4. export default class DrawTriangle {
  5. constructor(params) {
  6. const { Context, commonUtilMethods, commonDrawMethods } = params
  7. this.Context = Context
  8. this.commonUtilMethods = commonUtilMethods
  9. this.commonDrawMethods = commonDrawMethods
  10. }
  11. /**
  12. * 获取绘制参数
  13. * @param { Object } params 参数
  14. * @param { Boolean } conversion 是否需要转换单位
  15. */
  16. getDrawParams(params = {}, conversion = true) {
  17. const globalDataParams = this.commonUtilMethods.getGlobalDataDrawParams(params)
  18. // 绘制三角形的参数
  19. const { coordinate = [], drawType = 'isosceles', direction = 'top' } = params
  20. const triangleParams = {
  21. ...globalDataParams,
  22. // 当绘制类别是自定义的时候需要传递的参数
  23. coordinate,
  24. // 绘制三角形的类型
  25. // right: 直角三角形
  26. // isosceles: 等腰三角形
  27. // custom: 自定义时,x, y, 宽, 高都不需要传递。需要传递绘制点的坐标类型是数组(coordinate)
  28. // [[1, 3], [2, 3], [4, 5]]
  29. drawType,
  30. // 三角形顶点朝向 top, left, right, bottom
  31. direction,
  32. }
  33. return conversion ? this.commonUtilMethods.conversionUnit(triangleParams) : triangleParams
  34. }
  35. /**
  36. * 绘制三角形
  37. * @param { Object } params 矩形参数
  38. * @param { Boolean } conversion 是否需要转换单位
  39. */
  40. draw(params = {}, conversion = true) {
  41. const { Context, commonUtilMethods, commonDrawMethods } = this
  42. const { canvasWidth, is2d } = commonUtilMethods
  43. let {
  44. x, y, w, h, color, alpha, isFill, lineWidth, drawType, offsetRight,
  45. coordinate, rotate, windowAlign, direction, borderWidth, borderColor, drawImage
  46. } = this.getDrawParams(params, conversion)
  47. if (color === 'black' && drawImage) {
  48. color = 'none'
  49. } else if(color === 'black') {
  50. color = '#ffffff'
  51. }
  52. if (windowAlign !== 'none' && drawType != 'custom') {
  53. x = commonDrawMethods.computedCenterX(canvasWidth, w, windowAlign, offsetRight)
  54. }
  55. if (rotate.deg && drawType !== 'custom') {
  56. Context.save()
  57. commonDrawMethods.setTriangleRotate(x, y, w, h, rotate, drawType)
  58. }
  59. Context.beginPath()
  60. commonDrawMethods.setAlpha(alpha)
  61. if (drawType === 'isosceles') {
  62. switch (direction) {
  63. case 'top':
  64. Context.lineTo(x + w / 2, y)
  65. Context.lineTo(x, y + h)
  66. Context.lineTo(x + w, y + h)
  67. break
  68. case 'bottom':
  69. Context.lineTo(x, y)
  70. Context.lineTo(x + w, y)
  71. Context.lineTo(x + w / 2, y + h)
  72. break
  73. case 'right':
  74. Context.lineTo(x, y)
  75. Context.lineTo(x, y + h)
  76. Context.lineTo(x + w, y + h / 2)
  77. break
  78. case 'left':
  79. Context.lineTo(x + w, y)
  80. Context.lineTo(x + w, y + h)
  81. Context.lineTo(x, y + h / 2)
  82. break
  83. }
  84. } else if (drawType === 'right') {
  85. switch (direction) {
  86. case 'top':
  87. Context.lineTo(x, y)
  88. Context.lineTo(x, y + h)
  89. Context.lineTo(x + w, y + h)
  90. break
  91. case 'bottom':
  92. Context.lineTo(x, y)
  93. Context.lineTo(x + w, y)
  94. Context.lineTo(x, y + h)
  95. break
  96. case 'left':
  97. Context.lineTo(x, y)
  98. Context.lineTo(x, y + h)
  99. Context.lineTo(x + w, y)
  100. break
  101. case 'right':
  102. Context.lineTo(x, y + h)
  103. Context.lineTo(x + w, y + h)
  104. Context.lineTo(x + w, y)
  105. break
  106. }
  107. } else if (drawType === 'custom') {
  108. for (let i of coordinate) {
  109. Context.lineTo(i[0], i[1])
  110. }
  111. }
  112. Context.closePath()
  113. if (isFill) {
  114. if (borderWidth !== 0) {
  115. commonDrawMethods.setLineWidth(borderWidth)
  116. commonDrawMethods.setStrokeStyle(borderColor)
  117. Context.stroke()
  118. }
  119. if (!drawImage || (drawImage && color !== 'none')) {
  120. commonDrawMethods.setFillStyle(color)
  121. Context.fill()
  122. }
  123. } else {
  124. commonDrawMethods.setLineWidth(lineWidth)
  125. commonDrawMethods.setStrokeStyle(color)
  126. Context.stroke()
  127. }
  128. commonDrawMethods.setAlpha(1)
  129. if (rotate.deg && drawType !== 'custom') {
  130. Context.restore()
  131. }
  132. }
  133. }