1.1.点击分享弹出一个动画出现分享类型2.canvas绘制文字和图片到画布3.给用户展示画布,画布转成图片4.当用户点击保存卡片时,将图片保存到相册wxml:
viewviewanimation="{{animationData}}"class="buydes-dialog-container"wx:if="{{showModalStatus}}"viewclass="buydes-dialog-container-top"bindtap="hideModal"canvascanvas-id="shareImg"/canvas/viewviewclass="buydes-dialog-container-bottom"viewclass="flbottom-image-fen"buttonopen-type="share"hover-class="other-button-hover"class="button-bg"imagesrc="../../img/weixin.png"style="width:80rpx;height:80rpx;"class="bottom-img"/imageviewclass="image-txt"发送好友/view/button/viewviewclass="flbottom-image-fen"viewstyle="margin-top:5rpx;"bindtap="saveShareImg"imagesrc="../../img/down.png"style="width:60rpx;height:60rpx;"class="bottom-img-right"/imageviewclass="image-txt"保存卡片/view/view/viewviewclass="clear"/viewviewbindtap="hideModal"class="buydes-dialog-container-bottom-item"取消/view/view/viewimagebindtap="showModal"bindload="imageLoad"style="width:200px;"src="../../img/xingli.png"//view2.js:
constconfig=require("../../utils/config.js");letanimationShowHeight=300;Page({data:{animationData:"",showModalStatus:false,imageHeight:0,imageWidth:0,shareImgHidden:false,shareImgSrc:'',//画布转成图片的临时地址},/***//显示遮罩层*/showModal:function(){letthat=this;letanimation=wx.createAnimation({duration:200,timingFunction:"linear",});that.animation=animation;animation.translateY(animationShowHeight).step();that.setData({animationData:animation.export(),showModalStatus:true});setTimeout(function(){animation.translateY(0).step();that.setData({animationData:animation.export()})}.bind(that),200);//canvas绘制文字和图片constctx=wx.createCanvasContext('shareImg');letbgImg='../../img/car.png';letheadImg='../../img/headImg.png';letxcxImg='../../img/weixin2.png';letImgHeight=250;letImgWidth=250;//首图位置ctx.drawImage(bgImg,53,10,ImgWidth,ImgHeight);ctx.setFillStyle('white');ctx.fillRect(30,10,23,ImgHeight);ctx.setFillStyle('white');ctx.fillRect(303,10,23,ImgHeight);ctx.setFillStyle('white');ctx.fillRect(30,ImgHeight+10,296,180);//头像位置ctx.setFillStyle('#ececec');ctx.fillRect(42,ImgHeight-10,60,60);ctx.setFillStyle('white');ctx.fillRect(40,ImgHeight-12,60,60);ctx.drawImage(headImg,45,ImgHeight-7,50,50);//昵称ctx.setFontSize(14);ctx.setFillStyle('#888888');ctx.fillText('昵称',110,ImgHeight+25);ctx.setFontSize(14);ctx.setFillStyle(config.fontColor);ctx.fillText('“快来跟我一起抢”',105,ImgHeight+45);ctx.setFontSize(15);ctx.setFillStyle('#333333');letstr='RumbaTime时尚手表潮流时装女表电子钢带石英表美国正品进口表';letheight=that.drawText(ctx,str,45,ImgHeight+70,30,265);//调用行文本换行函数ctx.setFontSize(13);ctx.setFillStyle(config.fontColor);ctx.fillText('¥',45,ImgHeight+65+height);ctx.setFontSize(20);ctx.setFillStyle(config.fontColor);ctx.fillText('1999.00',60,ImgHeight+65+height);ctx.setFillStyle('#999999');ctx.setFontSize(12);ctx.fillText('¥',140,ImgHeight+65+height);ctx.setFontSize(16);ctx.fillText('1999.00',150,ImgHeight+65+height);ctx.moveTo(140,ImgHeight+60+height);ctx.lineTo(210,ImgHeight+60+height);ctx.stroke('#999999');ctx.setFontSize(14);ctx.setFillStyle('#888888');ctx.fillText('长按识别小程序码访问',45,ImgHeight+85+height);//二维码ctx.drawImage(xcxImg,240,ImgHeight+height+30,70,70);ctx.draw();//canvas画布转成图片setTimeout(function(){wx.canvasToTempFilePath({x:30,y:10,width:300,height:430,destWidth:900,//生成图片的大小设置成canvas大小的3倍destHeight:1290,canvasId:'shareImg',success:function(res){that.setData({shareImgSrc:res.tempFilePath});},fail:function(res){wx.showToast({title:res.errMsg,icon:'none',duration:1000,});}});},500);that.setData({shareImgHidden:true,});},/***保存到相册*/saveShareImg:function(){letthat=this;console.log(that.data.shareImgSrc);setTimeout(function(){wx.saveImageToPhotosAlbum({filePath:that.data.shareImgSrc,success:function(res){},fail:function(error){console.log(error.errMsg);wx.showModal({title:'提示',content:'请允许保存图片到相册',});},});},500);},/***绘制多行文本*@paramctxcanvas对象*@paramstr文本*@paramleftWidth距离左侧的距离*@paraminitHeight距离顶部的距离*@paramtitleHeight文本的高度*@paramcanvasWidth文本的宽度*@returns{*}*/drawText:function(ctx,str,leftWidth,initHeight,titleHeight,canvasWidth){letlineWidth=0;letlastSubStrIndex=0;//每次开始截取的字符串的索引for(leti=0;istr.length;i++){lineWidth+=ctx.measureText(str[i]).width;if(lineWidthcanvasWidth){ctx.fillText(str.substring(lastSubStrIndex,i),leftWidth,initHeight);//绘制截取部分initHeight+=22;//22为文字大小20+2lineWidth=0;lastSubStrIndex=i;titleHeight+=22;}if(i==str.length-1){//绘制剩余部分ctx.fillText(str.substring(lastSubStrIndex,i+1),leftWidth,initHeight);}}//标题border-bottom线距顶部距离titleHeight=titleHeight+10;returntitleHeight;},/***//隐藏遮罩层*/hideModal:function(){letthat=this;letanimation=wx.createAnimation({duration:200,timingFunction:"linear",delay:0});that.animation=animation;animation.translateY(animationShowHeight).step();that.setData({animationData:animation.export(),});setTimeout(function(){animation.translateY(0).step();that.setData({animationData:animation.export(),showModalStatus:false})}.bind(that),200);},onShow:function(){letthat=this;wx.getSystemInfo({success:function(res){animationShowHeight=res.windowHeight;}});},/***转发*/onShareAppMessage:function(){letthat=this;return{title:that.data.shopName,path:'/pages/detail/detail?id='+5,}},});3.
4.参考:https://blog.csdn.net/baozhuona/article/details/78570483
小程序如何分享朋友圈-小程序分享之分享到朋友圈-小程序分享
浏览量:1922
时间:
来源:yi好快的刀
版权声明
即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。
最新资讯
-

即速应用,赋能企业玩转微信小程序智慧经营
作为国内领军的智慧商业经营服务商,即速应用始终秉承“让每个企业都拥有自己的智慧店铺”的愿景,持续赋能更多企业玩转智慧经营。即速应用旗下拥有“小程序搭建工具-即速应用”、“私域流量专家-即客云”等产品,帮助商家打通互联网全生态营销闭环。 -

即客云2.0重磅更新,让微信小程序运营更简单!
即客云作为一款基于企业微信的第三方工具,现从多维度提供超过30种功能,自上线以来,已服务多家企业,受到一致好评。近期,我们根据客户反馈和市场调研正式推出升级版 即客云2.0!更新了私域运营SOP,群日历功能,批量拓客,客户雷达,消息推送,个人欢迎语,帮助企业更好运用企业微信;同时提升了社群运营工作标准化,提升运营效率,帮助企业实现客户增长,玩转私域流量。 -

零代码 + AI 双轮驱动|即速应用解锁人工智能小程序开发新范式
无需代码、无需 AI 算法功底,普通人也能快速搭建智能小程序。即速应用将人工智能与零代码开发深度融合,推出 AI 智能生成能力,用户通过自然语言描述需求,AI 自动生成小程序页面、功能模块与后台配置,覆盖商城、预约、同城、社区团购等全场景。平台内置 AI 智能推荐、智能客服、用户画像分析等能力,一键对接微信生态,打通视频号、企业微信、短信跳转,帮企业快速落地 AI 应用,抢占智慧经营先机,让每家企业都拥有 AI 驱动的智慧店铺。












