小程序canvas文本繪制自動換行、字體加粗簡單實現(xiàn)
標(biāo)簽:
JavaScript
直接上代码
Page({ /** * 渲染文字 * * @param {Object} obj */ drawText: function (obj) { console.log('渲染文字') this.ctx.save(); this.ctx.setFillStyle(obj.color); this.ctx.setFontSize(obj.size); this.ctx.setTextAlign(obj.align); this.ctx.setTextBaseline(obj.baseline); if (obj.bold) { console.log('字体加粗') this.ctx.fillText(obj.text, obj.x, obj.y - 0.5); this.ctx.fillText(obj.text, obj.x - 0.5, obj.y); } this.ctx.fillText(obj.text, obj.x, obj.y); if (obj.bold) { this.ctx.fillText(obj.text, obj.x, obj.y + 0.5); this.ctx.fillText(obj.text, obj.x + 0.5, obj.y); } this.ctx.restore(); }, /** * 文本换行 * * @param {Object} obj */ textWrap: function (obj) { console.log('文本换行') var td = Math.ceil(obj.width / (obj.size)); var tr = Math.ceil(obj.text.length / td); for (var i = 0; i < tr; i++) { var txt = { x: obj.x, y: obj.y + (i * obj.height), color: obj.color, size: obj.size, align: obj.align, baseline: obj.baseline, text: obj.text.substring(i * td, (i + 1) * td), bold: obj.bold }; if (i < obj.line) { if (i == obj.line-1){ txt.text = txt.text.substring(0, txt.text.length - 3) +'......'; } this.drawText(txt); } } }, })
参数说明
drawText:
let title = { x: 40, y: 524, color: '#333333', size: 32, align: 'left', baseline: 'top', text: '哈哈哈哈,加粗', bold: true};this.drawText(title);
参数 | 类型 | 说明 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
x | Number | 绘制文本的左上角x坐标位置 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
y | Number | 绘制文本的左上角y坐标位置 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
color | Color | 字体的颜色 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
size | Number | 字体的字号 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
效果生成图片效果 注意,以上是简单的实现,所以如果有英文会计算不精确,解决方法如下:优化原理:通过基础库 1.9.90 开始支持的measureText接口返回的文本宽度,把每个字符宽度不断累加,精确计算在哪个位置应该换行去实现这个功能。 /** * 获取文本折行 * @param {Object} obj * @return {Array} arrTr */getTextLine: function(obj){ this.ctx.setFontSize(obj.size); let arrText = obj.text.split(''); let line = ''; let arrTr = []; for (let i = 0; i < arrText.length; i++) { var testLine = line + arrText[i]; var metrics = this.ctx.measureText(testLine); var width = metrics.width; if (width > obj.width && i > 0) { arrTr.push(line); line = arrText[i]; } else { line = testLine; } if (i == arrText.length - 1) { arrTr.push(line); } } return arrTr; },
之后,textWrap方法改为: /** * 文本换行 * * @param {Object} obj */textWrap: function (obj) { console.log('文本换行') let tr = this.getTextLine(obj); for (let i = 0; i < tr.length; i++) { if (i < obj.line){ let txt = { x: obj.x, y: obj.y + (i * obj.height), color: obj.color, size: obj.size, align: obj.align, baseline: obj.baseline, text: tr[i], bold: obj.bold }; if (i == obj.line - 1) { txt.text = txt.text.substring(0, txt.text.length - 3) + '......'; } this.drawText(txt); } } }, 效果有英文字母生成图片效果 注:在开发工具截取圆形会失效,这张是开发者工具生成的,第一张是真机生成的。 优化前后对比优化前后对比 补充,字体加粗基础库 1.9.90 开始支持设置字体样式 canvasContext.font canvasContext.font = value value 支持的属性有:
加粗如果是不需要兼容的话直接用这个属性就好了,如果需要兼容低版本库,就用重复渲染的方式,希望小程序后续支持多行文本换行吧~ 优化待续...
點擊查看更多內(nèi)容
為 TA 點贊
評論
評論
共同學(xué)習(xí),寫下你的評論 評論加載中... 作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
微信客服
購課補(bǔ)貼 慕課網(wǎng)APP 掃描二維碼 舉報 0/150
提交
取消
|