博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信小程序-生成分享海报
阅读量:3935 次
发布时间:2019-05-23

本文共 2316 字,大约阅读时间需要 7 分钟。

实现主页面点击分享按钮,跳转至海报预览和保存页面。

分享按钮点击事件代码:

// shareImage为图片网络地址,跳转页面是作为参数传递  creatPoster(){
// 每日英语生成海报 const shareImage=this.data.shareImage if(shareImage){
wx.navigateTo({
url: '../poster/poster?shareImage=' + shareImage, }) } },

海报生成页面代码:

.wxml

保存图片

.json

{
"navigationBarTitleText": "今日英语海报", "usingComponents": {
}}

.js

Page({
data: {
shareImgPath:'', posterUrl:'', qrcode:'../../assets/images/qrcode.jpg', }, onLoad: function (data) {
this.setData({
shareImgPath:data.shareImage }, ()=>{
var query = wx.createSelectorQuery() query.select('#canvasPoster').boundingClientRect((res) => {
// 返回值包括画布的实际宽高 this.drawImage(res) }).exec() }) }, drawImage (canvasAttrs){
var context = wx.createCanvasContext('canvasPoster',this) let canvasW = canvasAttrs.width // 画布的真实宽度 let canvasH = canvasAttrs.height //画布的真实高度 context.fillStyle="#FFFFFF"; context.fillRect(0,0,canvasW,canvasH); context.drawImage(this.data.shareImgPath, 10, 10, 290, 380) context.save() // 绘制二维码 context.drawImage(this.data.qrcode, 190, 400, 80, 80) context.save() context.setFillStyle('#333333') context.setFontSize(14) // 文字字号:14px context.fillText('初云小程序', 92, 428); context.setFillStyle('#666666') context.setFontSize(10) context.fillText('by: ruimin.tan', 96, 442); context.setFillStyle('#666666') context.setFontSize(12) context.fillText('长按识别二维码', 76, 468); context.draw() setTimeout(() => {
const that=this wx.canvasToTempFilePath({
canvasId: 'canvasPoster', success: (res) => {
const canvasToTempFilePath = res.tempFilePath // 返回的图片地址保存到一个全局变量里 that.setData({
posterUrl: canvasToTempFilePath }) } }) }, 200) }, // 保存图片 savePoster () {
if (this.data.posterUrl) {
wx.saveImageToPhotosAlbum({
filePath: this.data.posterUrl, success: (result) => {
wx.showToast({
title: '海报已保存,快去分享给好友吧。', icon: 'none' }) } }) } }})

canvas绘制过程需要细微调整参数,详情参见

小程序现没提供转发API,所以我们需要先保存图片至手机,再发至朋友圈。

扫下方二维码查看小程序:

在这里插入图片描述

转载地址:http://dwrgn.baihongyu.com/

你可能感兴趣的文章
the jacobian matrix and the gradient matrix
查看>>
VS2010 将背景设为保护色
查看>>
ubutun里面用命令行安装软件
查看>>
ubuntu 常用命令
查看>>
SQLite Tutorial 4 : How to export SQLite file into CSV or Excel file
查看>>
how to move pivot to origin
查看>>
Optimizate objective function in matrix
查看>>
Convert polygon faces to triangles or quadrangles
查看>>
How do I divide matrix elements by column sums in MATLAB?
查看>>
read obj in matlab
查看>>
find out the neighbour matrix of a mesh
查看>>
Operators and special characters in matlab
查看>>
As-Conformal-As-Possible Surface Registration
查看>>
qmake Variable Reference
查看>>
Lesson 2 Gradient Desent
查看>>
find border vertex
查看>>
matlab sliced variable
查看>>
create symbolic array
查看>>
TAUCS库的编译(vs2010)
查看>>
color vector using in plotting example points and lines between corresponding vertices
查看>>