前端合并图片
... 2025-7-10 小于 1 分钟
# 基于 Canvas 实现的图片合并
// Vue data里的数据
type: "",
sub: 0,
noticeList:[
{
name: "图片名字",
images: [require("@/assets/notice/cdzbz01.png")]
}
]
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
// 图片合并 图片合成一张图
imagesMerge(size) {
let canvas = document.createElement('canvas')
canvas.width = size[0].width
canvas.height = size[0].height * size.length
let context = canvas.getContext('2d')
context.rect(0, 0, canvas.width, canvas.height)
context.fillStyle = '#fff'
context.fill()
let height = 0
size.forEach(item => {
context.drawImage(item.img, 0, height, item.width, item.height)
height += item.height
})
return canvas.toDataURL('image/png')
},
// 获取图片 以及 图片的实际大小
getImg(imgs) {
return new Promise(resolve => {
let arr = []
imgs.forEach((item, index) => {
let image = new Image()
image.src = item
image.onload = function () {
arr.push({ id: index, width: image.width, height: image.height, img: image })
if (arr.length == imgs.length) {
arr.sort((a, b) => a.id - b.id)
resolve(arr)
}
}
})
})
}
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
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