uniapp - 绘制海报(二)
1 // 绘制海报 2 drawPoster() { 3 uni.showLoading({ 4 title:'加载中...', 5 mask:true 6 }) 7 let _this = this 8 _this.isShow = true 9 let info = uni.getSystemInfoSync() 10 const pixelRatio = info.pixelRatio // 屏幕像素密度 11 uni.getImageInfo({ 12 src: _this.detail.avatarUrl || _this.userInfo.avatarUrl, 13 success(image) { 14 const canvasId = "guideQYCard" 15 let ctx = uni.createCanvasContext(canvasId,_this) 16 ctx.save(); // 先保存状态 已便于画完圆再用 17 ctx.beginPath(); //开始绘制 18 ctx.setFillStyle('white') 19 ctx.fillRect(0, 0, uni.upx2px(_this.cardWidth), uni.upx2px(_this.cardHeight)) 20 ctx.restore(); 21 let ratio = _this.cardWidth/image.width 22 let draw_h = image.height*ratio 23 // _this.cardHeight = draw_h 24 ctx.drawImage(image.path,0,0,uni.upx2px(_this.cardWidth),uni.upx2px(draw_h)) // 海报 25 // 绘制 昵称 26 ctx.save(); // 先保存状态 已便于画完圆再用 27 const name_size = 28 28 const name_x = 24 29 const name_y = 511 30 ctx.setFillStyle('#FFFFFF') 31 ctx.setTextBaseline('top') 32 ctx.setFontSize(uni.upx2px(name_size)) 33 ctx.fillText(_this.detail.name, uni.upx2px(name_x), uni.upx2px(name_y)) 34 const position_size = 24 35 const position_x = 24 36 const position_y = 554 37 ctx.setFontSize(uni.upx2px(position_size)) 38 ctx.fillText(_this.detail.position, uni.upx2px(position_x), uni.upx2px(position_y)) 39 // 绘制 小程序码 40 uni.getImageInfo({ 41 src: _this.detail.qrCode, 42 success(res){ 43 const size = 180 44 const x = 40 45 const y = 35 46 const img_x = _this.cardWidth - size - x 47 const img_y = _this.cardHeight - size - y 48 const img_w = size 49 const img_h = img_w 50 ctx.drawImage(res.path,uni.upx2px(img_x),uni.upx2px(img_y),uni.upx2px(img_w),uni.upx2px(img_h)) // 头像 51 ctx.draw(false, () => { 52 uni.canvasToTempFilePath({ 53 width: _this.cardWidth, 54 height: _this.cardHeight, 55 destWidth: _this.cardWidth * pixelRatio, 56 destHeight: _this.cardHeight *pixelRatio, 57 canvasId: canvasId, 58 fileType: 'png', 59 quality:1, 60 success: function(res) { 61 _this.posterImg = res.tempFilePath 62 _this.showCloseBtn = true 63 console.log(_this.posterImg) 64 uni.hideLoading() 65 }, 66 fail(error) { 67 console.log('4',error) 68 _this.isShow = false 69 uni.showLoading({ 70 title:'加载失败,请稍后重试', 71 mask:true 72 }) 73 uni.hideLoading() 74 } 75 },_this) 76 }) 77 }, 78 fail(error) { 79 console.log('3',error) 80 uni.showLoading({ 81 title:'加载失败,请稍后重试', 82 mask:true 83 }) 84 _this.isShow = false 85 uni.hideLoading() 86 } 87 }) 88 }, 89 fail(error) { 90 console.log('1',error) 91 _this.isShow = false 92 uni.showLoading({ 93 title:'加载失败,请稍后重试', 94 mask:true 95 }) 96 uni.hideLoading() 97 } 98 }); 99 }
这里需要注意的是 白色背景需要绘制,否则 用户下载下来 背景是黑色的。然而,在 html5 中canvas 背景色可以在css 中设置。
作者:胡倩倩0903
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
分类:
uniapp
标签:
uniapp
, canvas 绘制海报
posted on 2020-09-27 15:42 kitty20180903suzhou 阅读(466) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY