微信小程序用canvas畫(huà)圖并分享
最近開(kāi)始做微信小程序,有這樣一個(gè)需求:
從列表頁(yè)進(jìn)入詳情,在每一個(gè)詳情頁(yè)面去分享,分享出來(lái)的圖片是帶有當(dāng)前詳情數(shù)據(jù)的圖片
如下圖的列表:

分享出來(lái)的樣子:

解決方案和思路:canvas畫(huà)圖生成圖片
上代碼:
【html部分】
<canvas style='width:{{canvasWidth}}px;height:{{canvasHeight}}px' canvas-id='myCanvas'></canvas>
<button open-type='share'>分享</button>
【js部分】
var ctx = "" // 用于獲取canvas
var leftMargin = "" //文字距離左邊邊距
var topMargin = "" //文字距離右邊邊距
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
title: '人人車(chē)司機(jī)',
salary: '500-8000元/月',
rtype: '日結(jié)',
rmoney: '20元',
canvasWidth: '', // canvas寬度
canvasHeight: '', // canvas高度
imagePath: '' // 分享的圖片路徑
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
var that = this
var sysInfo = wx.getSystemInfo({
success: function (res) {
that.setData({
//設(shè)置寬高為屏幕寬,高為屏幕高的80%,因?yàn)槲臋n比例為5:4
canvasWidth: res.windowWidth,
canvasHeight: res.windowWidth * 0.8
})
leftMargin = res.windowWidth
topMargin = res.windowWidth * 0.8
},
})
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面初次渲染完成
*/
onReady: function () {
ctx = wx.createCanvasContext('myCanvas')
this.addImage()
this.tempFilePath()
},
//畫(huà)背景圖
addImage: function () {
var context = wx.createContext();
var that = this;
var path = "/images/share.jpg";
//將模板圖片繪制到canvas,在開(kāi)發(fā)工具中drawImage()函數(shù)有問(wèn)題,不顯示圖片
//不知道是什么原因,手機(jī)環(huán)境能正常顯示
ctx.drawImage(path, 0, 0, this.data.canvasWidth, this.data.canvasHeight);
this.addTitle()
this.addRtype()
this.addRmoney()
this.addSalary()
ctx.draw()
},
//畫(huà)標(biāo)題
addTitle: function (){
var str = this.data.title
ctx.font = 'normal bold 20px sans-serif';
ctx.setTextAlign('center'); // 文字居中
ctx.setFillStyle("#222222");
ctx.fillText(str, this.data.canvasWidth/2,65)
},
// 畫(huà)返費(fèi)方式
addRtype: function () {
var str = this.data.rtype
ctx.setFontSize(16)
ctx.setFillStyle("#ff4200");
ctx.setTextAlign('left');
ctx.fillText(str, leftMargin * 0.35, topMargin * 0.4)
},
// 畫(huà)返費(fèi)金額
addRmoney: function () {
var str = this.data.rmoney
ctx.setFontSize(16)
ctx.setFillStyle("#222");
ctx.setTextAlign('left');
ctx.fillText(str, leftMargin * 0.35, topMargin * 0.5)
},
// 畫(huà)薪資
addSalary: function () {
var str = this.data.salary
ctx.setFontSize(16)
ctx.setFillStyle("#222");
ctx.setTextAlign('left');
ctx.fillText(str, leftMargin * 0.35, topMargin * 0.61)
},
/**
* 用戶(hù)點(diǎn)擊右上角分享
*/
onShareAppMessage: function (res) {
// return eventHandler接收到的分享參數(shù)
return {
title: this.data.title,
path: '/pages/test/test',
imageUrl: this.data.imagePath
};
},
//導(dǎo)出圖片
tempFilePath: function(){
let that = this;
wx.canvasToTempFilePath({
canvasId: 'myCanvas',
success: function success(res) {
wx.saveFile({
tempFilePath: res.tempFilePath,
success: function success(res) {
that.setData({
imagePath: res.savedFilePath
});
}
});
}
});
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載
*/
onUnload: function () {
},
/**
* 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶(hù)下拉動(dòng)作
*/
onPullDownRefresh: function () {
},
/**
* 頁(yè)面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
}
})
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開(kāi)發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Firefox下設(shè)為主頁(yè)的JavaScript代碼
在IE下,我們可以輕松使用以下代碼來(lái)實(shí)現(xiàn)設(shè)置主頁(yè) 但在firefox我們需要做的更多2008-09-09
JavaScript獲取Excel表格的列序號(hào)和列名
這篇文章主要介紹了JavaScript獲取Excel表格的列序號(hào)和列名,在處理Excel文件時(shí),通常要獲取xx列的數(shù)據(jù),這就要求先找到列序號(hào),下文關(guān)于列名獲取需要的小伙伴可以參考一下2022-05-05
js判斷所有表單項(xiàng)不為空則提交表單的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇js判斷所有表單項(xiàng)不為空則提交表單的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的, 現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09
Bootstrap Metronic完全響應(yīng)式管理模板學(xué)習(xí)筆記
這篇文章主要為大家分享了Bootstrap Metronic完全響應(yīng)式管理模板學(xué)習(xí)筆記,感興趣的小伙伴們可以參考一下2016-07-07
js仿京東輪播效果 選項(xiàng)卡套選項(xiàng)卡使用
這篇文章主要為大家詳細(xì)介紹了js仿京東輪播效果,選項(xiàng)卡里套選項(xiàng)卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
JS實(shí)現(xiàn)數(shù)組去重,顯示重復(fù)元素及個(gè)數(shù)的方法示例
這篇文章主要介紹了JS實(shí)現(xiàn)數(shù)組去重,顯示重復(fù)元素及個(gè)數(shù)的方法,涉及javascript數(shù)組遍歷、統(tǒng)計(jì)、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
JS實(shí)現(xiàn)線性表的鏈?zhǔn)奖硎痉椒ㄊ纠窘?jīng)典數(shù)據(jù)結(jié)構(gòu)】
這篇文章主要介紹了JS實(shí)現(xiàn)線性表的鏈?zhǔn)奖硎痉椒?簡(jiǎn)單講解了線性表鏈?zhǔn)奖硎镜脑聿⒔Y(jié)合實(shí)例形式分析了js針對(duì)線性表鏈?zhǔn)奖硎镜膭?chuàng)建、插入、刪除等節(jié)點(diǎn)操作技巧,需要的朋友可以參考下2017-04-04

