使用 auto-drawing 绘制商城分销海报

auto-drawing 官方文档

简介

新年新气象,使用 auto-drawing 绘制一份绘制商城分销海报。

安装

基于 vue 环境

1
yarn add  auto-drawing

代码

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<template>
<div id="sharePost" class="canvas-wrapper"></div>
<div class="btn">
<el-button type="primary" @click="download">下载海报</el-button>
</div>
</template>

<script setup>
import FileSaver from 'file-saver'
import { onMounted, reactive } from 'vue'
import {
createCanvas,
createGroup,
renderCanvas,
canvasToImage
} from 'auto-drawing'
import { ElMessage } from 'element-plus'

const state = reactive({
zr: null,
gp: null
})

onMounted(() => {
const width = 375
const height = 592
state.zr = createCanvas('sharePost', {
width,
height
})
state.zr.setBackgroundColor('#ff6e0b')
state.gp = createGroup()
const data = [
{
type: 'image',
x: 0,
y: 0,
width: 375,
height: 592,
image: '/auto-drawing-doc/post.png'
},
{
type: 'image',
x: 40,
y: 20,
width: 50,
height: 50,
image: '/auto-drawing-doc/avatar.png',
zlevel: 1
},
{
type: 'text',
x: 98,
y: 24,
text: 'xiaofei的店铺',
fontSize: 16,
fill: '#fff'
},
{
type: 'text',
x: 98,
y: 50,
text: '邀请你共享优惠',
fontSize: 12,
fill: '#ffd3a2'
},
{
type: 'text',
x: 50,
y: 400,
text: '¥99.9',
fontSize: 32,
fill: '#f00'
},
{
type: 'text',
x: 150,
y: 410,
text: '¥1999.9',
fontSize: 12,
fill: '#999'
},
{
type: 'line',
x1: 158,
y1: 414,
x2: 200,
y2: 414,
stroke: '#999'
},
{
type: 'text',
x: 60,
y: 440,
text: '自营',
fontSize: 12,
backgroundColor: '#fa4f00',
padding: 2,
borderRadius: 5
},
{
type: 'text',
x: 96,
y: 440,
text: '30天最低价',
fontSize: 12,
fill: '#805609',
backgroundColor: '#faf5d9',
padding: 2
},
{
type: 'text',
x: 168,
y: 440,
text: '包邮',
fontSize: 12,
fill: '#805609',
backgroundColor: '#faf5d9',
padding: 2
},
{
type: 'text',
x: 200,
y: 440,
text: '满减优惠',
fontSize: 12,
fill: '#805609',
backgroundColor: '#faf5d9',
padding: 2
},
{
type: 'text',
x: 55,
y: 480,
text: '精美兔子毛绒',
fontSize: 24,
fill: '#000'
},
{
type: 'text',
x: 55,
y: 510,
text: '玩具,回家必备。',
fontSize: 24,
fill: '#000'
},
{
type: 'image',
x: 250,
y: 472,
width: 70,
height: 70,
image: '/auto-drawing-doc/code.jpg'
},
{
type: 'text',
x: 320,
y: 210,
text: '兔 年 快 乐',
fontSize: 20,
fill: '#fa4f00',
rotation: -90,
originX: 320,
originY: 210
}
]

renderCanvas(state.zr, state.gp, data)
})

// 下载
const download = async () => {
try {
const { blob, base64 } = await canvasToImage(state.zr)
console.log('blob:', blob)
console.log('base64:', base64)
FileSaver.saveAs(blob, 'post.png')
ElMessage.success('下载成功')
} catch (error) {
ElMessage.error('下载失败')
}
}
</script>

<style>
.btn {
margin: 10px;
text-align: left;
}
</style>

效果

https://l-x-f.github.io/auto-drawing-doc/example/example.html#%E5%88%86%E9%94%80%E6%B5%B7%E6%8A%A5

官方文档

官方文档 https://l-x-f.github.io/auto-drawing-doc/