uniapp - 绘制海报(一)
1 // 绘制海报 2 drawPoster() { 3 uni.showLoading({ 4 title:"海报生成中...", 5 mask:true 6 }) 7 let _this = this 8 _this.isShow = true // 控制canvas 显示,必须显示的状态下,才可绘制 9 let info = uni.getSystemInfoSync() 10 const pixelRatio = info.pixelRatio // 屏幕像素密度 11 uni.getImageInfo({ 12 src: _this.poster, //_this_pupop.image 13 success(image) { 14 const canvasId = "guidePopop" 15 let ctx = uni.createCanvasContext(canvasId,_this) // 自定义组件中 一定要传入 this ,这里一开始没加,困惑很久,一定要写一下 16 let ratio = _this.pupopWidth/image.width 17 let draw_h = image.height*ratio 18 _this.pupopHeight = draw_h 19 ctx.drawImage(image.path,0,0,uni.upx2px(_this.pupopWidth),uni.upx2px(draw_h)) // 海报背景图。 unp2px 用户进行像素转换。 20 // 绘制头像 21 uni.getImageInfo({ 22 src: _this.userInfo.avatarUrl, 23 success(res){ 24 const size = 76 25 const x = 36 26 const y = 36 27 const acr_r = size/2 28 const acr_x = x+acr_r 29 const acr_y = y+acr_r 30 const img_x = acr_x/2 31 const img_y = acr_y/2 32 const img_w = size 33 const img_h = img_w 34 ctx.save(); // 先保存状态 已便于画完圆再用 35 ctx.beginPath(); //开始绘制 36 //先画个圆 37 ctx.arc(uni.upx2px(acr_x), uni.upx2px(acr_y),uni.upx2px(acr_r), 0, Math.PI * 2); 38 ctx.setFillStyle('#ffffff') 39 ctx.fill()//保证图片无bug填充 40 ctx.clip();//画了圆 再剪切 原始画布中剪切任意形状和尺寸。一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内 41 ctx.drawImage(res.path,uni.upx2px(img_x),uni.upx2px(img_y),uni.upx2px(img_w),uni.upx2px(img_h)) // 头像 42 ctx.restore(); 43 // 绘制 昵称 44 ctx.save(); // 先保存状态 已便于画完圆再用 45 const text_size = 28 46 const text_x = x + img_w + 29 47 const text_y = 64 48 ctx.setFillStyle('#FFFFFF') 49 ctx.setTextBaseline('top') 50 ctx.setFontSize(uni.upx2px(text_size)) 51 ctx.fillText('Hi '+_this.userInfo.nickName, uni.upx2px(text_x), uni.upx2px(text_y)) 52 // 绘制 小程序码 53 uni.getImageInfo({ 54 src: _this.inviteQR, 55 success(res){ 56 const size = 160 57 const img_x = _this.pupopWidth - size - x 58 const img_y = _this.pupopHeight - size - y 59 const img_w = size 60 const img_h = img_w 61 ctx.drawImage(res.path,uni.upx2px(img_x),uni.upx2px(img_y),uni.upx2px(img_w),uni.upx2px(img_h)) // 头像 62 ctx.draw(false, () => { 63 uni.canvasToTempFilePath({ 64 width: _this.pupopWidth, 65 height: draw_h, 66 destWidth: _this.pupopWidth * pixelRatio, 67 destHeight: draw_h *pixelRatio, 68 canvasId: canvasId, 69 fileType: 'png', 70 quality:1, 71 success: function(res) { 72 _this.posterImg = res.tempFilePath 73 _this.showCloseBtn = true 74 console.log(_this.posterImg) 75 uni.hideLoading() 76 }, 77 fail(error) { 78 console.log('4',error) 79 _this.isShow = false 80 uni.showLoading({ 81 title:'生成海报失败,请稍后重试', 82 mask:true 83 }) 84 uni.hideLoading() 85 } 86 },_this) 87 }) 88 }, 89 fail(error) { 90 console.log('3',error) 91 uni.showLoading({ 92 title:'生成海报失败,请稍后重试', 93 mask:true 94 }) 95 _this.isShow = false 96 uni.hideLoading() 97 } 98 }) 99 }, 100 fail(error) { 101 console.log('2',error) 102 uni.showLoading({ 103 title:'生成海报失败,请稍后重试', 104 mask:true 105 }) 106 _this.isShow = false 107 uni.hideLoading() 108 } 109 }) 110 }, 111 fail(error) { 112 console.log('1',error) 113 _this.isShow = false 114 uni.showLoading({ 115 title:'生成海报失败,请稍后重试', 116 mask:true 117 }) 118 uni.hideLoading() 119 } 120 }); 121 },
122 savePhoto(filePath){ // 保存海报 123 124 checkForAuthorization('scope.writePhotosAlbum').then(()=>{ // 上一篇有提到 125 uni.saveImageToPhotosAlbum({ 126 filePath:filePath, 127 success: function () { 128 uni.hideLoading() 129 }, 130 fail: function(e){ 131 uni.hideLoading() 132 if(e.errMsg == "saveImageToPhotosAlbum:fail cancel"){ 133 uni.showToast({ 134 title:"已取消", 135 icon:"none" 136 }) 137 }else{ 138 uni.showToast({ 139 title:"保存失败", 140 icon:"none" 141 }) 142 } 143 } 144 }) 145 }).catch((err)=>{ 146 console.log(err.errMsg) 147 uni.showToast({ 148 title:"保存失败", 149 icon:"none" 150 }) 151 }) 152 },
这里只展示了主要的绘制代码,绘制的海报图如下图,绘制内容已标记。背景 + 用户头像 + 用户昵称 + 小程序码 ,
这个弹窗是自定义组件,
uni.createCanvasContext(canvasId,_this) // 自定义组件中 一定要传入 this ,这里一开始没加,困惑很久,一定要写一下
uni.draw(boolean,()=>{},_this) // 自定义组件中 一定要传入 this ,这里一开始没加,困惑很久,一定要写一下
作者:胡倩倩0903
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
分类:
uniapp
标签:
canvas 绘制海报
, uniapp
posted on 2020-09-27 15:31 kitty20180903suzhou 阅读(1628) 评论(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