使用小程序画布js代码如下:
-
onLoad: function (options) {
-
var that = this;
-
//1. 请求后端API生成小程序码
-
//that.getQr();
-
-
//2. canvas绘制文字和图片
-
const ctx = wx.createCanvasContext('myCanvas');
-
var imgPath = '../../../image/intro.jpg';
-
var bgImgPath = '../../../image/bgImgPath.jpg';
-
var basicprofile ='../../../image/basicprofile.png';
-
var xcxcode ="http://localhost/mining120/public/static/xcxcode/29-5.jpeg";
-
-
//填充背景
-
ctx.setFillStyle('#cccccc');
-
ctx.fillRect(0, 0, 240, 360);
-
ctx.setFillStyle('#ffffff');
-
ctx.fillRect(1, 1, 238, 358);
-
-
//绘制产品图
-
ctx.drawImage(imgPath, 2, 2, 236, 200);
-
-
//绘制标题
-
ctx.setFontSize(16);
-
ctx.setFillStyle('#000000');
-
ctx.fillText('圆通删掉贷方金额', 10, 225);
-
-
//绘制介绍产品
-
ctx.setFontSize(12);
-
ctx.setFillStyle('#6F6F6F');
-
ctx.fillText('宠友们快来围观萌宠靓照我在萌爪幼稚园', 10, 250);
-
ctx.fillText('我在萌爪幼稚园', 10, 270);
-
-
//绘制一条虚线
-
-
ctx.strokeStyle = 'blue';
-
ctx.beginPath();
-
ctx.setLineWidth(1);
-
ctx.setLineDash([2, 4]);
-
ctx.moveTo(10, 287);
-
ctx.lineTo(230, 287);
-
ctx.stroke();
-
-
//绘制矿业人图标
-
ctx.drawImage(basicprofile, 10, 310, 30, 30);
-
-
//绘制介绍
-
ctx.setFontSize(11);
-
ctx.setFillStyle('#aaaaaa');
-
ctx.fillText('长按扫码查看详情', 47, 318);
-
ctx.fillText('分享自矿业人小程序', 47, 338);
-
ctx.drawImage(xcxcode, 165, 295, 60, 60);
-
-
ctx.draw();
-
-
},
wxml代码如下:
-
<canvas style="margin:0 auto;margin-bottom:30px;width: 240px; height: 360px;box-shadow:1px 2px 2px #ccc;border-raidus:1px;" canvas-id="myCanvas"></canvas>
-
-
<view class='ft'>保存图片到手机相册,分享到朋友圈</view>
-
<view style="text-align:center;padding-bottom:20px"><button bindtap='savetup' type="primary" size='mini'>保存到相册</button></view>
-
</view>
保存图片代码:
-
savetup:function(){
-
var that = this;
-
wx.canvasToTempFilePath({
-
x: 0,
-
y: 0,
-
width: 240,
-
height: 360,
-
destWidth: 240,
-
destHeight: 360,
-
canvasId: 'myCanvas',
-
success: function (res) {
-
//调取小程序当中获取图片
-
console.log(res.tempFilePath);
-
wx.saveImageToPhotosAlbum({
-
filePath: res.tempFilePath,
-
success(res) {
-
wx.showModal({
-
title: '存图成功',
-
content: '图片成功保存到相册了,去发圈噻~',
-
showCancel: false,
-
confirmText: '好哒',
-
confirmColor: '#72B9C3',
-
success: function (res) {
-
if (res1.confirm) {
-
console.log('用户点击确定');
-
}
-
}
-
})
-
}
-
})
-
},
-
fail: function (res) {
-
console.log(res)
-
}
-
})
-
转载地址: https://blog.csdn.net/qq_37375882/article/details/79419596
|
请发表评论