微信小程序之海报生成

article/2025/11/3 21:27:37

前言: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 事件//这里什么都不要放},
})

 


http://chatgpt.dhexx.cn/article/XQIh7KRe.shtml

相关文章

如何自己制作小程序?

现在小程序非常流行&#xff0c;很多人都想制作一个自己的小程序来卖货。以前制作小程序通常需要请小程序开发公司&#xff0c;而且需要每年给多少钱&#xff08;通常几百到几千&#xff09;。但随着微信越来越完善&#xff0c;现在无需找小程序服务商搭建&#xff0c;无需懂代…

微信小程序链接快速生成方法

文章目录 前言一、如何生成微信小程序链接&#xff1f;二、生成微信小程序链接步骤1.获取 URL Scheme2.获取 URL Link3.获取 Short Link4.短信打开小程序5.NFC 标签打开小程序 总结 前言 微信官方更新了链接跳转微信小程序的功能&#xff0c;在点击链接后即可直接跳转到小程序…

小程序码的生成

一、生成方式 微信小程序提供了三个接口来生成小程序码&#xff0c;分别是wxacode.createQRCode()、wxacode.get()、wxacode.getUnlimited()&#xff0c;生成的小程序码永久有效&#xff0c;其中仅getUnlimited接口生成的小程序码数量暂无限制 二、接口详解 1、wxacode.creat…

微信小程序分享小程序码的生成(带参数)以及参数的获取

这篇文章主要介绍了微信小程序分享小程序码的生成&#xff08;带参数&#xff09;以及参数的获取&#xff0c;文中通过示例代码介绍的非常详细&#xff0c;对大家的学习或者工作具有一定的参考学习价值&#xff0c;需要的朋友们下面随着小编来一起学习学习吧 1.小程序码介绍 从…

netlify 自动部署化工具

一、使用github或者gitlab登陆netlify 打开进入netlify官网 二、然后使用github或者gitlab账号登录。 三、登录成功后直接将自己build生成的dist 文件目录拖到虚线框中 四、可以看到netlify为我们随机生成了一个netlify下的域名&#xff0c;可以直接在浏览器访问。 五&…

Coolify: 一款超强大的开源自托管 Heroku / Netlify 替代方案

公众号关注 「奇妙的 Linux 世界」 设为「星标」&#xff0c;每天带你玩转 Linux &#xff01; Coolify 是一种可自我托管的综合解决方案&#xff0c;只需单击几下即可托管你的应用、数据库或其他开源服务。它是 Heroku 和 Netlify 的一个替代方案。 通过 Coolify 可以部署很多…

122.将实战网页部署到Netlify

● 本章我们将网站部署到Netlify ● 首先先点击右上角注册一个账号 ● 注册完成之后&#xff0c;点击sites&#xff0c;将我们的网站文件夹拖入 ● 上传成功 ● 之后就可以正常访问啦 ● 我们也可以修改我们的站点名称&#xff0c;让他更加好记

在netlify上部署golang web应用

介绍 Netlify是一个专门托管静态文件的云。这使得它非常适合托管开发人员博客、宣传册网站&#xff0c;甚至只是一个个人简历。它甚至内置了对Hugo的支持。但是Netlify也有各种动态托管解决方案&#xff0c;他们的functions服务是托管Go Web应用程序的一种非常简单的方法&…

利用Netlify/Vercel和Digitalpress搭建免费Ghost静态博客

第一步 先去https://www.digitalpress.blog/申请一个免费的Ghost博客。 当然你可以用自己的主机&#xff0c;或者digitalocean免费的学生包。只要是Ghost就可以。 第二步 注册Netlify或者Vercel(两个都可以&#xff0c;自己挑选,我自己测试Vercel链接更快) 自行测试&#…

NET Framework

NET Framework 是一个可以快速开发、部署网站服务及应用程序的开发平台&#xff0c;是 Windows 中的一个组件&#xff0c;包括公共语言运行时&#xff08;Common Language Runtime, CLR&#xff09;虚拟执行系统和 .NET Framework 类库。 .NET Framework 的特点如下。 提供标准…

使用Netlify部署博客

文章目录 github 项目部署自定义域名 原先博客是部署在 githubPages 上的&#xff0c;稍微设置一下就能实现自动化部署和启用 https&#xff0c;还是蛮方便的&#xff0c;但是使用国内网络访问 githubPages 上部署的网站速度太慢了&#xff0c;体验很差&#xff0c;因此&#x…

如何下载.NET Framework

下载网址&#xff1a; https://dotnet.microsoft.com/zh-cn/download/dotnet-framework 登录网址 选择适合的版本&#xff0c;如果你的版本太低可能要多尝试一个版本是否能够安装成功

【2022年】的网页转 App 教程

● 如何将网站转为 App&#xff1f; 使用 HopWeb 可以毫无技术成本的制作属于你的App。 HopWeb 官方网站&#xff1a;https://atreep.netlify.app/hopweb ● 网站转 App 适用于以下类型的网站&#xff1a;个人博客、工具类网站等 ● 本教程将以【百度】网站为例&#xff0c;引…

通过Netlify制作个人网站

个人主页的创建 本文主要介绍个人主页的创建过程&#xff0c;主要通过知乎回答的指导&#xff0c;参考本人制作的个人主页。 本人个人主页制作主要使用Netlify&#xff0c;参考该网站安装教程进行安装&#xff0c;直接在自己的github中创建一个仓库作为网站节点。在安装过程中…

【经验】静态博客部署 Hexo + Netlify-CMS + Vercel (在线构建)

目录 引入背景方案 步骤生成starter模板添加Netlify CMS在线管理添加Netlify身份验证组件启用git gateway身份验证换用Vercel作为CDN重新添加js添加身份验证器绑定oauth 定制404页面 模板已知问题 引入 背景 Hexo等静态博客相对于Wordpress等动态博客&#xff0c;可以白嫖很多…

.net framework 官方下载地址

.net framework 官方下载地址 https://dotnet.microsoft.com/zh-cn/download/dotnet-framework

使用Netlify部署静态网站

之前写了一篇文章是关于在树莓派上部署Hexo的博客&#xff0c;但树莓派难免会出故障&#xff0c;所以将网站放在另一个地方会更安全一点。 前一篇&#xff1a;https://fitswcblog.com/%E6%A0%91%E8%8E%93%E6%B4%BE%E6%90%AD%E5%BB%BAhexo%E5%8D%9A%E5%AE%A2/ 我在csdn上的所有…

123.HTML5+CSS3完结_使用Netlify收取表单

Netlify也可以做表单接受&#xff1a; 我们启动一下 修改下表单 ● 接着在我们的网站输入并提交表单 ● 之后会有一个提示&#xff0c;提示我们提交成功 然后就能在Netlify接受到用户的表单 ● 当然这个表单只能接受100个&#xff0c;但是作为实验也够用了 到此&a…

第七章:使用Netlify零成本部署组件文档

第七章&#xff1a;使用Netlify无成本发布组件文档 为什么使用Netlify&#xff1f; 一开始一共有三个方案&#xff1a; 1、Github Page 2、Netlify 3、Vercel Github Page只支持一个repo发布一个网站&#xff0c;而我们的项目是一个mononrepo项目&#xff0c;后续可能还有其他…

React项目全球新闻发布管理系统 - 新版问题解决方式整理及部署网站至 Netlify

整理了一下新版的变化以及遇到的坑的解决办法&#xff0c;最后也会分享将网站及接口部署的方式。 千锋前端-React全家桶_React项目全球新闻发布管理系统 https://www.bilibili.com/video/BV1fw411d7R5 文章目录 P4P5P6P11P15P17P18P22P29P30P34P38P41P43P45P50P67进阶: 多语系网…