前言:2.9.0 起支持一套新 Canvas 2D 接口(需指定 type 属性),同时支持同层渲染,原有接口不再维护

参考文档:https://developers.weixin.qq.com/miniprogram/dev/component/canvas.html

<!-- 海报 -->
<view catchtouchmove="preventTouchMove" class="canvasMain" hidden="{{!posterDatas.show}}"><canvas type="2d" id="firstCanvas" class="firstCanvas" style="width:{{posterDatas.width}}px;height:{{posterDatas.height}}px;"></canvas><button wx:if="{{posterDatas.buttonType==1}}" class='button' bindtap='onDownloadImges'>点击保存,分享朋友圈</button><button wx:if="{{posterDatas.buttonType==2}}" class='button'>已保存到相册,快去分享吧</button><button wx:if="{{posterDatas.buttonType==3}}" class='button' open-type='openSetting' bindopensetting='onBindOpenSetting'>进入设置页,开启“保存到相册”</button><image bindtap='onIsCanvas' class='x' src='/pages/images/x3.png'></image>
</view><view class="canvas2d" catchtap='onBuildPosterSaveAlbum'>立即生成</view>
page {background-color: #fff;
}/* 海报 */
.canvasMain {display: flex;flex-direction: column;justify-content: center;align-items: center;align-content: center;position: fixed;left: 0;top: 0;width: 100%;height: 100%;background-color: rgba(0, 0, 0, 0.6);z-index: 9999;
}.canvasMain canvas {margin-bottom: 40px;
}.canvas2d {width: 300rpx;height: 80rpx;font-size: 28rpx;color: #000;display: flex;align-items: center;justify-content: center;border: 2rpx solid #000;margin: 0 auto;
}
.canvasMain .button {height: 80rpx;width: 600rpx;background-color: rgba(0, 0, 0, 0.3);border-radius: 40px;color: #fff;font-size: 16px;line-height: 80rpx;display: flex;align-items: center;justify-content: center;
}.canvasMain .x {width: 15px;height: 15px;margin-top: 10px;border: solid 2px #fff;border-radius: 50%;padding: 10px;
}
// pages/huabu/huabu.js
Page({/*** 页面的初始数据*/data: {//海报posterDatas: {width: 300, //画布宽度height: 350, //画布高度// 缓冲区,无需手动设定pic: null,buttonType: 1,show: false, // 显示隐藏海报弹窗success: false, // 是否成功生成过海报canvas: null, // 画布的节点ctx: null, // 画布的上下文dpr: 1, // 设备的像素比},},/*** 生命周期函数--监听页面加载*/onLoad(options) {var that = this;//生成海报初始化var posterDatas = that.data.posterDatasconst query = wx.createSelectorQuery()query.select('#firstCanvas').fields({node: true,size: true},function (res) {const canvas = res.nodeconst ctx = canvas.getContext('2d')const dpr = wx.getSystemInfoSync().pixelRatiocanvas.width = posterDatas.width * dprcanvas.height = posterDatas.height * dprctx.scale(dpr, dpr)posterDatas.canvas = canvasposterDatas.ctx = ctxposterDatas.dpr = dpr//存储that.setData({posterDatas})}).exec()},//海报生成//画布 生成 海报[海报]onBuildPosterSaveAlbum: function () {var that = this;var posterDatas = that.data.posterDatasvar canvas = posterDatas.canvasvar ctx = posterDatas.ctx//已生成过海报的直接显示弹窗if (posterDatas.success) {posterDatas["show"] = true;that.setData({posterDatas})return;}posterDatas.show = true;that.setData({posterDatas})wx.showLoading({title: '海报生成中',mask: true});//二维码var promise1 = new Promise(function (resolve, reject) {const photo = canvas.createImage();photo.src = "https://img0.baidu.com/it/u=2529320505,1328670466&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500";photo.onload = (e) => {resolve(photo);}});//获取图片信息Promise.all([promise1]).then(res => {// 绘制白色背景// util.roundRect(ctx, 0, 0, posterDatas.width, posterDatas.height, 10);ctx.fillStyle = "#ffffff";ctx.fillRect(0, 0, canvas.width, canvas.height);//绘制[商品图片]ctx.drawImage(res[0], 0, 0, posterDatas.width, 300);//名称//底部说明ctx.font = "bold 15px Arial"; //字体大小ctx.fillStyle = "#000"; //字体颜色ctx.textAlign = "center"ctx.fillText('海报已生成', 155, 330);// 关闭loadingwx.hideLoading();//显示海报posterDatas.success = true;that.setData({posterDatas})}).catch(err => {console.log(err)wx.hideLoading();wx.showToast({icon: 'none',title: '海报生成失败,请稍后再试.',})})},//画布 转 图片[海报]onCanvasBuildImges: function () {var that = this;var posterDatas = that.data.posterDatas;wx.canvasToTempFilePath({canvas: posterDatas.canvas,width: posterDatas.width,height: posterDatas.height,destWidth: posterDatas.width * 3,destHeight: posterDatas.height * 3,success: function success(res) {posterDatas["pic"] = res.tempFilePath;that.setData({posterDatas})that.onDownloadImges();},fail: function complete(e) {wx.hideLoading();wx.showToast({icon: 'none',title: 'sorry 保存失败,请稍后再试.',})return;}});},//下载图片[海报]onDownloadImges: function () {wx.showLoading({title: '保存中',mask: true});var that = this;var posterDatas = that.data.posterDatas;if (!posterDatas.pic) {that.onCanvasBuildImges();return;}//可写成函数调用 这里不做解释wx.saveImageToPhotosAlbum({filePath: posterDatas.pic,success(res) {wx.hideLoading();wx.showToast({icon: 'none',title: '已保存到相册,快去分享吧',})posterDatas["buttonType"] = 2;that.setData({posterDatas})},fail: function (res) {wx.hideLoading();wx.showToast({icon: 'none',title: '进入设置页,开启“保存到相册”',})posterDatas["buttonType"] = 3;that.setData({posterDatas})return;}})},//在打开授权设置页后回调onBindOpenSetting: function () {var that = this;var posterDatas = that.data.posterDatas;posterDatas["buttonType"] = 1;that.setData({posterDatas})},//隐藏海报[海报]onIsCanvas: function () {var that = this;var posterDatas = that.data.posterDatas;posterDatas["buttonType"] = 1;posterDatas["show"] = false;that.setData({posterDatas})},//自定义弹窗后禁止屏幕滚动(滚动穿透)[海报]preventTouchMove: function () {//在蒙层加上 catchtouchmove 事件//这里什么都不要放},
})

微信小程序之海报生成相关推荐

  1. 微信小程序分享海报/卡片 生成时一直加载可能存在的问题

    微信小程序分享海报/卡片 生成时一直加载可能存在的问题 很多时候,开发者在调试小程序的分享图时,总是会遇到不能正常生成的问题,这里面还是有许多的坑.这一次就把我个人所知道的问题分享一下. 第一种情况: ...

  2. uniapp 微信小程序分享海报

    uniapp 微信小程序分享海报 下面是一个Uniapp微信小程序分享海报的简单示例: 在Uniapp项目中创建一个新的页面,用于展示要分享的内容和生成海报.例如,我们可以在新页面中显示一张图片和一些 ...

  3. 微信小程序业务-字符串生成二维码(weapp-qrcode)

    微信小程序业务-字符串生成二维码(weapp-qrcode) 前言 邂逅weapp-qrcode 基本使用 详细参数 小程序组件中使用 image属性详解 想使用网络图片? 参考地址 前言 在小程序项 ...

  4. 微信小程序使用weapp-qrcode生成二维码

    <canvas style="width:108rpx;height:108rpx; canvas-id='qrcode'></canvas> // weapp-qr ...

  5. 微信小程序:检讨书生成微信小程序源码

    对于经常写检讨的小伙伴来说,福音来了 因为这是一款检讨书生成小程序 所以再也不用为了写检讨而烦恼了哦 支持自定义字数下线,主题自定义 支持多种类型检讨比如:学生党的,男朋友,领导演讲稿,共青团申请书等 ...

  6. 微信小程序绘制海报,或者把多张图片合成一张

    微信小程序绘制海报,或者把多张图片合成一张,例子 <image style='height:{{mabgh}}px;width:{{mabgw}}px;' class="savepng ...

  7. 微信小程序纯前端生成海报并保存本地

    需求 公司开发微信小程序,有一个海报页面,需要用户点击生成海报,可以将该该swipe-item 生成一个带二维码的图片,最终由纯前端实现! 技术调研 因为小程序的打包限制,不可能将所有的图片都放在代码 ...

  8. 微信小程序实现画布生成海报功能

    微信小程序可以通过使用 标签来实现生成海报的功能.以下是基本实现步骤: 1.在 WXML 文件中创建一个 标签,并设置其宽度和高度属性.' <canvas canvas-id="myC ...

  9. 微信小程序——weapp-qrcode.js生成二维码、海报二维码、核销码

    各位小伙伴们,不知道你们在开发的时候有没有遇到小程序生成二维码,生成海报时候带上二维码.核销码等等. 那么,小程序端该如何生成二维码图片呢? 效果如下: weapp-qrcode.js 小伙伴们可以来 ...

  10. 微信小程序合成海报_利用微信小程序中Canvas API来合成海报生成组件封装

    每个小程序成型后,一般都会选择生成带菊花码的海报分享出去来吸引更多的流量.下面来介绍下其他的一种实现方式吧 原理:主要利用微信小程序强大的Canvas API来合成,生成后可用wx.canvasToT ...

最新文章

  1. 属性处理器Spring攻略学习笔记(2.12)------外部化Bean配置
  2. Matlab:利用Matlab编程实现模拟分子布朗运动的动画展示
  3. struts2 spring hibernate 原理
  4. 代码评审会议_如何将电话会议(和访问代码)另存为联系人
  5. 你的代码是否按照高内聚、低耦合的原则来设计的?
  6. 不要错过路边的的风景
  7. 一篇文章带你搞定和Spring Boot有关的那些高频面试题
  8. Centos7 安装Jdk1.8u172
  9. [原]Java程序员的JavaScript学习笔记(1——理念)
  10. Android TextView 跑马灯滚动效果
  11. mysql日期范围比较函数_mysql 日期比较函数
  12. robocode_Robocode大师的秘密,多态敌人缓存
  13. HTML5期末大作业:体育主题网站设计——足球(9页) HTML+CSS+JavaScrip校园篮球网页作业成品 学校篮足球网页制作模板 学生简单体育运动网站设计成品
  14. NDEF格式的smart tag智能标签(Mifrare Ultralight)在BlackBerry 9900手机中的使用
  15. DH坐标系的建立与DH表—机器人学
  16. Only the Best Are on the Cutting Edge
  17. 校园导游系统数据结构课程设计(附完整代码)
  18. jQuery 的自定义事件
  19. STM32 cubemx 开发系列文章(一)认识cubemx
  20. 基于IndRNN的微博短文本情感分析设计与实现

热门文章

  1. go语言[3]-数组
  2. 会议及作用篇--项目管理(十三)
  3. 64位Windows2003下如何正确发布VesnData.Net(VDN)
  4. 微信小程序视频学习教程
  5. spring boot英语在线学习系统毕业设计-附源码211714
  6. 基于简单协同过滤推荐算法职位推荐系统
  7. 回弹怎么用计算机计算,回弹法检测混凝土抗压强度标准差和推定值的计算方法 公式...
  8. QT制作一个串口调试助手出现乱码问题
  9. 微信H5分享、复制链接遇到的坑
  10. 贾俊平《统计学》常用公式