draw.js 2.86 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-05-19 13:46:20
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-05-31 18:11:57
 * @FilePath: \scrm-micro-home\src\mixins\gpt.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { mapState, mapActions } from 'vuex'

import Drawing from '@/utils/drawing'
export default {
  data() {
    return {}
  },
  methods: {
    //主图、内容图、logo合并图片
    async createCanvas() {
      this.loading = true
      const { backgroundImage, detailImage, merchantLog, marketingTitle, marketingSubtitle } = this.editForm
      if (!backgroundImage) return
      try {
        const res = await Promise.all([this.getImgWH(backgroundImage), this.getImgWH(detailImage), this.getImgWH(merchantLog)])
        const h = (res[0].dh / res[0].dw) * 375 + (res[1].dh / res[1].dw) * 375
        this.logoInfo = { url: merchantLog, x: 375 - res[2].dw / 2 - 20, y: 20, width: res[2].dw / 2, height: res[2].dh / 2 }
        const drawfun = new Drawing(document.getElementById('canvasId'), {
            width: 375,
            height: h
          }),
          imageList = [
            {
              url: backgroundImage,
              x: 0,
              y: 0,
              width: 375,
              height: (res[0].dh / res[0].dw) * 375
            },
            {
              url: detailImage,
              x: 0,
              y: (res[0].dh / res[0].dw) * 375,
              width: 375,
              height: (res[1].dh / res[1].dw) * 375
            },
            { ...this.logoInfo }
          ].filter(item => item.url)
        let textList = [],
          title = this.cavansObj[this.editForm.templateId]
        if (title) {
          marketingTitle.split(' ').forEach((item, index) => {
            textList.push({
              x: title.titleStyle.x,
              y: title.titleStyle.y + index * title.titleStyle.fontSize * 1.14,
              fontSize: title.titleStyle.fontSize,
              color: title.titleStyle.color,
              text: item,
              bold: title.titleStyle.bold
            })
          })
          new Array(marketingSubtitle).forEach((item, index) => {
            textList.push({
              x: title.subTitleStyle.x,
              y: title.subTitleStyle.y + title.titleStyle.fontSize * 1.14 * (textList.length - 1),
              fontSize: title.subTitleStyle.fontSize,
              color: title.subTitleStyle.color,
              text: item
            })
          })
        }
        console.log(title, textList)
        const canvasBase64 = await drawfun.begin({
          imageList,
          textList
        })
        await this.uploadImg(canvasBase64)
        this.addOrUpdate()
      } catch (e) {
        console.log(e)
      }
    }
  },
  mounted() {}
}