drawing.js 1.28 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-05-17 16:07:55
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-05-18 09:58:54
 * @FilePath: \scrm-micro-home\src\utils\drawing.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
class Drawing {
  constructor(canvasParams, list = [], fn) {
    this.count = 0
    this.fn = fn
    this.list = list
    this.canvasId = document.createElement('canvas')
    const { width, height } = canvasParams
    this.canvasId.width = width
    this.canvasId.height = height
    this.ctx = this.canvasId.getContext('2d')
  }
  drawImages(fn) {
    const { imgSrc, x, y, width, height } = this.list[this.count]
    let image = this.createImage()
    image.src = imgSrc
    this.count++
    image.onload = () => {
      this.ctx.drawImage(image, x, y, width, height)
      console.log(this.count, this.list.length)
      if (this.count === this.list.length) {
        this.fn(this.canvasId.toDataURL('image/png'))
      } else {
        this.drawImages()
      }
    }
  }
  createImage() {
    let image = new Image()
    image.setAttribute('crossOrigin', 'Anonymous')
    return image
  }
}
export default Drawing