poster.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * @Description: 公共方法
  3. * @Version: 1.0.0
  4. * @Autor: hch
  5. * @Date: 2021-07-22 00:01:09
  6. */
  7. /**
  8. * @description: 绘制正方形(可以定义圆角),并且有图片地址的话填充图片
  9. * @param {CanvasContext} ctx canvas上下文
  10. * @param {number} x 圆角矩形选区的左上角 x坐标
  11. * @param {number} y 圆角矩形选区的左上角 y坐标
  12. * @param {number} w 圆角矩形选区的宽度
  13. * @param {number} h 圆角矩形选区的高度
  14. * @param {number} r 圆角的半径
  15. * @param {String} url 图片的url地址
  16. */
  17. export function drawSquarePic(ctx, x, y, w, h, r, url, color) {
  18. ctx.save()
  19. ctx.beginPath()
  20. // 绘制左上角圆弧
  21. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5)
  22. // 绘制border-top
  23. // 画一条线 x终点、y终点
  24. ctx.lineTo(x + w - r, y)
  25. // 绘制右上角圆弧
  26. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2)
  27. // 绘制border-right
  28. ctx.lineTo(x + w, y + h - r)
  29. // 绘制右下角圆弧
  30. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5)
  31. // 绘制左下角圆弧
  32. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI)
  33. // 绘制border-left
  34. ctx.lineTo(x, y + r)
  35. // 填充颜色(需要可以自行修改)
  36. ctx.setFillStyle(color || '#ffffff')
  37. ctx.fill()
  38. // 剪切,剪切之后的绘画绘制剪切区域内进行,需要save与restore 这个很重要 不然没办法保存
  39. ctx.clip()
  40. // 绘制图片
  41. return new Promise((resolve, reject) => {
  42. if (url) {
  43. uni.getImageInfo({
  44. src: url,
  45. success(res) {
  46. ctx.drawImage(res.path, x, y, w, h)
  47. ctx.restore() //恢复之前被切割的canvas,否则切割之外的就没办法用
  48. ctx.draw(true)
  49. resolve()
  50. },
  51. fail(res) {
  52. console.log('fail -> res', res)
  53. uni.showToast({
  54. title: '图片下载异常',
  55. duration: 2000,
  56. icon: 'none'
  57. })
  58. }
  59. })
  60. } else {
  61. ctx.draw(true)
  62. resolve()
  63. }
  64. })
  65. }
  66. /**
  67. * @description: 获取设备信息
  68. * @param {type}
  69. * @return {type}
  70. * @author: hch
  71. */
  72. export function getSystem() {
  73. // #ifdef H5
  74. let system = uni.getSystemInfoSync()
  75. // #endif
  76. // #ifndef H5
  77. let system = wx.getSystemInfoSync()
  78. // #endif
  79. let scale = system.windowWidth / 375 //按照苹果留 375*667比例 其他型号手机等比例缩放 显示
  80. return { w: system.windowWidth, h: system.windowHeight, scale: scale }
  81. }
  82. /**
  83. * @description: 绘制文本时文本的总体高度
  84. * @param {Object} ctx canvas上下文
  85. * @param {String} text 需要输入的文本
  86. * @param {Number} x X轴起始位置
  87. * @param {Number} y Y轴起始位置
  88. * @param {Number} maxWidth 单行最大宽度
  89. * @param {Number} fontSize 字体大小
  90. * @param {String} color 字体颜色
  91. * @param {Number} lineHeight 行高
  92. * @param {String} textAlign 字体对齐方式
  93. */
  94. export function drawTextReturnH(
  95. ctx,
  96. text,
  97. x,
  98. y,
  99. maxWidth = 375,
  100. fontSize = 14,
  101. color = '#000',
  102. lineHeight = 30,
  103. textAlign = 'left'
  104. ) {
  105. if (textAlign) {
  106. ctx.setTextAlign(textAlign) //设置文本的水平对齐方式 ctx.setTextAlign这个可以兼容百度小程序 ,注意:ctx.textAlign百度小程序有问题
  107. switch (textAlign) {
  108. case 'center':
  109. x = getSystem().w / 2
  110. break
  111. case 'right':
  112. x = (getSystem().w - maxWidth) / 2 + maxWidth
  113. break
  114. default:
  115. // 左对齐
  116. x = (getSystem().w - maxWidth) / 2
  117. break
  118. }
  119. }
  120. let arrText = text.split('')
  121. let line = ''
  122. for (let n = 0; n < arrText.length; n++) {
  123. let testLine = line + arrText[n]
  124. ctx.font = fontSize + 'px sans-serif' //设置字体大小,注意:百度小程序 用ctx.setFontSize设置字体大小后,计算字体宽度会无效
  125. ctx.setFillStyle(color) //设置字体颜色
  126. let metrics = ctx.measureText(testLine) //measureText() 方法返回包含一个对象,该对象包含以像素计的指定字体宽度。
  127. let testWidth = metrics.width
  128. if (testWidth > maxWidth && n > 0) {
  129. ctx.fillText(line, x, y)
  130. line = arrText[n]
  131. y += lineHeight
  132. } else {
  133. line = testLine
  134. }
  135. }
  136. ctx.fillText(line, x, y)
  137. ctx.draw(true) //本次绘制是否接着上一次绘制。即 reserve 参数为 false,则在本次调用绘制之前 native 层会先清空画布再继续绘制;若 reserve 参数为 true,则保留当前画布上的内容,本次调用 drawCanvas 绘制的内容覆盖在上面,默认 false。
  138. return y
  139. }