uniapp微信小程序生成分享海报(模板自取)

这篇具有很好参考价值的文章主要介绍了uniapp微信小程序生成分享海报(模板自取)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

uniapp微信小程序生成分享海报模板

1、模板自取

2、可自行按需求更改调整

3、效果图如下: 生成前  -----> 生成后的图

小程序分享生成海报,小程序,微信小程序,uni-app,小程序小程序分享生成海报,小程序,微信小程序,uni-app,小程序

需知:博主的实现效果是先把需要生成的图片排版成静态页面,再点击生成海报----->通过canvas生成海报!!!

           不需要这样效果话可以省略第一步,直接一进页面调用方法生成canvas海报!!!( 示例大部分代码可省略 )

4、全部代码如下所示:

html:

<template>
    <view class="container">
       <view class="bigTitle">分享签到</view>
       <view class="bigTitleTwo">坚持打卡学习,和我一起吧!</view>
        
      <view class="shareBox">
        <view class="topTime">
            <view class="topItem">{{todayTime}}</view>
            <view class="topItem">{{today}}</view>
        </view>
        <!-- <u--image mode="widthFix" :showLoading="true" :src="mainPic" width="400rpx" height="400rpx"></u--image> -->
        <image :src="mainPic" mode="widthFix" class='bgImg'></image>
        <view class="shareText">
         <view class='text'>
             <rich-text :nodes="todaySaying || '-'"></rich-text>
         </view>
          <!-- <text class='text text2'> ———— 周杰伦《晴天》</text> -->
        </view>
     
        <view class='imgBox'>
          <button open-type="share" class='zfbtn'>
            <image src="http://www.meitang.cn/static/img/images/share_weixin.png" class='img'></image>
             <text class='btntxt'>分享朋友</text>
          </button>
          <button class='zfbtn m_l' @click="formSubmit">
            <image src="http://www.meitang.cn/static/img/images/share_fuzhi.png" class='img'></image>
            <text class='btntxt'>生成海报</text>
          </button>
        </view>
      
        <!--生成海报后的图  -->
          <view class='imagePathBox' :hidden="maskHidden == false">
            <image :src="imagePath" class='shengcheng' show-menu-by-longpress="true"></image>
            <button class='baocun' @click="baocun">保存相册,分享到朋友圈</button>
          </view>
         <view :hidden="maskHidden == false" class="mask"></view> 
        <view class="canvas-box">
            <!-- style="width: 500rpx;height: 840rpx;position:fixed;" -->
            <canvas canvas-id="mycanvas" style="width: 750rpx;height: 1334rpx;position:fixed;" />
        </view>  
      </view>
    </view>
   
</template>

    data数据:

    data() {
            return {
               formatDate,
               imgView:this.$api.viewFile,
               canvasW:375,     // 设备宽
               img: "1654073743713419267",
               maskHidden: false,
               name: "野生九五",
               todaySaying:'好运永远不会眷顾傻瓜',
               imagePath:'',    // 显示生成的图片
               mainPic: this.$api.viewFile + '1654305599822200833',
               erPic: this.$api.viewFile + '20230518001',
               todayTime:'2023-03-18',
               today:'星期一',
               urlParams:{},   // 路由参数
            }
        },

onShow():

    onShow() {

        // 获取屏幕的宽度为画布宽
        let that = this
        uni.getSystemInfo({
            success(res) {
                let { windowWidth } = res
                that.canvasW = windowWidth
            }
        });
        
         this.getShareImgInfo();   // 获取内容信息
        
    },

方法:

     methods: {
            
            /* 获取分享图片上的内容信息 */
            async getShareImgInfo(){
                let { indexContent } = this.urlParams
                try{
                    let res = await this.$api.getShareImgInfo()
                    if(res.code === 200){
                        let { content,img,localDate,week } = res.data
                        this.mainPic = this.$api.viewFile + (img === null ? '1654305599822200833' : img )
                        this.todayTime = localDate
                        this.todaySaying = indexContent ? indexContent : content
                        this.today = week
                    }
                }catch(e){
                    
                }
            },
                
            
            // 获取图片信息
            getImageInfo(image) {
                return new Promise((req, rej) => {
                    uni.getImageInfo({
                        src: image,
                        success: function(res) {
                            req(res)
                        },
                    });
                })
            },
              /*  canvas文本换行 */
                drawText (ctx, str, leftWidth, initHeight, titleHeight, canvasWidth) {
                    let lineWidth = 0;
                    let lastSubStrIndex = 0; //每次开始截取的字符串的索引
                    for (let i = 0; i < str.length; i++) {
                        lineWidth += ctx.measureText(str[i]).width;
                        if (lineWidth > canvasWidth) {
                            ctx.fillText(str.substring(lastSubStrIndex, i), leftWidth, initHeight); //绘制截取部分
                            initHeight += 22; //22为 文字大小20 + 2
                            lineWidth = 0;
                            lastSubStrIndex = i;
                            titleHeight += 22;
                        }
                        if (i == str.length - 1) { //绘制剩余部分
                            ctx.fillText(str.substring(lastSubStrIndex, i + 1), leftWidth, initHeight);
                        }
                    }
                    // 标题border-bottom 线距顶部距离
                    titleHeight = titleHeight + 10;
                    return titleHeight;
                },

                
                 //将canvas转换为图片保存到本地,然后将图片路径传给image图片的src
              async createNewImg() {
                 let that = this;
                 let { mainPic,erPic,name,todaySaying,todayTime,today,canvasW } = this
                
                 this.mainPic = await this.getImageInfo(mainPic)    // 处理图片
                 this.erPic = await this.getImageInfo(erPic)        // 处理图片
                  
                 var context = uni.createCanvasContext('mycanvas');
                 context.setFillStyle("#FFF")      // #F24077            // 背景颜色
                 context.fillRect(0, 0, canvasW, 610)              //  667 绘制一个矩形作为背景矩形x坐标、y坐标、图片宽、高
                 
                 context.setFontSize(26);
                 context.setFillStyle('#000');     // #FFF
                 context.setTextAlign('left');
                 context.fillText("分享签到", 37, 45);
                 context.stroke();
                 context.setFontSize(14);
                 context.setFillStyle('#000');
                 context.setTextAlign('left');
                 context.fillText("坚持打卡学习,和我一起吧!", 37, 75);
                 context.stroke();
                 
                 context.setFontSize(14);
                 context.setFillStyle('#333');
                 context.setTextAlign('left');
                 context.fillText(todayTime, 37, 100);
                 context.stroke();
                 
                 context.setFontSize(16);
                 context.setFillStyle('#333');
                 context.setTextAlign('right');
                 context.fillText(today, (canvasW - 37), 100);
                 context.stroke();
                 
                 
                 context.drawImage(this.mainPic.path, (canvasW - 300)/2, 110, 300, 320);  // 262 349

                 // context.drawImage(path2, 56, 400, 263, 121);  // 绘制图像到画布src、x坐标、y坐标、图片宽、高 
                 
                 // 今日上上签
                 context.setFontSize(15);
                 context.setFillStyle('#333');
                 context.setTextAlign('left');
                 // context.fillText(todaySaying, 37, 480);
                 this.drawText(context, todaySaying, 37, 470, 30, 300);   // 调用行文本换行函数(300则换行)
                 context.stroke();
               
                 //绘制左下角文字
                 context.setFontSize(12);
                 context.setFillStyle('#333');
                 context.setTextAlign('left');
                 context.fillText("长按识别小程序", 80, 560);
                 context.stroke();
                 context.setFontSize(12);
                 context.setFillStyle('#333');
                 context.setTextAlign('left');
                 context.fillText("跟我一起来学习吧~~", 80, 580);
                 context.stroke();
                
                 //绘制右下角小程序二维码
                 context.drawImage(this.erPic.path, 230, 525,70,70);
              
                 context.draw();
                 //将生成好的图片保存到本地
                 setTimeout(function () {
                   uni.canvasToTempFilePath({
                     canvasId: 'mycanvas',
                     success: function (res) {
                       var tempFilePath = res.tempFilePath;
                       that.imagePath = tempFilePath,
                       that.canvasHidden = true
                       
                        console.log('将生成好的图片保存到本地',res,that.imagePath)
                       
                     },
                     fail: function (res) {
                       console.log(res);
                     }
                   });
                 }, 200);
               },
               
               //点击保存到相册
                 baocun() {
                   var that = this
                   let { imagePath } = this 
                   console.log('点击保存到相册',imagePath)
                   uni.saveImageToPhotosAlbum({
                     filePath: imagePath,
                     success(res) {
                       uni.showModal({
                         content: '海报已保存到相册',
                         showCancel: false,
                         confirmText: '确定',
                         confirmColor: '#333',
                         success: function (res) {
                           if (res.confirm) {
                             console.log('用户点击确定');
                             /* 该隐藏的隐藏 */
                             that.maskHidden = false
                           }
                         }, fail: function (res) {
                           console.log(11111)
                         }
                       })
                     },
                     fail() {
                          uni.showModal({
                            title: '提示',
                            content: '需要您授权保存相册',
                            showCancel: false,
                            success() {
                              uni.openSetting({
                                success(settingdata) {
                                  if (settingdata.authSetting['scope.writePhotosAlbum']) {       //是否授权保存到相册
                                    wx.showModal({
                                      title: '提示',
                                      content: '获取权限成功,再次保存图片即可成功',
                                      showCancel: false,
                                    })
                                  } else {
                                    uni.showModal({
                                      title: '提示',
                                      content: '获取权限失败,无法保存到相册',
                                      showCancel: false,
                                    })
                                  }
                
                                },
                              })
                            },
                            fail() {
                                uni.$u.toast('您已取消')
                            },
                          })
                        },
                     
                   })
                 },
                 
                  //点击生成
                   formSubmit: function (e) {
                     var that = this;
                     this.maskHidden = false
                     // this.setData({
                     //   maskHidden: false
                     // });
                     uni.showToast({
                       title: '海报生成中...',
                       icon: 'loading',
                       duration: 1000
                     });
                     setTimeout(function () {
                       uni.hideToast()
                       that.createNewImg();   // 绘图
                       
                       that.maskHidden = true
                       // that.setData({
                       //   maskHidden: true
                       // });
                     }, 1000)
                   },
                   
        },

css样式:

<style lang="scss">

  
  
.container{
    height: 100vh;
    padding: 10%;
    // background-image: linear-gradient(to bottom right,#F2427B,#EE2847);
    background-color: #DAEDFB;
    
    .bigTitle{
        font-size: 42rpx;
        // color: #FFFFFF;
        padding-bottom: 25rpx;
    }
    .bigTitleTwo{
        font-size: 26rpx;
        // color: #FFFFFF;
        padding-bottom: 25rpx;
    }
    
    .shareBox{
       display: flex;
       flex-direction: column;
       background-color: #fff;
       
        .topTime{
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 40rpx;
            
            .topItem{
                font-size: 26rpx;
                color: #333;
            }
        }
        
        
        // canvas
        .canvas-box{
            position: fixed;
            opacity: 0;
            bottom: 9999999rpx;
            //  width: 600rpx;
            //  height: 860rpx;
            //  display: flex;
            //  flex-direction: column;
            //  align-items: center;
            
            //  top: 50rpx;
            //  left: calc(50% - 300rpx);
             
            // canvas{
            //     width: 100%;
            //     height: 100%;

            // }
        }
    }
}
    


.bgImg{
  width: 70%;
  height: 440rpx !important;
  margin: 0% 15%;
}
.shareText{
  color: #333;
  font-size: 28rpx;
  display: flex;
  flex-direction: column;
  align-content: center;
  justify-content: center;
  padding: 50rpx 30rpx;
}
.shareText .text{
  // line-height: 60rpx;
  width: 100%;
  box-sizing: border-box;
  display: block;
  color: #333;
}
.shareText .text2{
  text-align: right;
}
.btntxt{
   color: #333;
     font-size: 26rpx;
}
.imgBox{
  text-align: center;
  width: 100%;
  margin-top:20rpx;
  display: flex;
}
.img{
  display: inline-block;
  width: 100%;
  height: 100%;
}
.m_l{
  margin-left: 180rpx;
}
.zfbtn{
  display:flex;
  flex-direction: column;
  font-size: 28rpx;
  align-items: center;
  justify-content: center;
  background: #fff;
}
.zfbtn image{
  width: 70rpx;
  height: 70rpx;
}
button[class="zfbtn"]::after {
  border: 0;

button[class="zfbtn m_l"]::after {
  border: 0;

.imagePathBox{
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.7);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100000;
}
.shengcheng{
  width: 86%;
  height: 80%;
  position: fixed;
  top: 10%;
  left: 50%;
  margin-left: -43%;
  z-index: 999;
}
.baocun{
  display: block;
  width: 80%;
  height: 75rpx;
  padding: 0;
  line-height: 75rpx;
  text-align: center;
  position: fixed;
  bottom: 50rpx;
  left: 10%;
  color: #fff;
  font-size: 32rpx;
  border-radius: 50rpx;
  background: #fdd668;

}
button[class="baocun"]::after{
  border: 0;
}
</style>
文章来源地址https://www.toymoban.com/news/detail-627749.html

到了这里,关于uniapp微信小程序生成分享海报(模板自取)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序生成二维码海报并分享

    背景:点击图标,生成海报后,点击保存相册,可以保存 生成海报:插件wxa-plugin-canvas,此处使用页面异步生成组件方式,官网地址:wxa-plugin-canvas - npm 二维码:调用后端接口生成二维码 需要调用获取图片信息接口wx.getImageInfo(),获取到图片的宽高以做整体宽高配置 closePos

    2024年03月21日
    浏览(34)
  • 微信小程序 api+前端实现生成分享海报

    1.先看效果图,点击分享海报按钮,然后弹出分享海报  2.前端代码 这里用的组件有vant组件库还有canvas_drawer(一个画布组件) canvas_drawer下载地址 https://github.com/kuckboy1994/mp_canvas_drawer 把 components 中的 canvasdrawer 拷贝到自己项目下,然后再app.json中引用就行了,如下 \\\"usingCompon

    2024年02月09日
    浏览(38)
  • 微信小程序使用canvas生成分享海报功能复盘

    近期需要开发一个微信小程序生成海报分享的功能。在h5一般都会直接采用 html2canvas 或者 dom2image 之类的库直接处理。但是由于小程序不具备传统意义的dom元素,所以也没有办法采用此类工具。 所以就只能一笔一笔的用 canvas 画出来了,下面对实现这个功能中遇到的问题做一

    2024年02月16日
    浏览(42)
  • 【微信小程序】用painter插件生成海报分享朋友圈简单教程

    第一步:去Git下载插件 1.这是核心插件 需要下载全部内容 2.官方文档 3.新建painter文件夹放到下面 4.在引用文件的json文件引用一下 5.在使用文件里创建个canvas.js文件 获取canvas.js内容去这个网站 先点击导出,在点击复制,复制到canvas.js文件里(替换) 6.然后在对应page页面的

    2024年02月10日
    浏览(42)
  • 微信小程序使用canvas画布生成二维码海报分享图片(完整示例代码)

    canvas.js //获取应用实例 const app = getApp() Page({ /** 页面的初始数据 */ data: { // canvas _width: 0, //手机屏宽 _heigth: 0,//手机屏高 swiperHeight: 300,//主图图片高度 canvasType: false,//canvas是否显示 loadImagePath: ‘’,//下载的图片 imageUrl: ‘https://cos.myfaka.com/car/service/1.jpg’, //主图网络路径 codeU

    2024年04月12日
    浏览(55)
  • 微信小程序canvas type=2d生成海报保存到相册、文字换行溢出显示...、文字删除线、分享面板

    做个简单的生成二维码海报分享, 我做的时候也找简单的方法看能不能实现页面直接截图那种生成图片,原生小程序不支持, 不多介绍下面有全部代码有注释、参数自行替换运行看看,还有需要优化的地方,有问题可以咨询我,我写的已经上线 如图:

    2024年02月11日
    浏览(32)
  • uniapp 实现生成海报并分享给微信好友和保存到本地相册

    – 最近又遇到一个需求:用户需要将小程序生成的二维码海报分享给微信好友或者保存到本地,最后实现的效果如下: 这种网上随便找一下就有了,楼主采用的是tki-qrcode 生成二维码组件,具体的链接如下: 链接: https://blog.csdn.net/qq_45829293/article/details/123169952 因为考虑到到时

    2023年04月09日
    浏览(25)
  • uniapp开发微信小程序生成二维码海报

    模板:

    2024年02月12日
    浏览(35)
  • 微信小程序之海报生成

    前言:2.9.0 起支持一套新 Canvas 2D 接口(需指定 type 属性),同时支持同层渲染,原有接口不再维护 参考文档:https://developers.weixin.qq.com/miniprogram/dev/component/canvas.html    

    2024年02月11日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包