1 . 效果图大概就是下面的样子,里面的内容可以按照需要添加
下面直接代码
- html
<button bindtap='gotoSubmit'>保存到相册</button>
<!-- 弹框 -->
<view class='container1' bindtap="previewImg">
<image style="width:100%;" src="{{imagePath}}" ></image>
</view>
<canvas style="width:{{420}}px;height:{{720}}px;}}" canvas-id="mycanvas" />
3 .wxss
page{
height: calc(100% - 30rpx);
width: calc(100% - 80rpx);
padding: 30rpx 40rpx 0;
background: #f1f1f1
}
button{
width: calc(100% - 80rpx);
position: fixed;
z-index: 1;
bottom:60rpx;
background: #e4393c;
color: #fff;
height: 88rpx;
}
.container1{
height: 900rpx;
width: 600rpx;
background-color:#FFF;
border-radius: 30rpx;
margin: 0rpx auto;
}
.container1 image{
width: 100%;
height: 100%;
border-radius: 30rpx;
background: #fff
}
canvas{
position: fixed;
top: 9999rpx;
}
4 js
Page({
data: {
showhaibao:false,//隐藏显示
maskHidden: true,//隐藏显示
bendiImg:'/images/logo.png',//本地图片
ercodeImg:'/images/erweima.png',//二维码图片
userBg:'/images/userbg.png',
// 屏幕可用宽高
windowWidth: wx.getSystemInfoSync().windowWidth,
windowHeight: wx.getSystemInfoSync().screenHeight,
// 图片预览本地文件路径
previewImageUrl: null
},
onLoad: function (options) {
// 此处获取设备的宽高。canvas绘制的图片以次宽高为准
var _this = this;
wx.getSystemInfo({
success: function (res) {
console.log(res)
_this.setData({
windowW: res.windowWidth,
windowH: res.screenHeight,
screenWidth: res.screenWidth ,
pixelRatio: res.pixelRatio
})
},
})
wx.getUserInfo({ //获取微信用户信息
success: function (res) {
_this.getImageInfo(res.userInfo.avatarUrl); // 调取图片处理方法
_this.setData({
userName: res.userInfo.nickName
});
}
});
// 网络图片必须是要下载的
wx.downloadFile({
url: 网络图片地址必须要在小程序中配备合法域名,
success: function (res1) {
console.log(res1.tempFilePath)
//缓存商品图片
_this.setData({
img1: res1.tempFilePath
})
}
})
},
onReady: function () {
// 页面渲染完成
this.createNewImg();
//创建初始化图片
},
onShow:function(){
var _this = this;
this.setData({
maskHidden: false,
showhaibao: true
})
wx.showToast({
title: '图片生成中...',
icon: 'loading',
duration: 2000
});
setTimeout(function () {
wx.hideToast()
_this.createNewImg();
}, 2000)
},
getImageInfo:function(url) {
var that = this; // 图片缓存本地的方法
if (typeof url === 'string') {
wx.getImageInfo({ // 小程序获取图片信息API
src: url,
success: function (res) {
that.setData({
head_img: res.path
})
},
fail(err) {
console.log(err)
}
})
}
},
//将金额绘制到canvas的固定
setMoney: function (context) {
var name = 'ONLY2018夏季新款蕾丝短袖连衣裙ONLY2018夏季新款蕾丝短袖连衣裙';//商品标题
if (name.length>40){
var icon = '¥';
context.setFontSize(14);
context.setFillStyle("#e4393c");
context.fillText(icon, 20, 150);
context.stroke();
var money = '29.9';//价格放在这
context.setFontSize(20);
context.setFillStyle("#e4393c");
context.fillText(money, 35, 150);
context.stroke();
}else{
var icon = '¥';
context.setFontSize(14);
context.setFillStyle("#e4393c");
context.fillText(icon, 20, 110);
context.stroke();
var money = '29.9';//价格放在这
context.setFontSize(20);
context.setFillStyle("#e4393c");
context.fillText(money, 35, 110);
context.stroke();
}
},
//将说明绘制到canvas固定
setSuoming: function (context) {
var Suoming = "电商版云服务"
context.setFontSize(16);
context.setFillStyle("#e4393c");
context.fillText(Suoming, 20, 650);
context.stroke();
},
//将说明2绘制到canvas固定
setSuoming1: function (context) {
var Suoming = "扫一扫进入小程序查看更多信息";
context.setFontSize(16);
context.setFillStyle("#666");
context.fillText(Suoming, 20, 680);
context.stroke();
},
// 绘制用户名
username: function (context) {
var username = '用户名';
context.setFontSize(16);
context.setFillStyle("#333");
context.fillText(username, 90, 550);
context.stroke();
},
//将标题绘制到canvas的固定
setName: function (context) {
var name = "ONLY2018夏季新款蕾丝短袖连衣裙ONLY2018夏季新款蕾丝短袖连衣裙";
if (name.length>40){
var name1 = name.substr(0, 23);
var name2 = name.substr(22,44);
var name3 = name.substr(44);
console.log(name1, name2)
context.setFontSize(18);
context.setFillStyle("#333333");
context.fillText(name1, 20, 40);
context.stroke();
context.setFontSize(18);
context.setFillStyle("#333333");
context.fillText(name2, 20, 70);
context.stroke();
context.setFontSize(18);
context.setFillStyle("#333333");
context.fillText(name3, 20, 70);
context.stroke();
}else{
var name1 = name.substr(0, 23);
var name2 = name.substr(23);
context.setFontSize(19);
context.setFillStyle("#333333");
context.fillText(name1, 20, 40);
context.stroke();
context.setFontSize(19);
context.setFillStyle("#333333");
context.fillText(name2, 20, 70);
context.stroke();
}
},
// 绘制头像
drawHead: function (context){
let that = this;
let path1 = that.data.poster_head; //用户头像
ctx.arc(70, 60, 32, 0, 2 * Math.PI) //画圆
ctx.setFillStyle('#fff')
ctx.fill();
ctx.clip();
ctx.drawImage(path1, 38, 28, 64, 64)
},
//将canvas转换为图片保存到本地,然后将图片路径传给image图片的src
createNewImg: function () {
var _this = this;
var name = "ONLY2018夏季新款蕾丝短袖连衣裙ONLY2018夏季新款蕾丝短袖连衣裙";
if(name.length>40){
var axesY = 180
}else{
var axesY = 130
}
var context = wx.createCanvasContext("mycanvas");
context.setFillStyle('#FFF')
context.fillRect(0, 0, this.data.windowWidth, this.data.windowHeight)
var path = _this.data.bendiImg; //详细看onLoad函数注释部分这里放你的本地图片路径,或者网络图片缓存在本地的路径
var path1 = _this.data.ercodeImg;//二维码图片
var path2 = _this.data.userBg;//用户背景图
context.drawImage(path, 20, axesY, 370, 370); //这里是商品图片
this.setSuoming(context);
this.setName(context);
this.setMoney(context);
this.setSuoming1(context);
context.drawImage(path1, 285, 610, 100, 100);//这里是二维码图片
context.drawImage(path2, 20, 520, 370, 50);//用户背景图
this.drawHead(context); //用户头像
this.username(context);
context.save(); //保存绘图上下文。
context.beginPath(); //开始创建一个路径
context.arc(45, 545, 25, 0, 2 * Math.PI);
context.clip(); //从原始画布中剪切任意形状和尺寸
context.drawImage(path, 20, 520, 50, 50); //t头像放在这
context.restore(); //恢复之前保存的绘图上下文
context.draw(false, () => { that.getImg()}, 500) //将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
},
//将生成好的图片保存到本地 下面这句注释是文档中的原话。
// tip: 在 draw 回调里调用canvasToTempFilePath方法才能保证图片导出成功。
getImg() {
var _this = this;
// 获取宽高
let wW = _this.data.windowWidth;
let wH = _this.data.windowHeight;
wx.canvasToTempFilePath({
width: 420, //canvas原本的大小
heght: 720,
destWidth: 420*2 ,
destHeight:720*2,
canvasId: 'mycanvas',
// fileType: 'png',
quality: 1,
success: function success(res) {
_this.setData({
imagePath: res.tempFilePath,
});
}
});
},
//点击图片进行预览,长按保存分享图片
previewImg: function (e) {
var img = this.data.imagePath
wx.previewImage({
current: img, // 当前显示图片的http链接
urls: [img] // 需要预览的图片http链接列表
})
},
gotoSubmit: function (e) {
var _this = this;
wx.saveImageToPhotosAlbum({
filePath: _this.data.imagePath,
success(res) {
//保存成功
console.log(res);
},
fail: function (res) {
wx.showToast({
icon: 'fail',
title: 'sorry 保存失败,请稍后再试',
})
return;
}
})
// this.setData({
// maskHidden: false,
// showhaibao: true
// })
// wx.showToast({
// title: '图片生成中...',
// icon: 'loading',
// duration: 2000
// });
// setTimeout(function () {
// wx.hideToast()
// _this.createNewImg();
// }, 2000)
}
})