微信小程序跳

/**

* 画布文本换行绘制

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

})

})

}

相关推荐
粤M温同学10 小时前
Notepad++ 在当前文件中查找窗口不见了解决方案
notepad++
m0_6948455712 小时前
Oh My Zsh 使用指南:Zsh 终端配置与插件管理教程
服务器·前端·小程序·开源·github
喂_balabala14 小时前
小程序-下拉刷新不走回调函数
微信小程序
2501_9339072115 小时前
宁波本凡科技提供性价比高的智能解决方案
科技·微信小程序·小程序
一字白首17 小时前
进阶初学:微信小程序核心语法与配置实战DAY02
微信小程序·小程序
2601_9520137617 小时前
2024仿东郊微端家政预约上门小程序【亲测可用】家政管理系统抢单派单开源源码
小程序
CHU72903518 小时前
随时随地学新知——线上网课教学小程序前端功能详解
前端·小程序
职豚求职小程序19 小时前
京东26春招笔试题库宝藏小程序刷题yyds!
小程序
游戏开发爱好者819 小时前
如何使用Instruments和Keymob进行Swift应用性能优化分析
开发语言·ios·性能优化·小程序·uni-app·iphone·swift
2501_9160088919 小时前
移动应用上架到应用商店的完整指南:原理与详细步骤
android·ios·小程序·https·uni-app·iphone·webview