微信小程序要实现使用canvas绘制一个图,然后保存到手机相册
**最终效果:**实现生成以下图片
一、初始化canvas
<canvas style="width: {{windowW}}px; height: {{windowH}}px;" disable-scroll='true' canvas-id="myCanvas" wx:if="{{showCanvas}}"></canvas>
data: {
ctx: '',
details: '',
windowW: 375*3,
windowH: 265*3,
ratio:3,
showCanvas:false
},
onLoad(options) {
this.setData({
ctx: wx.createCanvasContext('myCanvas'),
showCanvas:true
})
},
二、绘制canvas
getsaves(){
var that = this;
let rat = 3;
that.data.ctx.setFontSize(8*rat);
that.data.ctx.setFillStyle('#080808');
that.data.ctx.fillText('山东省第六届中小学生作文大赛决赛', 28*rat, 29*rat);
that.data.ctx.setFontSize(8*rat);
that.data.ctx.fillText('准考证', 81*rat, 59*rat);
that.data.ctx.setFontSize(6*rat);
that.data.ctx.fillText('考试时间:' + '10月18日(周五)上午9:00-11:00', 35*rat, 78*rat);
that.data.ctx.fillText('考试地点:山东师范大学附属小学', 35*rat, 93*rat);
that.data.ctx.fillText('济南市历下区山师北街1号', 66*rat, 104*rat);
that.data.ctx.setFillStyle('#444444');
that.data.ctx.fillText('准考证号', 43*rat, 128*rat);
that.data.ctx.fillText('考生姓名', 43*rat, 144*rat);
that.data.ctx.fillText('考生学校', 43*rat, 160*rat);
that.data.ctx.fillText('考生组别', 43*rat, 176*rat);
that.data.ctx.fillText('考场号', 43*rat, 192*rat);
that.data.ctx.fillText('座位号', 43*rat, 208*rat);
that.data.ctx.setFillStyle('#000000');
that.data.ctx.fillText('2023005020340', 81*rat, 128*rat);
that.data.ctx.fillText('张三', 81*rat, 144*rat);
that.data.ctx.fillText('蒋庄矿区学校', 81*rat, 160*rat);
that.data.ctx.fillText('初中组', 81*rat, 176*rat);
that.data.ctx.fillText('--', 81*rat, 192*rat);
that.data.ctx.fillText('--', 81*rat, 208*rat);
that.data.ctx.setFillStyle('#080808');
that.data.ctx.setFontSize(5*rat);
that.data.ctx.fillText('请妥善保管,凭有效身份证件(身份证、户口本、护照、', 35*rat, 226*rat);
that.data.ctx.fillText('社保卡、学生证等)入场考试。', 35*rat, 238*rat);
that.data.ctx.setFontSize(8*rat);
that.data.ctx.fillText('考生守则', 255*rat, 29*rat);
const text1 = ' 一、考生必须自觉服从监考员等考试工作人员管理,不得以任何理由妨碍监考员等考试工作人员履行职责,不得扰乱考场及其他考试工作地点的秩序。二、考生凭准考证、身份证(学生证、户口簿)按规定时间、地点参加考试。三、考生人场,除书写用0.5mm黑色签字笔外,其他任何物品不准带入考场。严禁携带各种通讯工具(如无线耳机、移动电话及其他无线接收、传送设备等)、手表、电子存储记忆录放设备以及涂改液、修正带等物品进人考场。考场内不得自行传递文具、用品等。四、考生入场后,对号入座,将准考证等证件放在课桌左上角以便核验,考生领到答题和试卷后,应检查答题卡和试卷是否有重印、漏印、字迹不清等印刷质量问题或缺页现象,如有,请立即举手报告,否则在开考一段时间后,考生如发现上述问题,由此延误的考试时不予弥补。考生应在指定位置和规定的时间内准确清楚地填写毕业学校、姓名准考证号、座号等栏目。五、开考信号发出后才能开始答题。六、开考15 分钟后不准人场。七、在考场内须保持安静,不准喧哗,不准交头接耳、左顾右盼,不准将试题、草稿纸带出考场。八、考试终了信号发出后,考生应立即停止答卷,并按试题、草稿纸自下而上的顺序排放好,坐在座位上,等候考员查收,无误后,根据监考员指令依次离开考场。九、如不遵守考场纪律,不服从考试工作人员管理,有违纪、作弊等行为的,将按照《国家教育考试违规处理办法》进行处理。';
that.drawText(that.data.ctx, text1, 185*rat, 49*rat, 30*rat,165*rat);
that.data.ctx.setStrokeStyle("#646464");
that.data.ctx.setLineWidth(1);
that.data.ctx.rect(35*rat, 118*rat, 127*rat, 96*rat);
that.data.ctx.stroke()
that.data.ctx.beginPath();
that.data.ctx.moveTo(35*rat, 133*rat);
that.data.ctx.lineTo(162*rat, 133*rat);
that.data.ctx.moveTo(35*rat, 149*rat);
that.data.ctx.lineTo(162*rat, 149*rat);
that.data.ctx.moveTo(35*rat, 165*rat);
that.data.ctx.lineTo(162*rat, 165*rat);
that.data.ctx.moveTo(35*rat, 181*rat);
that.data.ctx.lineTo(162*rat, 181*rat);
that.data.ctx.moveTo(35*rat, 197*rat);
that.data.ctx.lineTo(162*rat, 197*rat);
that.data.ctx.moveTo(73*rat,118*rat);
that.data.ctx.lineTo(73*rat, 214*rat);
that.data.ctx.stroke();
that.data.ctx.draw(true, function () {
that.daochu();
});
},
三、封装文字换行缩进
drawText(ctx, str, leftWidth, initHeight, titleHeight, canvasWidth) {
ctx.setFontSize(16);
let lineWidth = 0;
let lastSubStrIndex = 0;
const indentWidth = 20;
let shouldIndent = false;
for (let i = 0; i < str.length; i++) {
const charWidth = ctx.measureText(str[i]).width;
lineWidth += charWidth;
if (lineWidth > canvasWidth - indentWidth || str[i] === '。') {
let lineText = str.substring(lastSubStrIndex, i + 1);
if (shouldIndent) {
lineText = ' '.repeat(indentWidth / ctx.measureText(' ').width) + lineText;
shouldIndent = false;
}
ctx.fillText(lineText, leftWidth, initHeight);
initHeight += 22;
lineWidth = 0;
lastSubStrIndex = i + 1;
if (str[i] === '。') {
shouldIndent = true;
}
leftWidth = leftWidth;
}
}
if (lastSubStrIndex < str.length) {
let lineText = str.substring(lastSubStrIndex);
if (shouldIndent) {
lineText = ' '.repeat(indentWidth / ctx.measureText(' ').width) + lineText;
}
ctx.fillText(lineText, leftWidth, initHeight);
}
titleHeight += 10;
return titleHeight;
},