小程式js繪製海報
使用小程式畫布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)
-
}
-
})