将canvas画布生成图片,并保存到手机相册
wxml:
1 2 3 4 5 6 7 | < canvas canvas-id="gameCanvas" style="width:750rpx; height:350rpx" hidden="{{!statusTag}}" ></ canvas > < button bindtap="createImage">生成图片</ button > |
js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | // pages/index/pic.js Page({ /** * 页面的初始数据 */ data: { imagePath: 'http://imgo2o.shikee.com/goods/2019/10/17/201910171144361688.jpg' , imageWidth: '' , imageHeight: '' , show:0, statusTag: false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { let _this = this , deviceWidth = 0; //获取设备宽度,用于求所需画在画布上的图片的高度 wx.getSystemInfo({ success: function (res){ deviceWidth = res.windowWidth; //获取设备宽度 wx.getImageInfo({ //获取图片信息 src: _this.data.imagePath, success: function (res){ let imageWidth = deviceWidth, imageHeight = deviceWidth/res.width*res.height; //求所需画在画布上的图片的高度 _this.setData({ 'imageWidth' : imageWidth, 'imageHeight' :imageHeight }); console.log(imageHeight); const ctx = wx.createCanvasContext( 'gameCanvas' ); //创建画布对象 ctx.drawImage(_this.data.imagePath, 0, 0, imageWidth, imageHeight); //添加图片 ctx.setFontSize(16); //设置字体大小 ctx.setFillStyle( 'blue' ); //设置字体颜色 ctx.fillText( '你的名字' , imageWidth/2, imageHeight/2); //设置字体内容、坐标 ctx.draw(); //开始绘画 } }) } }); }, createImage: function (){ this .setData({ statusTag: true }) let imageWidth = this .data.imageWidth, imageHeight = this .data.imageHeight; wx.canvasToTempFilePath({ //将canvas生成图片 canvasId: 'gameCanvas' , x: 0, y: 0, width: imageWidth, height: imageHeight, destWidth: imageWidth, //截取canvas的宽度 destHeight: imageHeight, //截取canvas的高度 success: function (res) { wx.saveImageToPhotosAlbum({ //保存图片到相册 filePath: res.tempFilePath, success: function () { wx.showToast({ title: "生成图片成功!" , duration: 2000 }) } }) } }) }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } }) |
更多详细的功能:https://www.cnblogs.com/gcjun/p/11724531.html
H5-canvas生成图片案例:
链接:https://pan.baidu.com/s/1IRiHGeK-X4kC4or0Zj97Gw
提取码:u15s
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2017-01-06 百度API城市代码CityCode官方文档