draw_function.js 8.9 KB
const _ctx_ = Symbol('_ctx_')
const _pixelRatio_ = Symbol('_pixelRatio_')
class Draw_Function {
  [_ctx_] = null;
  [_pixelRatio_] = null;
  constructor(_this_) {
    this[_ctx_] = _this_.ctx
    this[_pixelRatio_] = _this_.data.pixelRatio || 2
  }
  drawImages(imageParams) {
    this[_ctx_].beginPath()
    this[_ctx_].save()
    let {
      borderRadius,
      height,
      width,
      url,
      x,
      y,
    } = imageParams
    height = this.toRpx(height)
    width = this.toRpx(width)
    x = this.toRpx(x)
    y = this.toRpx(y)
    if (borderRadius) {
      this.drawRadiusRect({
        borderRadius,
        height,
        width,
        x,
        y,
      })
      this[_ctx_].clip()
    }
    this[_ctx_].drawImage(url, x, y, width, height)
    this[_ctx_].restore()
  }
  toRpx(val) {
    return this[_pixelRatio_] * val
  }
  getTextWidth(text, fontSize) {
    if (fontSize) this[_ctx_].setFontSize(fontSize)
    return this[_ctx_].measureText(text).width
  }
  drawFill(x, y, width, height, color) {
    this[_ctx_].beginPath()
    this[_ctx_].setFillStyle(color || '#000')
    this[_ctx_].fillRect(x, y, width, height)
    this[_ctx_].stroke()
    this[_ctx_].restore()
    this[_ctx_].save()
  }
  drawText(textParams) {
    let {
      textDecoration,
      textBaseline,
      lineHeight,
      fontWeight,
      textAlign,
      textWidth,
      maxWidth,
      fontSize,
      opacity,
      lineNum,
      width,
      color,
      text,
      x,
      y,
    } = textParams
    let text_width = this.getTextWidth(text)
    textAlign = textAlign || 'left'
    textBaseline = textBaseline || 'normal'
    opacity = typeof opacity === 'number' ? opacity : 1
    lineHeight = this.toRpx(lineHeight || fontSize || 12)
    textWidth = this.toRpx(textWidth || 0)
    fontSize = this.toRpx(fontSize || 12)
    maxWidth = this.toRpx(maxWidth || width)
    width = this.toRpx(width)
    x = this.toRpx(x)
    y = this.toRpx(y)
    let fontX = this.getFontWidth({
      textAlign,
      fontSize,
      text,
      x,
    })
    if (textParams.border) {
      let boxX = this.toRpx(textParams.x - 4.5),
        boxY = this.toRpx(textParams.y - 0.5),
        boxWidth = text_width + this.toRpx(9),
        boxHeight = lineHeight + this.toRpx(4)
      if (boxWidth > width - fontX - this.getTextWidth('…') - fontSize) {
        boxWidth = width - boxX + this.toRpx(9)
      }
      this.drawFill(boxX, boxY, boxWidth, boxHeight, textParams.border)
    }
    if (textParams.background) {
      let boxX = this.toRpx(textParams.x - 4),
        boxY = this.toRpx(textParams.y),
        boxWidth = text_width + this.toRpx(8),
        boxHeight = lineHeight + this.toRpx(3)
      if (boxWidth > width - fontX - this.getTextWidth('…') - fontSize) {
        boxWidth = width - boxX + this.toRpx(8)
      }
      this.drawFill(boxX, boxY, boxWidth, boxHeight, textParams.background)
    }
    this[_ctx_].beginPath()
    this[_ctx_].setGlobalAlpha(opacity)
    this[_ctx_].setFontSize(fontSize)
    this[_ctx_].setFillStyle(color || '#000')
    this[_ctx_].setStrokeStyle(color || '#000')
    this[_ctx_].setTextBaseline(textBaseline)
    this[_ctx_].setTextAlign(textAlign)
    this[_ctx_].setLineWidth(this.toRpx(2))
    const drawOneLineText = () => {
      let textArr = text.split(''),
        newText = '',
        ellipsis = false
      try {
        textArr.map(e => {
          newText += e
          if (this.getTextWidth(newText) > width - fontX) throw true
        })
      } catch (error) {
        ellipsis = error
      }
      if (ellipsis) newText += '…'
      if (lastFont) newText += lastFont
      this[_ctx_].fillText(newText, x, y, maxWidth)
      if (checkFontWeight(fontWeight)) this[_ctx_].strokeText(newText, x, textHeight, maxWidth)
      if (checkTextDecoration(textDecoration)) {
        this.drawDecoration({
          textDecoration,
          textBaseline,
          textAlign,
          fontSize,
          newText,
          opacity,
          text,
          y,
          x
        })
      }
    }
    if (lineNum > 1) {
      let textArr = text.split(''),
        textRowArr = [],
        currentText = '',
        viewWidth = width - fontX
      if (text_width > viewWidth) {
        textArr.map(e => {
          currentText += e
          if (this.getTextWidth(currentText) >= viewWidth && textRowArr.length < lineNum - 1) {
            textRowArr.push(currentText)
            currentText = ''
          }
        })
        if (currentText.length) textRowArr.push(currentText)
        textRowArr.map((e, index) => {
          if (this.getTextWidth(e) > viewWidth && index == lineNum - 1) {
            try {
              let lastText = '',
                lastArr = e.split('')
              lastArr.map(i => {
                lastText += i
                if (this.getTextWidth(lastText) > width - fontX - this.getTextWidth('…')) throw lastText
              })
            } catch (error) {
              if (lastFont) e = `${error.substring(0, error.length - lastFont.length)}…${lastFont}`
              else e = `${error}…`
            }
          }
          if (lastFont && e.indexOf(lastFont) == -1 && textRowArr.length < 2) e = `${e.substring(0, e.length - lastFont.length)}…${lastFont}`
          let textHeight = y + index * (lineHeight || fontSize)
          this[_ctx_].fillText(e, x, textHeight, maxWidth)
          if (checkFontWeight(fontWeight)) this[_ctx_].strokeText(newText, x, textHeight, maxWidth)
          if (checkTextDecoration(textDecoration)) {
            this.drawDecoration({
              y: textHeight,
              textDecoration,
              textBaseline,
              textAlign,
              fontSize,
              newText: e,
              opacity,
              text,
              x
            })
          }
        })
      } else {
        drawOneLineText()
      }
    } else {
      drawOneLineText()
    }
    this[_ctx_].stroke()
  }
  drawLine(lineParams) {
    this[_ctx_].beginPath()
    let {
      opacity,
      startX,
      startY,
      width,
      color,
      endX,
      endY,
    } = lineParams
    startX = this.toRpx(startX)
    startY = this.toRpx(startY)
    width = this.toRpx(width)
    endX = this.toRpx(endX)
    endY = this.toRpx(endY)
    this[_ctx_].setGlobalAlpha(typeof opacity === 'number' ? opacity : 1)
    this[_ctx_].setStrokeStyle(color || '#000')
    this[_ctx_].setLineWidth(width || this.toRpx(1))
    this[_ctx_].moveTo(startX, startY)
    this[_ctx_].lineTo(endX, endY)
    this[_ctx_].stroke()
    this[_ctx_].restore()
    this[_ctx_].save()
  }
  setLineY = params => {
    let {
      textDecoration,
      textBaseline,
      fontSize,
      y
    } = params
    let type = {
      'underline': {
        'top': y + fontSize,
        'middle': y + fontSize / 2,
        'bottom': y,
        'normal': y,
      },
      'overline': {
        'top': y,
        'middle': y - fontSize / 2,
        'bottom': y - fontSize,
        'normal': y,
      },
      'line-through': {
        'top': y + fontSize / 2,
        'middle': y - fontSize / 2,
        'bottom': y,
        'normal': y,
      }
    }
    return type[textDecoration][textBaseline]
  }

  setLineX(params) {
    let {
      textAlign,
      text,
      x
    } = params
    let textWidth = this[_ctx_].measureText(text).width
    let type = {
      'left': x,
      'center': x - textWidth / 2,
      'right': x - textWidth
    }
    return type[textAlign]
  }

  drawRadiusRect(params) {
    let {
      borderRadius,
      height,
      width,
      x,
      y,
    } = params
    let radius = typeof borderRadius === 'string' ? (Number(borderRadius.split('%')[0]) / 100 * width) / 2 : borderRadius / 2
    radius = radius || 0
    this[_ctx_].beginPath()
    for (let i = 1; i < 5; i++) {
      let arcX = i === 1 || i === 4 ? x + width - radius : x + radius
      let arcY = i === 1 || i === 2 ? y + height - radius : y + radius
      this[_ctx_].arc(arcX, arcY, radius, 2 * Math.PI * ((i - 1) / 4), 2 * Math.PI * (i / 4))
    }
  }
  getFontWidth(params) {
    let {
      textAlign,
      fontSize,
      text,
      x
    } = params
    let type = {
      'left': x,
      'center': x - fontSize * text.length / 2,
      'right': x - fontSize * text.length
    }
    return type[textAlign] < 0 ? 0 : type[textAlign]
  }

  drawDecoration(params) {
    let {
      newText,
      opacity,
      y,
      x
    } = params
    let lineX = x
    let lineY = y
    lineX = this.setLineX(params)
    lineY = this.setLineY(params)
    this[_ctx_].setGlobalAlpha(opacity)
    this[_ctx_].moveTo(lineX, lineY)
    this[_ctx_].lineTo(lineX + this[_ctx_].measureText(newText).width, lineY)
  }
}
const checkFontWeight = (fontWeight = '0') => {
  return (fontWeight && (typeof fontWeight === 'string' || typeof fontWeight === 'number') && (fontWeight == 'bold' || fontWeight >= 700))
}
const checkTextDecoration = textDecoration => {
  return (textDecoration && (textDecoration === 'underline' || textDecoration === 'overline' || textDecoration === 'line-through'))
}
export default Draw_Function