*新闻详情页*/>
刚开始完成以前先上个实际效果图
tips
1.互联网照片需先配备download网站域名,可根据wx.getImageInfo
变为临时性相对路径;
2.本人习惯性难题,我习惯性应用async-await英语的语法,因此必须引进regenerator这个库,应用方法可在网上查。
1、封裝通用性手机微信api回到为Promise目标
/datas/common.js
// 封裝获得手机微信照片信息内容。 export const getWxImageInfo = (imgPath) => { return new Promise((resolve, reject) => { wx.getImageInfo({ src: imgPath, success: res => { resolve(res) }, fail: res => { reject(res) } }) }) } // 封裝获得连接点挑选器信息内容 export const getSelectQurey = (queryStr) => { return new Promise(resolve => { var query = wx.createSelectorQuery(); query.select(queryStr).boundingClientRect(); query.exec(res => { resolve(res) }) }) } // 封裝把画布导出来转化成特定尺寸的照片 export const canvasToTempFilePath = (width, height, canvasId, fileType = 'jpg') => { return new Promise((resolve, reject) => { wx.canvasToTempFilePath({ width, height, canvasId, fileType, success: res => { resolve(res) }, fail: res => { reject(res) } }) }) } // 封裝储存照片到系统软件相册 export const saveImageToPhotosAlbum = (filePath) => { return new Promise((resolve, reject) => { wx.saveImageToPhotosAlbum({ filePath, success: res => { resolve(res) }, fail: res => { reject(res) } }) }) }
2、主视图的完成
.wxml
<view class="icon-download" catchtap="getCanvas">点一下转化成照片</view> <!-- 2维码大图 --> <view class='shade' wx:if="{{isShowCanvas}}"> <view class='qr-code'> <canvas class='qr-canvas' canvas-id="qrCanvas" id="qrCanvas"></canvas> <view class='qr-btn'> <view class='qr-btn-save' catchtap='saveImageToPhotosAlbumFunc'>储存照片,共享到盆友圈</view> <view class='qr-btn-cancel' catchtap='hideCanvas'>撤销</view> </view> </view> </view> <!-- 2维码大图.end -->
.wxss
/* 查询大图 */ .shade { width: 100%; height: 100%; background-color: rgba(240, 235, 235, 0.5); position: fixed; z-index: 100; top: 0; left: 0; } .qr-code { width: 600rpx; height: 1000rpx; background-color: #fff; position: absolute; top: 50%; left: 50%; transform: translate(⑸0%, ⑸0%); /* margin: 30rpx auto; */ } .qr-canvas { display: block; background-color: #fff; margin: 0 auto; width: 600rpx; height: 900rpx; } .qr-btn { width: 600rpx; height: 100rpx; line-height: 100rpx; margin: 0 auto; font-size: 28rpx; color: #fff; display: flex; background-color: #658dc5; } .qr-btn-save { flex: 0 0 500rpx; text-align: center; border-right: 1rpx solid #fff; } .qr-btn-cancel { text-align: center; flex: 0 0 100rpx; }
3、建立canvas并储存到系统软件相册
tips
产品图是正方形的,因此这里产品图的宽高都用canvas的宽文本不可以换行,这里只是简易的解决了1下
留意: wx.canvasToTempFilePath(Object object, Object this)
这个的应用,文本文档有1句话必须留意的:“把当今画布特定地区的內容导出来转化成特定尺寸的照片。在 draw() 回调函数里启用该方式才可以确保照片导出来取得成功。”
const app = getApp() const regeneratorRuntime = app.globalData.regeneratorRuntimeconst const util = require('../../utils/util.js') import { getSelectQurey, getWxImageInfo, canvasToTempFilePath, saveImageToPhotosAlbum } from '../../datas/common.js' Page({ data: { isShowCanvas: false, // 是不是显示信息canvas wxaCode: 'https://xxx..jpg', // 产品小程序流程码 goodsImageUrl: 'https://xxx..jpg', // 产品照片 canvasTempFilePath: '', // canvas导出来转化成照片的临时性相对路径 }, // 点一下显示信息要转化成的canvas getCanvas(e) { if (!this.data.wxaCode) { util.showToast('2维码转化成不成功'); return; } this.setData({ isShowCanvas: true }, () => { this.createCanvas(); }) }, // 掩藏canvas hideCanvas() { this.setData({ isShowCanvas: false }) }, // 建立canvas async createCanvas() { wx.showLoading({ title: '照片转化成中...' }) const _this = this // 建立连接点挑选器 const res = await getSelectQurey('#qrCanvas'); // canvas的宽高 const cvWidth = res[0].width; const cvHeight = res[0].height; const cvSubValue = cvHeight - cvWidth const qrWidth = cvSubValue / 1.5 const qrMargin = (cvSubValue - qrWidth) / 2 const qrX = cvWidth - qrWidth - qrMargin / 2 const qrY = cvWidth + qrMargin const shopNameY = cvWidth + cvSubValue - qrWidth // 2维码互联网照片转临时性相对路径 let qrImagePath = ''; try { const wxaCode = _this.data.wxaCode; const qrImage = await getWxImageInfo(wxaCode); qrImagePath = qrImage.path } catch (e) { wx.hideLoading(); this.hideCanvas(); util.showToast('2维码转化成不成功'); return; } // 产品互联网照片转临时性相对路径 let goodsImagePath = '/images/default_goods.png'; const goodsImage = _this.data.goodsImageUrl; if (goodsImage) { const goodsImageRes = await getWxImageInfo(goodsImage); goodsImagePath = goodsImageRes.path; } // 建立canvas var ctx = wx.createCanvasContext('qrCanvas', _this); // 设定情况 ctx.setFillStyle('#fff'); ctx.fillRect(0, 0, cvWidth, cvHeight); // 设定产品照片 产品图宽高是1样的 ctx.drawImage(goodsImagePath, 0, 0, cvWidth, cvWidth); // 设定2维码照片 ctx.drawImage(qrImagePath, qrX, qrY, qrWidth, qrWidth); // 设定店面名字 const shopName = '我是店面名字'; ctx.setFillStyle('black') ctx.setFontSize(16) ctx.setTextAlign('left') ctx.fillText(shopName, 10, shopNameY, cvWidth - qrWidth); // 设定产品名字 文本不可以换行,这里只是简易的解决了1下 const goodsName = '1个姓名很长很长的产品就问你怕不怕'; let goodsName1 = ''; let goodsName2 = ''; ctx.setFillStyle('black') ctx.setFontSize(14) ctx.setTextAlign('left') if (goodsName.length <= 10) { ctx.fillText(goodsName, 10, shopNameY + 30, cvWidth - qrWidth); } else if (goodsName.length > 10 && goodsName.length <= 22) { goodsName1 = goodsName.substring(0, 10); goodsName2 = goodsName.substring(10); ctx.fillText(goodsName1, 10, shopNameY + 30, cvWidth - qrWidth); ctx.fillText(goodsName2, 10, shopNameY + 50, cvWidth - qrWidth); } else { goodsName1 = goodsName.substring(0, 10); goodsName2 = goodsName.substring(10, 22) + '...'; ctx.fillText(goodsName1, 10, shopNameY + 30, cvWidth - qrWidth); ctx.fillText(goodsName2, 10, shopNameY + 50, cvWidth - qrWidth); } // 设定提醒 const tipText = '长按鉴别小程序流程,立刻下单!'; ctx.setFillStyle('gray') ctx.setFontSize(8) ctx.setTextAlign('center') ctx.fillText(tipText, cvWidth / 2, cvHeight - 10); // 进行 ctx.draw(false, () => { wx.hideLoading(); _this.canvasToTempFilePathFunc(cvWidth, cvHeight, 'qrCanvas') }); }, // 把当今画布特定地区的內容导出来转化成特定尺寸的照片 async canvasToTempFilePathFunc(cvWidth, cvHeight, qrCanvas) { try { let res = await canvasToTempFilePath(cvWidth, cvHeight, qrCanvas); this.setData({ canvasTempFilePath: res.tempFilePath }); } catch (error) { console.log(error); util.showToast(error.errMsg); } }, // 储存照片到当地 async saveImageToPhotosAlbumFunc() { try { let res = await saveImageToPhotosAlbum(this.data.canvasTempFilePath); console.log(res); this.hideCanvas(); util.showToast('照片储存取得成功'); } catch (err) { console.log(err); } } })
写得较为简易,由于关键是便捷自身做纪录的,因此也沒有考虑到到过量的应用情景。
总结
以上所述是网编给大伙儿详细介绍的手机微信小程序流程之html5 canvas制图并储存到系统软件相册,期待对大伙儿有一定的协助,假如大伙儿有任何疑惑欢迎给我留言,网编会立即回应大伙儿的!
Copyright © 2002-2020 html网页制作_php网页制作_网页设计稿_网页编辑工具_学生网页设计模板 版权所有 (网站地图) 粤ICP备10235580号