小程序頁(yè)面生成圖片保存分享——筆記

效果圖:


canvas生成
導(dǎo)出圖片

wxml:

<canvas bindtap='click' canvas-id='canvas' class='share' style='width:{{screenWidth}}px;height:{{screenHeight -64 }}px' hidden='{{canvasHidden}}'></canvas>

<cover-view? class='canvas-save' bindtap='saveImageToPhotosAlbum' hidden='{{canvasHidden}}' > <button> 保存到手機(jī)</button></cover-view >?

css:

.share{ position: fixed; z-index: 1111; background-color: #f2f2f2; left: 0px; top: 0px}

.canvas-save{position:fixed; z-index: 9999; bottom: 100rpx; text-align: center;width: 100%}

.canvas-save button{ background-color: #3998F7; color: #fff; width: 300rpx; height: 80rpx; display: inline-block; font-size: 30rpx}

js:

var project_name;

var pm10;

var air_quality;

var dust_time;

var screenWidth;

var screenHeight;

var drawing = false; /*避免多次點(diǎn)擊保存按鈕*/

var imgFilePath;

Page({

? data: {

? ? canvasHidden: true,

? ? code:''

? },

? //頁(yè)面加載處理函數(shù)

? onLoad: function (options) {

? ? const that = this;

? ? var id = options.id

? ? if (id) {

? ? ? wx.request({

? ? ? ? url: '數(shù)據(jù)請(qǐng)求地址',

? ? ? ? data: { 'id': id },

? ? ? ? header: {

? ? ? ? ? 'content-type': 'application/x-www-form-urlencoded'

? ? ? ? },

? ? ? ? method: 'POST',

? ? ? ? // 請(qǐng)求成功后執(zhí)行

? ? ? ? success: function (res) {

? ? ? ? ? project_name = res.data.content.project_name;

? ? ? ? ? pm10 = res.data.content.pm10;

? ? ? ? ? air_quality = res.data.content.air_quality;

? ? ? ? ? dust_time = res.data.content.dust_time;


? ? ? ? ? //獲取用戶(hù)設(shè)備信息,屏幕寬度

? ? ? ? ? wx.getSystemInfo({

? ? ? ? ? ? success: res => {

? ? ? ? ? ? ? that.setData({

? ? ? ? ? ? ? ? screenWidth: res.screenWidth,

? ? ? ? ? ? ? ? screenHeight: res.screenHeight

? ? ? ? ? ? ? })

? ? ? ? ? ? ? screenWidth = res.screenWidth;

? ? ? ? ? ? ? screenHeight = res.screenHeight -64;//減去微信頭部高度

? ? ? ? ? ? }

? ? ? ? ? })


? ? ? ? ? wx.showLoading({

? ? ? ? ? ? title: '圖片生成中',

? ? ? ? ? })

? ? ? ? ? that.setData({

? ? ? ? ? ? canvasHidden: false,

? ? ? ? ? ? code: res.data.code,

? ? ? ? ? })

? ? ? ? ? that.canvasdraw();

? ? ? ? }

? ? ? })

? ? } else {

? ? ? wx.showToast({

? ? ? ? title: '參數(shù)錯(cuò)誤,請(qǐng)重試',

? ? ? ? icon: 'none',

? ? ? })

? ? }

? },

? // 繪制canvas

? canvasdraw: function (share) {

? ? let that = this;

? ? let canvas = wx.createCanvasContext('canvas');

? ? // 處理文字居于中心位置

? ? var imgw = screenWidth / 2 - 58;

? ? var qw = screenWidth / 2 - 35;

? ? var aw = screenWidth / 2 + 40;

? ? var time = screenWidth / 2 - 45;

? ? var area = screenWidth / 2 + 50;

? ? var circle = "../../images/circle.png";

? ? var redcircle = "../../images/redcircle.png";

? ? var bg = "../../images/bg.png";

? ? var bgh = screenHeight - 100;

? //下載小程序碼到本地

? ? wx.downloadFile({

? ? ? url: that.data.code,

? ? ? success: function (msg) {


? ? ? ? if (msg.statusCode === 200) {

? ? ? ? ? if (pm10>100) {

? ? ? ? ? ? // 繪制pm10

? ? ? ? ? ? canvas.setFontSize(26)

? ? ? ? ? ? canvas.setFillStyle("#f00")

? ? ? ? ? ? canvas.textAlign = 'center';

? ? ? ? ? ? canvas.fillText(pm10, screenWidth / 2, 160)

? ? ? ? ? ? // 繪制圓形圖

? ? ? ? ? ? canvas.drawImage(redcircle, imgw, 100, 120, 100)

? ? ? ? ? ? // 繪制空氣質(zhì)量

? ? ? ? ? ? canvas.setFontSize(13)

? ? ? ? ? ? canvas.setFillStyle("#f00")

? ? ? ? ? ? canvas.fillText(air_quality, aw, 225)

? ? ? ? ? }

? ? ? ? ? else{

? ? ? ? ? ? // 繪制pm10

? ? ? ? ? ? canvas.setFontSize(26)

? ? ? ? ? ? canvas.setFillStyle("#1A7CDE")

? ? ? ? ? ? canvas.textAlign = 'center';

? ? ? ? ? ? canvas.fillText(pm10, screenWidth / 2, 160)

? ? ? ? ? ? // 繪制圓形圖

? ? ? ? ? ? canvas.drawImage(circle, imgw, 100, 120, 100)

? ? ? ? ? ? // 繪制空氣質(zhì)量

? ? ? ? ? ? canvas.setFontSize(13)

? ? ? ? ? ? canvas.setFillStyle("#05950F")

? ? ? ? ? ? canvas.fillText(air_quality, aw, 225)


? ? ? ? ? }

? ? ? ? ? // 繪制項(xiàng)目名稱(chēng)

? ? ? ? ? canvas.setFontSize(17)

? ? ? ? ? canvas.setFillStyle("#000")

? ? ? ? ? canvas.textAlign = 'center';

? ? ? ? ? canvas.fillText(project_name, screenWidth / 2, 60)


? ? ? ? ? // 繪制pm10

? ? ? ? ? canvas.setFontSize(13)

? ? ? ? ? canvas.setFillStyle("#666")

? ? ? ? ? canvas.fillText('pm10', screenWidth / 2, 180)

? ? ? ? ? // 繪制空氣質(zhì)量

? ? ? ? ? canvas.setFontSize(13)

? ? ? ? ? canvas.setFillStyle("#666")

? ? ? ? ? canvas.fillText('空氣質(zhì)量', qw, 225)


? ? ? ? ? // 繪制時(shí)間

? ? ? ? ? canvas.setFontSize(13)

? ? ? ? ? canvas.setFillStyle("#666")

? ? ? ? ? canvas.textAlign = 'center';

? ? ? ? ? canvas.fillText(dust_time, screenWidth / 2, 250)


? ? ? ? ? // 繪制背景

? ? ? ? ? canvas.drawImage(bg, 0, bgh, screenWidth, 120)

? ? ? ? ? // 繪制線(xiàn)條

? ? ? ? ? canvas.setStrokeStyle('#ccc')

? ? ? ? ? canvas.strokeRect(40, 280, screenWidth - 80, 160)

? ? ? ? ? //繪制提示

? ? ? ? ? canvas.setFontSize(14);

? ? ? ? ? canvas.fillStyle = "#999999";

? ? ? ? ? canvas.setTextAlign('center');

? ? ? ? ? canvas.fillText('長(zhǎng)按識(shí)別小程序', screenWidth / 2, 420);

? ? ? ? ? //繪制小程序碼

? ? ? ? ? canvas.drawImage(msg.tempFilePath, screenWidth / 2 - 55 , 290 , 110, 110)

? ? ? ? ? canvas.draw(true, setTimeout(function () {

? ? ? ? ? ? that.daochu()

? ? ? ? ? }, 1000));

? ? ? ? }else{

? ? ? ? ? wx.showToast({

? ? ? ? ? ? title: '圖片生成錯(cuò)誤!',

? ? ? ? ? ? icon: 'none',

? ? ? ? ? ? duration: 2000

? ? ? ? ? })

? ? ? ? }

? ? ? }

? ? })

? },

? //導(dǎo)出生成指定大小的圖片

? daochu: function () {

? ? var that = this;

? ? wx.canvasToTempFilePath({

? ? ? x: 0,

? ? ? y: 0,

? ? ? width: screenWidth,

? ? ? height: screenHeight,

? ? ? destWidth: screenWidth * 3,? //3倍像素? 更清晰

? ? ? destHeight: screenHeight * 3,

? ? ? canvasId: 'canvas',

? ? ? fileType: 'jpg',//設(shè)置圖片類(lèi)型,否則到處圖片無(wú)背景色

? ? ? success: function (res) {

? ? ? ? imgFilePath = res.tempFilePath

? ? ? ? wx.hideLoading();

? ? ? ? //that.saveImageToPhotosAlbum(res.tempFilePath)

? ? ? },

? ? ? fail: function (err) {

? ? ? ? console.log(err)

? ? ? }

? ? })

? },

? //保存至相冊(cè)

? saveImageToPhotosAlbum: function () {

? ? console.log(imgFilePath)

? ? wx.saveImageToPhotosAlbum({

? ? ? filePath: imgFilePath,

? ? ? success: (res) => {

? ? ? ? wx.showToast({

? ? ? ? ? title: '保存成功',

? ? ? ? ? icon: 'success',

? ? ? ? ? duration: 2000

? ? ? ? })

? ? ? ? drawing = false

? ? ? },

? ? ? fail: (err) => {

? ? ? ? wx.showToast({

? ? ? ? ? title: '保存失敗',

? ? ? ? ? icon: 'none',

? ? ? ? ? duration: 2000

? ? ? ? })

? ? ? ? drawing = false

? ? ? }

? ? })

? },

? // 點(diǎn)擊圖片預(yù)覽

? click: function () {

? ? wx.previewImage({

? ? ? urls: [imgFilePath],

? ? })

? }

});

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容