poster.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. let system = wx.getSystemInfoSync()
  74. let scale = system.windowWidth / 375 //按照苹果留 375*667比例 其他型号手机等比例缩放 显示
  75. return { w: system.windowWidth, h: system.windowHeight, scale: scale }
  76. }
  77. /**
  78. * @description: 绘制文本时文本的总体高度
  79. * @param {Object} ctx canvas上下文
  80. * @param {String} text 需要输入的文本
  81. * @param {Number} x X轴起始位置
  82. * @param {Number} y Y轴起始位置
  83. * @param {Number} maxWidth 单行最大宽度
  84. * @param {Number} fontSize 字体大小
  85. * @param {String} color 字体颜色
  86. * @param {Number} lineHeight 行高
  87. * @param {String} textAlign 字体对齐方式
  88. */
  89. export function drawTextReturnH(
  90. ctx,
  91. text,
  92. x,
  93. y,
  94. maxWidth = 375,
  95. fontSize = 14,
  96. color = '#000',
  97. lineHeight = 30,
  98. textAlign = 'left'
  99. ) {
  100. if (textAlign) {
  101. ctx.setTextAlign(textAlign) //设置文本的水平对齐方式 ctx.setTextAlign这个可以兼容百度小程序 ,注意:ctx.textAlign百度小程序有问题
  102. switch (textAlign) {
  103. case 'center':
  104. x = getSystem().w / 2
  105. break
  106. case 'right':
  107. x = (getSystem().w - maxWidth) / 2 + maxWidth
  108. break
  109. default:
  110. // 左对齐
  111. x = (getSystem().w - maxWidth) / 2
  112. break
  113. }
  114. }
  115. let arrText = text.split('')
  116. let line = ''
  117. for (let n = 0; n < arrText.length; n++) {
  118. let testLine = line + arrText[n]
  119. ctx.font = fontSize + 'px sans-serif' //设置字体大小,注意:百度小程序 用ctx.setFontSize设置字体大小后,计算字体宽度会无效
  120. ctx.setFillStyle(color) //设置字体颜色
  121. let metrics = ctx.measureText(testLine) //measureText() 方法返回包含一个对象,该对象包含以像素计的指定字体宽度。
  122. let testWidth = metrics.width
  123. if (testWidth > maxWidth && n > 0) {
  124. ctx.fillText(line, x, y)
  125. line = arrText[n]
  126. y += lineHeight
  127. } else {
  128. line = testLine
  129. }
  130. }
  131. ctx.fillText(line, x, y)
  132. ctx.draw(true) //本次绘制是否接着上一次绘制。即 reserve 参数为 false,则在本次调用绘制之前 native 层会先清空画布再继续绘制;若 reserve 参数为 true,则保留当前画布上的内容,本次调用 drawCanvas 绘制的内容覆盖在上面,默认 false。
  133. return y
  134. }