text.js 377 B

1234567891011121314
  1. export const getWrapText = (ctx, text = "", maxWidth = 200) => {
  2. let txtList = [];
  3. let str = "";
  4. for (let i = 0, len = text.length; i < len; i++) {
  5. str += text.charAt(i);
  6. if (ctx.measureText(str).width > maxWidth) {
  7. txtList.push(str.substring(0, str.length - 1));
  8. str = "";
  9. i--;
  10. }
  11. }
  12. txtList.push(str);
  13. return txtList;
  14. };