rect.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. export default class DrawRect {
  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. return conversion ? this.commonUtilMethods.conversionUnit(globalDataParams) : globalDataParams
  16. }
  17. /**
  18. * 绘制矩形
  19. * @param { Object } params 矩形参数
  20. * @param { Boolean } conversion 是否需要转换单位
  21. */
  22. draw(params = {}, conversion = true) {
  23. const { Context, commonUtilMethods, commonDrawMethods } = this
  24. const { canvasWidth, is2d, pxToRpx, unit } = commonUtilMethods
  25. let {
  26. x, y, w, h, r, color, alpha, isFill, lineWidth,
  27. windowAlign, rotate, drawImage,
  28. borderColor, borderWidth, borderType, shadow, gradient, offsetRight
  29. } = this.getDrawParams(params, conversion)
  30. if (color === 'black' && drawImage) {
  31. color = 'none'
  32. } else if(color === 'black') {
  33. color = '#ffffff'
  34. }
  35. if (typeof r === 'number') {
  36. if (r * 2 > h) {
  37. r = h / 2
  38. }
  39. }
  40. if (windowAlign !== 'none') {
  41. x = commonDrawMethods.computedCenterX(canvasWidth, w, windowAlign, offsetRight) || x
  42. }
  43. if (!drawImage && rotate.deg) {
  44. Context.save()
  45. commonDrawMethods.setRotate(x, y, w, h, rotate)
  46. }
  47. if (drawImage && shadow.show) {
  48. commonDrawMethods.setShadow(shadow.x, shadow.y, shadow.blur, shadow.color)
  49. }
  50. if (!drawImage && shadow.show) {
  51. // Context.save()
  52. commonDrawMethods.setShadow(shadow.x, shadow.y, shadow.blur, shadow.color)
  53. }
  54. Context.beginPath()
  55. commonDrawMethods.setAlpha(alpha)
  56. let tr = 0
  57. let tl = 0
  58. let br = 0
  59. let bl = 0
  60. if (typeof r === 'object') {
  61. const length = r.length
  62. if (length === 1) {
  63. tl = r[0]
  64. tr = r[0]
  65. br = r[0]
  66. bl = r[0]
  67. }
  68. if (length === 2) {
  69. tl = r[0]
  70. tr = r[1]
  71. br = r[0]
  72. bl = r[1]
  73. }
  74. if (length === 3) {
  75. tl = r[0]
  76. tr = r[1]
  77. br = r[2]
  78. bl = r[1]
  79. }
  80. if (length === 4) {
  81. tl = r[0]
  82. tr = r[1]
  83. br = r[2]
  84. bl = r[3]
  85. }
  86. } else if (typeof borderType === 'string') {
  87. switch(borderType) {
  88. case 'tr':
  89. tr = r
  90. break
  91. case 'tl':
  92. tl = r
  93. break
  94. case 'br':
  95. br = r
  96. break
  97. case 'bl':
  98. bl = r
  99. break
  100. default:
  101. tr = r
  102. tl = r
  103. br = r
  104. bl = r
  105. }
  106. } else if (borderType instanceof Array && typeof r === 'number') {
  107. if (borderType.includes('tr')) {
  108. tr = r
  109. }
  110. if (borderType.includes('tl')) {
  111. tl = r
  112. }
  113. if (borderType.includes('br')) {
  114. br = r
  115. }
  116. if (borderType.includes('bl')) {
  117. bl = r
  118. }
  119. if (borderType.includes('default')) {
  120. tr = r
  121. tl = r
  122. br = r
  123. bl = r
  124. }
  125. }
  126. // 上右 tr
  127. Context.lineTo(x + tl, y)
  128. Context.arc(x + w - tr, y + tr, tr, Math.PI * 1.5, 0, false)
  129. // 下右 br
  130. Context.lineTo(x + w, y + h - br)
  131. Context.arc(x + w - br,y + h - br, br, 0, Math.PI * .5, false)
  132. // 下左 bl
  133. Context.lineTo(x + bl, y + h)
  134. Context.arc(x + bl, y + h - bl, bl, Math.PI * .5, Math.PI, false)
  135. // 上左 tl
  136. Context.lineTo(x, y + tl)
  137. Context.arc(x + tl, y + tl, tl, Math.PI * 1, Math.PI * 1.5, false)
  138. Context.closePath()
  139. if (isFill) {
  140. if (borderWidth != 0) {
  141. // console.log(borderWidth)
  142. // console.log(borderColor)
  143. commonDrawMethods.setLineWidth(borderWidth)
  144. commonDrawMethods.setStrokeStyle(borderColor)
  145. Context.stroke()
  146. }
  147. if (gradient.show) {
  148. const { type = 'linear', relative, position, colors } = gradient
  149. color = commonUtilMethods.getGradient(type, relative, commonUtilMethods.conversionUnit(position), colors, { x, y })
  150. }
  151. if (!drawImage || (drawImage && color !== 'none')) {
  152. commonDrawMethods.setFillStyle(color)
  153. Context.fill()
  154. }
  155. } else {
  156. commonDrawMethods.setLineWidth(lineWidth)
  157. commonDrawMethods.setStrokeStyle(color)
  158. Context.stroke()
  159. }
  160. commonDrawMethods.setAlpha(1)
  161. if (!drawImage && rotate.deg) {
  162. Context.restore()
  163. }
  164. if (shadow.show) {
  165. commonDrawMethods.setShadow(0, 0, 0, 'black')
  166. }
  167. return {
  168. success: true,
  169. w: unit === 'rpx' ? pxToRpx(w) : w,
  170. h: unit === 'rpx' ? pxToRpx(h) : h
  171. }
  172. }
  173. }