当前位置:网站首页>微信小程序使用canvas绘图,圆形头像,网络背景图,文字,虚线,直线
微信小程序使用canvas绘图,圆形头像,网络背景图,文字,虚线,直线
2022-07-28 20:46:00 【夏天想】
<canvas type="2d" id="shareCanvas0" style="height: 1196rpx;width:578rpx;position: fixed;z-index: -100; left: -99999rpx"></canvas>
//绘图
getCanvas(index){
var self=this;
console.log('绘图')
const query = wx.createSelectorQuery();
query.select("#shareCanvas0")
.fields({
node: true,
size: false
}).exec((res) => {
const canvas = res[0].node;
const ctx = canvas.getContext('2d');
canvas.width = 1144;
canvas.height = 2392;
self.setData({
drawing: true,
})
new Promise(function (resolve) {
// 绘制背景图片
wx.getImageInfo({
src: self.data.posterBg, //网络图片
success(res) {
const img = canvas.createImage();
img.src = res.path;
img.onload = () => {
ctx.drawImage(img, 0, 0, 1144, 2392);
resolve(true);
}
}
})
}).then(function () {
return new Promise(function(resolve){
// 绘制姓名
ctx.font ="500 56px Arial";
ctx.fillStyle = '#343434';
ctx.fillText(app.globalData.student.name, 260, 104)
resolve(true);
})
}).then(function () {
return new Promise(function(resolve){
// 绘制姓名
ctx.font ="400 40px Arial";
ctx.fillStyle = '#9A9A9A';
ctx.fillText('邀你来体验跟动体能直播课~', 256, 176)
resolve(true);
})
}).then(function () {
return new Promise((resolve)=>{
let posterimg=self.data.images[index].img;
console.log(index, self.data.images)
wx.getImageInfo({
src: posterimg, //网络图片.海报图片
success(res) {
const img = canvas.createImage();
img.src = res.path;
img.onload = () => {
ctx.drawImage(img, 36, 244, 1076, 1628);
resolve(true);
}
}
})
})
}).then(function () {
return new Promise(function(resolve){
// 获取二维码
shareQr().then(res=>{
self.setData({
base64Qr:'data:image/png;base64,' + res//base64的二维码
})
// 绘制二维码
const qrImg = canvas.createImage();
qrImg.src = self.data.base64Qr;
qrImg.onload = () => {
ctx.drawImage(qrImg, 784, 1976, 320, 320);
resolve(true);
}
qrImg.onerror = (err) => {
console.log(err);
resolve(true)
}
})
})
}).then(function () {
return new Promise(function(resolve){
// 绘制虚线
ctx.save();
ctx.setLineDash([3,6]);
ctx.lineWidth = 4;
ctx.strokeStyle = '#e3e3e3';
ctx.beginPath();
ctx.moveTo(96, 1936);
ctx.lineTo(1040, 1936);
ctx.stroke();
resolve(true);
})
}).then(function () {
return new Promise(function(resolve){
// 绘制文字
ctx.font ="500 56px Arial";
ctx.fillStyle = '#343434';
ctx.fillText('超值体验', 63.6, 2108)
ctx.fillText('元', 480, 2108)
ctx.font ="500 132px Arial";
ctx.fillStyle = '#FF7A00';
ctx.fillText('9.9', 300, 2108)
ctx.font ="500 56px Arial";
ctx.fillStyle = '#343434';
ctx.fillText('体验价值', 63.6, 2276)
ctx.font ="500 96px Arial";
ctx.fillStyle = '#FF7A00';
ctx.fillText('299', 300, 2276)
ctx.font ="500 56px Arial";
ctx.fillStyle = '#343434';
ctx.fillText('元的直播课程', 460, 2276)
resolve(true);
})
}).then(function () {
return new Promise(function(resolve){
// 设置直线的颜色
ctx.save();
ctx.beginPath();
ctx.setLineDash([3,0]);
ctx.strokeStyle = "#FF7A00";
ctx.moveTo(300, 2240);
// 设置直线的终点坐标
ctx.lineTo(460, 2240);
// 设置直线的粗细
ctx.lineWidth = "4";
// 设置直线两端的样式(线帽) butt | round | square
ctx.lineCap = "round";
// 绘制线条(通过描边的方式绘制图形)
ctx.stroke();
ctx.restore();
resolve(true);
})
}).then(function () {
// 绘制头像
return new Promise(function(resolve){// 绘制头像圆形边框
let avatar = app.globalData.student.avatar || app.oss_pic_prefix_url + "/wechat/static/student.png"
wx.getImageInfo({
src: avatar, //网络图片
success(res) {
const avatarImg = canvas.createImage();
avatarImg.src = res.path;
avatarImg.onload = () => {
ctx.save();
ctx.beginPath();
ctx.arc(124, 116, 80, 0, Math.PI * 2, false);
ctx.clip();
ctx.drawImage(avatarImg, 44, 36, 160, 160);
ctx.restore();
// console.log('绘制头像')
resolve(true);
}
}
})
})
}).then(function () {
return new Promise(function(resolve){
// console.log('绘制完毕,转换成图片')
self.transferCanvasToImage(canvas, index)
resolve(true);
})
}).catch((err) => {
console.log(err)
self.setData({
drawing: false,
})
self.data.loading && wx.hideLoading()
})
})
},//把当前画布指定区域的内容导出生成指定大小的图片
transferCanvasToImage(canvas, index) {
const self = this;
wx.canvasToTempFilePath({
canvas: canvas,
success(res){
console.log('canvas分享图片地址', res.tempFilePath)
self.data.loading && wx.hideLoading()
self.setData({
loading: false,
drawing: false
})
self.setData({ canvasImg0: res.tempFilePath})
}
})
},边栏推荐
- HCIP(11)
- hcip实验(12)
- How to establish a decentralized community in Web3
- What is a prime factor? In number theory, a prime factor (prime factor or prime factor) refers to a prime number that can divide a given positive integer
- Static details of static members
- Excel-VBA 快速上手(十三、日期的常见用法)
- JS convert numbers to letters
- Hcip seventh experiment
- HYDAC overflow valve db08a-01-c-n-500v
- MOV格式是不是静态图像文件格式
猜你喜欢

Day3 classification management of Ruiji takeout project

Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million

HYDAC overflow valve db08a-01-c-n-500v

示波器发展史中的变化

npm ERR code ETIMEDOUT npm ERR syscall connect npm ERR errno ETIMEDOUT npm ERR network reques...

Part 8: creating camera classes

Bugku, Web: all filtered

Establishment of Ruiji takeout development environment

DHCP and PPPoE protocols and packet capture analysis

Ruiji takeout - background login function development
随机推荐
成立不到一年!MIT衍生量子计算公司完成900万美元融资
【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络
Ordinary practice of JS DOM programming
HCIP第七次实验
SSH password free login
SQL injection less42 (post stack injection)
Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)
105. Construct binary tree from preorder and inorder traversal sequence (medium binary tree DFS hash table binary tree)
Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
2021 mathematical modeling group B code
Lvs+keepalived high availability deployment practical application
MySQL built-in functions
Leetcode integer exercises integer inversion
Necessary for in-depth learning: split the data set, split the labels according to the split pictures, and check the interval of all marked labels
Intranet penetration learning (III) horizontal movement of domain - planning tasks
20-09-27项目迁移到阿里折腾记录(网卡顺序导致服务无法通过haproxy连接到db)
Basic introduction of Rockwell AB PLC rslogix digital quantity IO module
MySQL installation and configuration (super detailed, simple and practical)
HCIP(13)
HYDAC overflow valve db08a-01-c-n-500v