微信小程序跳

/**

* 画布文本换行绘制

* 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)

})

})

}

相关推荐
独自归家的兔几秒前
微信小程序开发框架全解析:成熟项目架构、主流技术与优劣对比
微信小程序·小程序
全栈小51 小时前
【小程序】微信小程序开发,分享给朋友或者朋友圈的功能增加地址参数,以及如何进行带参数本地测试
微信小程序·小程序
canglingyue1 小时前
小程序NFC应用
小程序
说私域1 小时前
链动2+1模式AI智能名片小程序驱动下的社群互动与消费升级研究
人工智能·小程序
FILLKL1 小时前
微信PC版本4.0后小程序目录变更
微信·小程序
QQ588501981 小时前
springboot地区特色农产品团购商城平台 小程序
spring boot·后端·小程序
曾宇琪 8323013251 小时前
PoopCare 小程序用户使用调查报告
小程序
蜂酷科技资讯11 小时前
用小程序开发平台一天做俩小程序?这赚钱效率我服了
小程序
猫头虎1 小时前
蚂蚁百宝箱 3 分钟上手 MCP:6 步轻松构建 Qwen3 智能体应用并发布小程序
人工智能·小程序·prompt·aigc·agi·ai-native·智能体
咖啡の猫20 小时前
微信小程序页面事件
微信小程序·小程序·notepad++