微信小程序跳

/**

* 画布文本换行绘制

* canvasContext 画布实例

* text 要写入的文本

* x 初始x轴位置

* y 初始y轴位置

* ySpacing 换行后,每行直接的间隔

* maxWidth 此文本写入画布的最大宽度,超过此宽度就换行

* color 文本颜色

* size 文本字体大小

* align 文本方向 left rigt center 额一直搞不清楚这个方向是怎么个原理

* @returns { textY 绘制最后一行文本的Y轴结束坐标,drawNum 画布本次绘制了几次 }

*/

canvasTextNewlinedraw (options) {

const { canvasContext, text = '', x = 0, y = 0, ySpacing = 0, maxWidth = 0, color, size, align } = options

return new Promise((resolve, reject) => {

size && canvasContext.setFontSize(size)

align && canvasContext.setTextAlign(align)

color && canvasContext.setFillStyle(color)

const textList = text.split('')

let currText = '', textY = 0, drawNum = 0

for (let i = 0; i < textList.length; i++) {

if (canvasContext.measureText(currText + textList[i]).width + x > maxWidth - 10) {

textY += textY === 0 ? y : ySpacing

canvasContext.fillText(currText, x, textY)

currText = textList[i]

drawNum++

} else {

currText += textList[i]

}

}

textY = textY === 0 ? y : textY + ySpacing

canvasContext.fillText(currText, x, textY)

drawNum++

canvasContext.draw(true, _ => {

setTimeout(() => {

resolve({ y: textY, res: _, drawNum })

}, 100)

})

})

}

相关推荐
小溪彼岸2 天前
是时候给想做小程序的小伙伴泼盆冷水了
微信小程序
远山枫谷2 天前
一文理清页面/组件通信与 Store 全局状态管理
前端·微信小程序
木易士心3 天前
一文读懂:微信小程序云数据库直连原理与使用指南
微信小程序·serverless
明月_清风4 天前
小程序云函数:从入门到全栈的“降维打击”指南
前端·微信小程序·小程序·云开发
拉不动的猪4 天前
移动端调试工具VConsole初始化时的加载阻塞问题
前端·javascript·微信小程序
WangHappy6 天前
不写 Canvas 也能搞定!小程序图片导出的 WebView 通信方案
前端·微信小程序
小时前端7 天前
微信小程序选不了本地文件?用 web-view + H5 一招搞定
前端·微信小程序·uni-app
icebreaker7 天前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker7 天前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
大米饭消灭者10 天前
Taro是怎么实现一码多端的【底层原理】
微信小程序·taro