小程序制作海报,保存本地

news/2024/11/30 7:56:51/
<view style='width:100%;'>
<image style='width:100%;' src="../../img/china_img.png"></image>
</view>
<view class='CONTECT'>欢迎你是神啊深刻的三大杀菌剂啥都会受到大叔大婶帝国时代干</view>
<view style='width:100%;margin-top:0.5rem;'>
<view class='CREATETIME'>2016-11-03</view>
<view class='both'></view>
</view><view class='QRCODE'>
<image style='width:100%;height:100%;' src="../../img/china_img.png"></image>
</view>
<canvas canvas-id='share' style='width:100%;height:100%;' hidden='{{canvasHidden}}'></canvas>
<view class='more' bindtap='saveImageToPhotosAlbum'>保存图片</view> 
const app = getApp();
let wx_request_zr = require('../../utils/wx_request_zr.1.0.1.js');
var that;
Page({/*** 页面的初始数据*/data: {canvasHidden: true,     //设置画板的显示与隐藏,画板不隐藏会影响页面正常显示avatarUrl: '',         //用户头像nickName: '',          //用户昵称wxappName: "端端的",    //小程序名称shareImgPath: '',screenWidth: '',       //设备屏幕宽度description: "撒的撒打算的撒打算大的",    //奖品描述FilePath: '',           //头像路径PICTURE:'../../img/timg.jpg',erweima:'../../img/aaa.jpg',ttim:"../../img/ttim.png",textn:"电话萨达哈时间快点哈时间看到黄金卡速度哈时间打哈时间看到哈萨克的哈萨克记得哈时间快点哈时间快点哈时间肯定会爱仕达多进口红酒斯卡迪哈时间快点哈时间看到哈萨克带回家啊"},onLoad: function (options) {var that = thisvar userInfo, nickName, avatarUrlconst context = wx.createCanvasContext('myCanvas')//获取用户信息,头像,昵称之类的数据wx.getUserInfo({success: function (res) {console.log(res);userInfo = res.userInfonickName = userInfo.nickNameavatarUrl = userInfo.avatarUrlthat.setData({avatarUrl: res.userInfo.avatarUrl,nickName: res.userInfo.nickName,})wx.downloadFile({url: res.userInfo.avatarUrl})}})//获取用户设备信息,屏幕宽度wx.getSystemInfo({success: res => {that.setData({screenWidth: res.screenWidth})console.log(that.data.screenWidth)}})},//定义的保存图片方法saveImageToPhotosAlbum:function () {wx.showLoading({title: '保存中...',})var that = this;//设置画板显示,才能开始绘图that.setData({canvasHidden: false})var unit = that.data.screenWidth / 375var path1 = that.data.ttim //白底var avatarUrl = that.data.avatarUrlconsole.log(avatarUrl + "头像")//var path2 = that.data.PICTUREvar path3 = that.data.erweima //二维码//var path4 = that.data.PICTUREvar path5 = that.data.PICTURE //顶图var textn = that.data.textn //描述//var unlight = "../../img/china_img.png"var nickName = that.data.nickNameconsole.log(nickName + "昵称")var context = wx.createCanvasContext('share')var description = that.data.description//var wxappName = "来「 " + that.data.wxappName + " 」试试运气"context.drawImage(path1, 0, 0, unit * 480, unit * 800)//   context.drawImage(path4, unit * 164, unit * 40, unit * 50, unit * 50)//context.drawImage(path2, unit * 48, unit * 120, unit * 280, unit * 280)context.drawImage(path5, unit * 0, unit * 0, unit * 480, unit * 160)// context.drawImage(unlight, unit * 82, unit * 145, unit * 22, unit * 32)// context.drawImage(unlight, unit * 178, unit * 145, unit * 22, unit * 32)// context.drawImage(unlight, unit * 274, unit * 145, unit * 22, unit * 32)// context.drawImage(unlight, unit * 82, unit * 240, unit * 22, unit * 32)// context.drawImage(unlight, unit * 178, unit * 240, unit * 22, unit * 32)// context.drawImage(unlight, unit * 274, unit * 240, unit * 22, unit * 32)context.drawImage(path3, unit * 20, unit * 385, unit * 80, unit * 55)//   context.drawImage(path4, 48, 200, 280, 128)context.setFontSize(14)context.setFillStyle("#999")// context.fillText(textn, unit * 10, unit * 180)context.fillText("长按识别图中二维码", unit * 120, unit * 418)//context.fillText(wxappName, unit * 90, unit * 428)//tou 删除var text = that.data.textn;//这是要绘制的文本var chr = text.split("");//这个方法是将一个字符串分割成字符串数组var temp = "";var row = [];context.setFontSize(16)context.setFillStyle("#000")for (var a = 0; a < chr.length; a++) {if (context.measureText(temp).width < 800) {temp += chr[a];}else {a--; //这里添加了a-- 是为了防止字符丢失,效果图中有对比row.push(temp);temp = "";}}row.push(temp);//如果数组长度大于2 则截取前两个if (row.length > 3) {var rowCut = row.slice(0, 3);var rowPart = rowCut[i];var test = "";var empty = [];for (var a = 0; a < rowPart.length; a++) {if (context.measureText(test).width < 380) {test += rowPart[a];}else {break;}}empty.push(test);var group = empty[i] + "..."//这里只显示两行,超出的用...表示rowCut.splice(3, 4, group);row = rowCut;}for (var b = 0; b < row.length; b++) {context.fillText(row[b], 10, 30 + b * 130, 200);}
//di//把画板内容绘制成图片,并回调 画板图片路径context.draw(false, function () {wx.canvasToTempFilePath({x: 0,y: 0,width: unit * 375,height: unit * 462.5,destWidth: unit * 480,destHeight: unit * 800,canvasId: 'share',success: function (res) {that.setData({shareImgPath: res.tempFilePath})if (!res.tempFilePath) {wx.showModal({title: '提示',content: '图片绘制中,请稍后重试',showCancel: false})}console.log(that.data.shareImgPath)//画板路径保存成功后,调用方法吧图片保存到用户相册wx.saveImageToPhotosAlbum({filePath: res.tempFilePath,//保存成功失败之后,都要隐藏画板,否则影响界面显示。success: (res) => {console.log(res)wx.hideLoading()that.setData({canvasHidden: true})},fail: (err) => {console.log(err)wx.hideLoading()that.setData({canvasHidden: true})}})}})});},})

 


http://www.ppmy.cn/news/234423.html

相关文章

欢迎使用CSDN-markdown编辑器;手残啊呜发

快捷键-加粗 按按按Ctrl B -斜体 CTRL I-引用 CTRL Q-插入链接 Ctrl L键键键-插入代码 按下按下按下Ctrl K-插入图片 按按按Ctrl G-提升标题 Ctrl H键键键-有序列表 Ctrl O键键键-无序列表 按按按Ctrl U-横线 按按按Ctrl [R-撤销按Ctrl Z键键-重做 按Ctrl Y ##减…

关于感兴趣区域和掩膜的问题!

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/JiaFanChen/article/details/51149338 本文章主要对感兴趣区域ROI的操作&#xff08;对于ROI区域图像叠加&#xff09;说明 在图像处理的领域&#xff0c;我们常常需要去设置…

python笔记:太困了,读取并显示按行业分类的股票数据提提神

先升级pip&#xff0c;要不tushare安装不上&#xff0c;使用Anaconda Prompt执行命令&#xff1a; python -m pip install --upgrade pip 再安装tushare&#xff1a; pip install tushare # -*- coding: utf-8 -*- """ Created on Tue May 7 16:16:17 2019…

Vue基础语法(样式绑定、时间处理器、表单、组件)

样式绑定 一、 class绑定 使用方式&#xff1a;v-bind:class“expression” expression的类型&#xff1a;字符串、数组、对象 二、 style绑定 v-bind:style“expression” expression的类型&#xff1a;字符串、数组、对象 样式绑定.html <!DOCTYPE html> <html&…

wxpython学习记录

写在前面的话 对于wxpython的学习我推荐三个阶段&#xff0c;第一阶段是完全无基础学习阶段&#xff0c;建议使用 UI 软件进行布局&#xff0c;第二阶段是深化阶段&#xff0c;对wxpython有了一定的基础&#xff0c;可以尝试纯代码布局&#xff0c;加深印象&#xff0c;第三阶…

扩展语法GFM ---

推展语法GFM 在众多Markdown拓展语法中&#xff0c;GFM自宫了包括表格、任务列表、删除线、围栏代码、Emoji等在内的标记语法。 删除线 语法&#xff1a;~~被删除的文字~~ 实例&#xff1a;sadawdasascassa爱仕达群翁1234565 表情符号 语法&#xff1a;:表情代码: 实例&…

day16-正则表达式和面向对象

day16-正则表达式和面向对象 一、检测类符号 1.\b - 检测是否是单词边界 单词边界&#xff1a;凡是可以将两个单词分开的符号都是单词边界&#xff0c;比如&#xff1a;空白字符、标点符号对应的字符&#xff0c;字符串开头和结尾 注意&#xff1a;检测类符号是在匹配成功的…

C语言整型提升、算术转换、操作符的属性

C语言整型提升、算术转换、操作符的属性 整型提升 C语言在进行整型算术运算时&#xff0c;总是把字符型、短整型操作数在使用之前转换为普通整型参与运算。   整型提升是按照数据类型的符号位来提升的&#xff0c;正数补0&#xff0c;负数补1。如果是无符号的数&#xff0c;高…