• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

微信小程序生成海报保存图片到相册小测试

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

test.wxml

<canvas style="width:{{imageWidth}}px;height:{{imageHeight}}px;" canvas-id="myCanvas" class=\'canvas\' hidden=\'{{hideme}}\'></canvas>
<image wx:if="{{tempPath!=\'\'}}" class="viewimg" src="{{tempPath}}" mode="widthFix"></image>
<view class="operbtns">
  <button class="button" type="primary" bindtap="savePic">保存图片</button> 
</view>

 

test.js

Page({

  data: {
    title: \'测试\',
    tempPath: \'\',
    hideme: false
  },

  onLoad: function (options) {
    var that = this;
    var imageSize = that.image();
    that.setData({
      imageWidth: imageSize.imageWidth,
      imageHeight: imageSize.imageHeight,
      windowscale: imageSize.windowscale,
    });
    
    //获取背景图片
    that.getBackground();
    //获取头像
    //that.getAvatar();

    wx.showToast({
      title: \'正在生成图片\',
      icon: \'loading\',
      duration: 10000,
    });
  },

  image: function() {
      var imageSize = {};
      var originalScale = 1;//图片高宽比  
      //获取屏幕宽高  
      wx.getSystemInfo({
        success: function (res) {
          var windowWidth = res.windowWidth;
          var windowHeight = res.windowHeight;
          var windowscale = windowHeight / windowWidth;//屏幕高宽比  
          imageSize.windowscale = windowscale;
          if (originalScale < windowscale) {//图片高宽比小于屏幕高宽比  
            //图片缩放后的宽为屏幕宽  
            imageSize.imageWidth = windowWidth;
            imageSize.imageHeight = Math.floor(windowWidth * 1.779);
          } else {//图片高宽比大于屏幕高宽比  
            //图片缩放后的高为屏幕高  
            imageSize.imageHeight = windowHeight;
            imageSize.imageWidth = Math.floor(windowHeight / 1.779);
          }

        }
      })
      return imageSize;
    },

  getBackground: function () {
    var that = this;
    wx.downloadFile({
      url: \'https://a.b.c/public/images/bg.png\',
      success: function (res) {
        that.setData({
          tempPath: res.tempFilePath,
          share: res.tempFilePath,
        })
        //把背景画到画布上
        that.createImg();
      },
      fail: function () {
        console.log(\'fail\')
      }
    })
  },

  createImg: function () {
    var that = this;
    var ctx = wx.createCanvasContext(\'myCanvas\');
    ctx.setFillStyle(\'White\');
    ctx.fillRect(0, 0, that.data.imageWidth, that.data.imageHeight);
    ctx.drawImage(that.data.share, 0, 0, that.data.imageWidth, that.data.imageHeight);
    ctx.save();
    //ctx.beginPath();
    //ctx.arc(that.data.imageWidth / 2, that.data.imageHeight * 0.63, 65, 0, 2 * Math.PI);
    //ctx.clip();
    //ctx.drawImage(that.data.avatar, that.data.imageWidth / 2 - 60, that.data.imageHeight * 0.55, 120, 120);
    //ctx.restore();
    ctx.setTextAlign(\'center\');
    ctx.setFillStyle(\'#ffffff\');
    ctx.setFontSize(22);
    ctx.fillText(that.data.title, that.data.imageWidth / 2, that.data.imageHeight * 0.41);
    
    ctx.draw();
    //显示新画的图片
    that.viewPic();
  },

  viewPic: function(){
    var that = this;
    wx.canvasToTempFilePath({
      width: that.data.imageWidth,
      height: that.data.imageHeight,
      canvasId: \'myCanvas\',
      success: function (res) {
        that.setData({
          tempPath: res.tempFilePath,
          hideme: true
        });
      }
    });
    wx.hideToast();
  },

  savePic: function () {
    var that = this;
    wx.showToast({
      title: \'正在保存图片\',
      icon: \'loading\',
      duration: 10000,
    });
    util.savePicToAlbum(that.data.tempPath);
  },

  onReady: function () {
  },
  onShow: function () {
  },
  onHide: function () {
  },
  onUnload: function () {
  },
  onShareAppMessage: function () {
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  }
})

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小程序canvas生成海报保存至手机相册发布时间:2022-07-18
下一篇:
Taro 1.2.16 发布,BAT 小程序、H5 与 RN 端统一框架发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap