内容不能为空且长度不能超过 20000 字
CanvasContext.measureText的性能是不是很低?在使用measureText的时候,canvas的绘制非常耗时,一张简单的页面也需要10s左右,开发工具在真机上调试时会出现“API measureText加载中”之类的提示(调试环境速度很快)。是我的使用方式不对吗?有办法优化吗? 代码如下: [代码]textProcess: [代码][代码]function[代码][代码](canvas, text, width, line) {[代码][代码] [代码][代码]let chr = text.split([代码][代码]''[代码][代码])[代码][代码] [代码][代码]let temp = [代码][代码]''[代码][代码] [代码][代码]let rowArray = [][代码] [代码] [代码][代码]for[代码] [代码](let i = 0; i < chr.length; i++) {[代码][代码] [代码][代码]if[代码] [代码](canvas.measureText(temp).width + canvas.measureText(chr[i].toString()).width < width) {[代码][代码] [代码][代码]temp += chr[i][代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]i-- [代码][代码]//这里添加了a-- 是为了防止字符丢失[代码][代码] [代码][代码]rowArray.push(temp)[代码][代码] [代码][代码]temp = [代码][代码]""[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]rowArray.push(temp)[代码] [代码] [代码][代码]if[代码] [代码](rowArray.length > line) {[代码][代码] [代码][代码]let arrayPart = rowArray.slice(0, line)[代码][代码] [代码][代码]let row = arrayPart[line - 1][代码][代码] [代码][代码]let tempString = [代码][代码]''[代码][代码] [代码][代码]let tempRow = [][代码][代码] [代码][代码]for[代码] [代码](let i = 0; i < row.length; i++) {[代码][代码] [代码][代码]if[代码] [代码](canvas.measureText(tempString).width + canvas.measureText(chr[i].toString()).width + canvas.measureText([代码][代码]'...'[代码][代码]).width < width) {[代码][代码] [代码][代码]tempString += row[i][代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]break[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]tempRow.push(tempString)[代码][代码] [代码][代码]let group = tempRow[0] + [代码][代码]'...'[代码][代码] [代码][代码]arrayPart.splice(line - 1, 1, group)[代码][代码] [代码][代码]rowArray = arrayPart[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]return[代码] [代码]rowArray[代码][代码] [代码][代码]},[代码] 插一句使用canvas的感受:开始以为绘制图片跟截图一样简单,结果发现必须使用canvas,一个简单的图片要花几天的时间去绘制,还特别丑,一下子感觉回到了用C语言的时代。。。
2021-09-142000
微信小程序广告费用很低?微信小程序广告结算从8月份以来,一直很低,低得可怕!
2021-09-14