uniapp生成高清海报图canvas
本期先讲uniapp生成,下期讲本插件怼到原生小程序中,编辑不易,给个赞吧!!!!!
首先将三个外部文件放到你的公共文件夹下

然后在海报生成的页面内引入这个两个文件

data上设置变量

html代码块

canvas-id设置与data的canvasId一致
代码块
废话不多说直接贴代码
html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!-- 海报生成 --> <view> <!-- 图片展示由自己实现 --> <view class="flex_row_c_c modalView" :class="qrShow?'show':''" @tap="hideQr()"> <view class="flex_column"> <view class="backgroundColor-white border_radius_10px"> <image :src="poster.finalPath || ''" mode="widthFix" class="posterImage"></image> </view> <view class="marginTop2vh"> <button class="savaImg" type="primary" @tap.prevent.stop="saveImage()"> 保存图片</button> <button class="closeImg" @tap.prevent.stop="hideQr()">关闭</button> </view> </view> </view> <!-- 画布 --> <view class="hideCanvasView"> <canvas class="hideCanvas" canvas-id="default_PosterCanvasId" :style="{width: (poster.width||10) + 'px', height: (poster.height||10) + 'px'}"></canvas> </view> </view> |
css代码
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 | // 海报生成样式 .creatPosterBtn { position: absolute; top: 30rpx; right: 40rpx; background: rgba(0, 0, 0, 0.5) !important; color: #FFFFFF; border-radius: 2px; z-index: 10; font-size: 15px; padding: 5px 10px; line-height: 40rpx; } .hideCanvasView { position: relative; } .hideCanvas { position: fixed; top: -99999upx; left: -99999upx; z-index: -99999; } .flex_row_c_c { display: flex; flex-direction: row; justify-content: center; align-items: center; } .modalView { width: 100%; height: 100%; position: fixed; top: 0; left: 0; right: 0; bottom: 0; opacity: 0; outline: 0; transform: scale(1.2); perspective: 2500upx; background: rgba(0, 0, 0, 0.6); transition: all .3s ease-in-out; pointer-events: none; backface-visibility: hidden; z-index: 999; } .modalView.show { opacity: 1; transform: scale(1); pointer-events: auto; } .flex_column { display: flex; flex-direction: column; } .backgroundColor-white { background-color: white; } .backgroundColor-white image { border-radius: 0px; } .border_radius_10px { border-radius: 0px; } .padding1vh { padding: 1vh; } .posterImage { width: 80vw; } .flex_row { display: flex; flex-direction: row; } .marginTop2vh { margin-top: 2vh; } .savaImg { width: 100%; padding: 16rpx 0; margin-top: 20rpx; font-size: 28rpx; line-height: 50rpx; display: block; } .closeImg { width: 100%; padding: 16rpx 0; margin-top: 20rpx; font-size: 28rpx; line-height: 50rpx; background: #FFFFFF; color: #333333; display: block; } |
最重要的js模块
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | import _app from '@/utils/QS-SharePoster/app.js'; import {getSharePoster} from '@/utils/QS-SharePoster/QS-SharePoster.js'; export default { data() { return { QrSrc: '', wmVisible: false, poster: {}, qrShow: false, canvasId: 'default_PosterCanvasId', count: 0, } }, onLoad(options) { // 海报二维码进入场景 if (options.scene) { // 此方法为获取你的商品详情方法,此处省略,主要是扫码进来会拿scene,例如你的商品id this.getdetail(options.scene); } }, methods: { // 生成海报 creatPoster: function() { this.wmVisible = true }, // 关闭海报 closePoster: function() { this.wmVisible = false }, // 海报生成相关js开始 async shareFc() { let _this = this; try { this.count++; _app.log('准备生成:' + new Date()) const d = await getSharePoster({ _this: _this, //若在组件中使用 必传 type: 'testShareType', formData: { //访问接口获取背景图携带自定义数据 }, posterCanvasId: _this.canvasId, //canvasId delayTimeScale: 20, //延时系数 background: { width: 1080, height: 1920, }, setCanvasWH({ bgObj }) { _this.poster = bgObj }, drawArray({ bgObj, type, bgScale, setBgObj, getBgObj }) { return [{ type: 'image', id: 'productImage', // url: _this.count % 2 === 0 ? '/static/1.png' : '/static/2.jpg', url: _this.swiperList[0], dx: getBgObj().width * .05 - 20, dy: getBgObj().width * .05 - 20, serialNum: 0, infoCallBack(imageInfo) { let width; let height; if (imageInfo.width > imageInfo.height) { width = imageInfo.width > 700 ? 700 : imageInfo.width; height = width / imageInfo.width * imageInfo.height; } else { height = imageInfo.height > 700 ? 700 : imageInfo.height; width = height / imageInfo.height * imageInfo.width; } if (width < 500) { width = 500; height = width / imageInfo.width * imageInfo.height; } let addHeight = height * .3; if (addHeight < 150) addHeight = 150; if (addHeight > 250) addHeight = 250; setBgObj({ width, height: height + addHeight + 280, backgroundColor: '#ffffff' }); return { dWidth: width - 70, dHeight: height } } }, { type: 'text', id: 'productName', text: _this.vehicleInfo.name, color: '#000', serialNum: 1, allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { size: getBgObj().width * 0.04, lineFeed: { maxWidth: getBgObj().width * 0.9, lineNum: 2 }, dx: getBgObj().width * .05, dy: productImage.dHeight + addHeight * .2, } } }, { type: 'text', text: '车辆售价:10万', color: '#ff5500', serialNum: 1, allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { size: getBgObj().width * 0.04, lineFeed: { maxWidth: getBgObj().width, lineNum: 1 }, dx: getBgObj().width * .05, dy: productImage.dHeight + addHeight * .38, } } }, { type: 'text', text: '新车售价:20万', color: '#000', serialNum: 1, allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { size: getBgObj().width * 0.04, lineFeed: { maxWidth: getBgObj().width, lineNum: 1 }, dx: getBgObj().width * .05, dy: productImage.dHeight + addHeight * .48, } } }, { type: 'text', text: '车辆里程:5万公里', color: '#000', serialNum: 3, allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { size: getBgObj().width * 0.04, lineFeed: { maxWidth: getBgObj().width * 0.5, lineNum: 1 }, dx: getBgObj().width * .05, dy: productImage.dHeight + addHeight * .58, } } }, { type: 'text', text: '排放标准:国五' , color: '#000', serialNum: 4, allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { size: getBgObj().width * 0.04, lineFeed: { maxWidth: getBgObj().width * 0.5, lineNum: 1 }, dx: getBgObj().width * .05, dy: productImage.dHeight + addHeight * .68, } } }, { type: 'text', text: '牌照地点:北京', color: '#000', serialNum: 5, allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { size: getBgObj().width * 0.04, lineFeed: { maxWidth: getBgObj().width * 0.5, lineNum: 1 }, dx: getBgObj().width * .05, dy: productImage.dHeight + addHeight * .78, } } }, { type: 'text', text: '上牌时间:2019年1月1日' , serialNum: 6, allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { size: getBgObj().width * 0.04, lineFeed: { maxWidth: getBgObj().width * 0.5, lineNum: 1 }, dx: getBgObj().width * .05, dy: productImage.dHeight + addHeight * .88, } } }, // 此处放的为商品的太阳码,QrSrc为太阳码地址或者远程图片地址 { type: 'image', id: 'smallImage', url: _this.QrSrc, dx: getBgObj().width * .05, dy: 600, serialNum: 7, serialNum: 6, allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { dWidth: 150, dHeight: 150, dx: getBgObj().width * .65, dy: productImage.dHeight + addHeight * .32, } } }, { type: 'text', text: '识别小程序', serialNum: 8, color: '#000', allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { size: getBgObj().width * 0.04, lineFeed: { maxWidth: getBgObj().width * 0.5, lineNum: 1 }, dx: getBgObj().width * .65, dy: productImage.dHeight + addHeight * .78, } } }, { type: 'text', text: '进入二手车商城', serialNum: 9, color: '#000', allInfoCallback({ drawArray }) { const productImage = drawArray.find(item => item.id === 'productImage') const addHeight = getBgObj().height - productImage.dHeight; return { size: getBgObj().width * 0.04, lineFeed: { maxWidth: getBgObj().width * 0.5, lineNum: 1 }, dx: getBgObj().width * .62, dy: productImage.dHeight + addHeight * .88, } } }, ] } }) _app.log('海报生成成功, 时间:' + new Date() + ', 临时路径: ' + d.poster.tempFilePath) _this.poster.finalPath = d.poster.tempFilePath; _this.qrShow = true; } catch (e) { _app.hideLoading(); _app.showToast(JSON.stringify(e)); } }, saveImage() { let that = this; wx.getSetting({ success(res) { console.log(res) console.log(that.poster.finalPath) // 如果没有则获取授权 if (!res.authSetting['scope.writePhotosAlbum']) { wx.authorize({ scope: 'scope.writePhotosAlbum', success() { wx.saveImageToPhotosAlbum({ filePath: that.poster.finalPath, success() { wx.showToast({ title: '保存成功' }) }, fail() { wx.showToast({ title: '保存失败', icon: 'none' }) } }) }, fail() { wx.showModal({ title: '提示:', content: "授权失败,请前往设置中心设置权限", confirmText: '前往授权', success: function() { wx.openSetting({ success(res) { // res.authSetting = { // "scope.userInfo": true, // "scope.userLocation": true // } } }) } }) } }) } else { // 有则直接保存 wx.saveImageToPhotosAlbum({ filePath: that.poster.finalPath, success() { wx.showToast({ title: '保存成功' }) }, fail() { wx.showToast({ title: '保存失败', icon: 'none' }) } }) } } }) // // #ifndef H5 // uni.saveImageToPhotosAlbum({ // filePath: that.poster.finalPath, // success(res) { // _app.showToast('保存成功'); // } // }) // // #endif // // #ifdef H5 // _app.showToast('保存了'); // // #endif }, share() { // #ifdef APP-PLUS _app.getShare(false, false, 2, '', '', '', this.poster.finalPath, false, false); // #endif // #ifndef APP-PLUS _app.showToast('分享了'); // #endif }, hideQr() { this.qrShow = false; }, // 海报生成相关js结束 } } |
生成结果

原插件地址:https://ext.dcloud.net.cn/plugin?id=471
编辑不易,给个赞吧!!!!