# Canvas 绘制海报及多机型兼容

1. 兼容布局最好的方式就是同时使用 image 展示海报,将 canvas 隐藏,避免 canvas 画布大小导致显示效果欠佳,

2. 如果需要使用 canvas 组件展示的就需要在绘制的时候使用 scale 对画布进行缩放避免后期缩放导致坐标混乱

直接上代码:没有基础也能看懂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 创建 canvas 绘图上下文对象
const context = wx.createCanvasContext('screenCanvas');
//要绘制海报用到的图片
const imageUrl="/static/images/family.jpg"
//小程序二维码
const imageUrl1="/static/images/erCode.png"
// 绘制图片到canvas
context.drawImage(imageUrl, 0, 0, canvasWidth.value,canvasHeight.value*0.6)
//开始绘制线条
context.beginPath();
//定位到线条起始点
context.moveTo(0, canvasHeight.value * 0.5);
//拉一条线到中间坐标
// context.lineTo(0, canvasHeight.value * 0.5);
//绘制圆弧从x轴0.25-0.5,y轴:0.6-0.6不严谨但是效果一样弧度为300
context.arcTo(canvasWidth.value / 4, canvasHeight.value * 0.6, canvasWidth.value * 0.5, canvasHeight.value * 0.6,canvasHeight.value-canvasHeight.value * 0.6 );
//绘制圆弧从x轴0.75-1,y轴:0.6-0.5不严谨但是效果一样弧度为300
context.arcTo(canvasWidth.value * 0.75, canvasHeight.value * 0.6, canvasWidth.value, canvasHeight.value * 0.5,canvasHeight.value-canvasHeight.value * 0.6 );
// 划线从(1,0.5)开始
context.lineTo(canvasWidth.value, canvasHeight.value * 0.5);
// 划线从(1,0.5)开始-(1,1)
context.lineTo(canvasWidth.value, canvasHeight.value);
// 划线从(1,1)-(0,1)
context.lineTo(0, canvasHeight.value);
// 划线从(0,1)-(0,0.5)
context.lineTo(0, canvasHeight.value*0.5);
//绘制完成设置填充色
context.fillStyle = "rgb(255, 128, 0)";
//开始填充
context.fill();
context.closePath()
//判断缩放比例二维码
const a=canvasWidth.value/710
const b=canvasHeight.value/674
// 对比比例哪个小就以哪个为标准缩放
const scX=a>b?b:a
//这里缩放后除以2是为了以基准宽的0.5作为二维码宽度
const width=710*scX/2
const height=674*scX/2
//动态绘制二维码到指定位置与canvas的比例可能发生改变但是不会发生形变
context.drawImage(imageUrl1,0, 0,710,674, canvasWidth.value / 2 -width/2, canvasHeight.value*0.65-height/2, width, height);
context.closePath();
//开始绘制文字
context.beginPath();
//设置颜色
context.fillStyle = "rgb(255, 85, 0)";
// 设置字体样式
context.font='italic bold 33px Microsoft Yahei'
//字体内容
context.fillText('速达·陪诊', canvasWidth.value / 10, canvasHeight.value/2.6)
context.font='italic bold 30px Microsoft Yahei'
context.fillText('快速·必达', canvasWidth.value / 2, canvasHeight.value/2.2)
context.closePath();
context.fillStyle = "rgb(255, 255, 255)";
context.font='18rpx Microsoft Yahei'
//动态可修改文字内容
const len=btText1.value.length/2
context.fillText(btText1.value, canvasWidth.value /2-20*len, canvasHeight.value-50)
context.font="18rpx Microsoft Yahei"
context.fillText(btText2.value, canvasWidth.value / 2-20*len, canvasHeight.value-25)
//绘制并保留上面的内容
context.draw(true)
// 开始绘制
context.draw(true, function() {
// 将 canvas 保存为临时图片
wx.canvasToTempFilePath({
canvasId: 'screenCanvas',
destWidth: canvasWidth.value,
destHeight: canvasHeight.value,
success: function (res) {
tempFilePath.value = res.tempFilePath;
wx.showToast({
title: '生成成功',
icon: 'success',
duration: 2000
})
},
fail: function (res) {
wx.showToast({
title: '生成失败',
icon: 'none',
duration: 2000
})
}
})
});
}


兼容

因为华为系统比较特殊有一些原生的 api 是不支持的所以展现效果会不一样

1
2
3
4
5
6
7
8
9
10
// 获取屏幕宽高并根据设计稿动态的改变canvas的宽高
wx.getSystemInfo({
success: (res) => {
scale.value=Math.min(res.screenWidth / 375, res.screenHeight / 603);
drp.value=res.pixelRatio
canvasWidth.value=Math.floor(375*scale.value)
canvasHeight.value=Math.floor(603*scale.value)
isHuawei.value=res.brand=="HUAWEI"
}
})

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Xiao Yang Guo 微信支付

微信支付

Xiao Yang Guo 支付宝

支付宝

Xiao Yang Guo 贝宝

贝宝