index.vue
4.49 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
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
191
192
193
194
195
196
197
198
199
<template>
<view class="index">
<view class="action-bar">
<button @tap="start">生成海报</button>
<button @tap="savePoster">下载海报</button>
</view>
<view class="preview-area">
<image v-if="posterPath" :src="posterPath" mode="widthFix" />
<view v-else class="text">预览区域</view>
</view>
<PosterBuilder v-if="startDraw" custom-style="position: fixed; left: 200%;" :config="base" @success="drawSuccess"
@fail="drawFail" />
</view>
</template>
<script>
import Taro from '@tarojs/taro'
import { defineComponent, reactive, toRefs } from 'vue';
import PosterBuilder from '@/components/PosterBuilder/index.vue';
export default defineComponent({
name: 'Index',
components: {
PosterBuilder,
},
setup() {
const state = reactive({
startDraw: false,
posterPath: ''
})
const base = {
width: 750,
height: 1334,
backgroundColor: '#232422',
debug: false,
blocks: [
// 头部底色
{
x: 32,
y: 80,
width: 686,
height: 160,
paddingLeft: 0,
paddingRight: 0,
backgroundColor: '#FFFFFF',
borderRadius: 32,
zIndex: 10
},
//底部背景
{
x: 32,
y: 950,
width: 686,
height: 302,
paddingLeft: 0,
paddingRight: 0,
borderRadiusGroup: [0, 0, 16, 16],
backgroundColor: '#FFFFFF',
zIndex: 11
}
],
texts: [
{
x: 216,
y: 108,
text: 'BiBin',
width: 380,
lineNum: 2, // 最多几行
fontSize: 36,
fontWeight: 'bold',
color: '#1A171B',
zIndex: 11
},
{
x: 216,
y: 174,
text: '为你挑选了一个好物',
width: 380,
fontSize: 28,
color: '#7C7D7A',
zIndex: 11
},
{
x: 64,
y: 994,
text: `¥6799`,
fontSize: 48,
color: '#ED2D2B',
fontWeight: 'bold',
zIndex: 12
},
{
x: 64,
y: 1092,
text: 'Apple iPhone 13 (A2634) 256GB 蓝色 支持移动联通电信5G 双卡双待手机',
fontSize: 32,
width: 380,
color: '#282925',
lineNum: 2, // 最多几行
zIndex: 12
}
],
images: [
{
x: 64,
y: 100,
width: 120,
height: 120,
borderRadius: 60,
url: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJP05RJ5icJkUkBjVtSb3DHib4pGRVEqjw3qNic53kd1tJmibPpzR1etJnWJFiaIJDLK6TDzD3d5SyPsXQ/132',
zIndex: 11
},
{
x: 32,
y: 272,
width: 686,
height: 686,
url: 'https://m.360buyimg.com/mobilecms/s750x750_jfs/t1/119360/32/20820/239818/618be3c6E1dbf188d/4070880e024273bb.jpg!q80.dpg',
borderRadiusGroup: [16, 16, 0, 0],
zIndex: 11
}
]
};
const start = () => {
state.startDraw = true;
Taro.showLoading();
}
const drawSuccess = (result) => {
console.warn('绘制好了', result);
const { tempFilePath, errMsg } = result;
if (errMsg === 'canvasToTempFilePath:ok') {
state.posterPath = tempFilePath;
Taro.hideLoading();
} else {
Taro.hideLoading();
Taro.showToast({
title: '失败,请稍后重试',
icon: 'none',
duration: 2500
});
}
};
const drawFail = (result) => {
console.warn('绘制失败', result);
Taro.hideLoading();
}
const savePoster = () => {
Taro.saveImageToPhotosAlbum({
filePath: state.posterPath,
success() {
Taro.showToast({
title: '已保存到相册',
icon: 'success',
duration: 2000
});
}
});
}
return {
...toRefs(state),
base,
start,
drawSuccess,
drawFail,
savePoster
}
}
});
</script>
<style>
.index {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
.action-bar {
display: flex;
}
.preview-area {
width: 80%;
min-height: 800px;
margin: 20px auto;
text-align: center;
border: 1px solid #cccccc;
}
.text {
line-height: 800px;
}
</style>