drawing.js
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* @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