Advertisement
dnnkeeper

wrapText

Dec 18th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wrapText(text, maxWidth = 100, lineHeight = 10, offsetX = 0) {
  2.     var cars = text.split("\n");
  3.     for (var ii = 0; ii < cars.length; ii++) {
  4.         //console.log(cars.length, ii);
  5.         var line = "";
  6.         var words = cars[ii].split(" ");
  7.  
  8.         for (var n = 0; n < words.length; n++) {
  9.        
  10.             var testWord = words[n];
  11.            
  12.             var testLine = line + testWord;// + " ";
  13.             var testLineWidth = measureText(testLine).width;
  14.             testLine += " ";
  15.            
  16.             if (testLineWidth > maxWidth) {
  17.                 var lengthToEnd = maxWidth-measureText(line).width;
  18.                 console.log("testLineWidth "+testLine+" = "+testLineWidth+">"+maxWidth+" is too big!");
  19.                
  20.                 var wordWidth = measureText(testWord).width;
  21.                 var secondPart = "";
  22.                
  23.                 if (wordWidth > maxWidth){
  24.                     console.log("even word "+testWord+" is too big! "+wordWidth);
  25.                     var b = 0;
  26.                     while(wordWidth > maxWidth && b < 100){
  27.                         b++;
  28.                         var symbolWidth = wordWidth/testWord.length;
  29.                         lengthToEnd = maxWidth-measureText(line).width;
  30.                         var maxN = Math.floor( (lengthToEnd)/symbolWidth );
  31.                         if (maxN > 0){
  32.                             var firstPart = testWord.substring(0,maxN);
  33.                            
  34.                             console.log("but "+firstPart+" is ok");
  35.                            
  36.                             secondPart = testWord.substring(maxN,testWord.length);
  37.                            
  38.                             addLine(line+firstPart, lineHeight, offsetX + ((maxWidth-measureText(line+firstPart).width)/2).clamp(0,maxWidth));
  39.                            
  40.                             line = "";
  41.                            
  42.                             wordWidth = measureText(secondPart).width;
  43.                             testWord = secondPart;
  44.                         }
  45.                         else{
  46.                             console.log("and we can't add anything here", line, context.measureText(line).width, symbolWidth );
  47.                             wordWidth = measureText(testWord).width;
  48.                             addLine(line, lineHeight, (offsetX + lengthToEnd/2).clamp(0,maxWidth));
  49.                            
  50.                             line = "";
  51.                         }
  52.                     }
  53.                     line = testWord;// + " ";
  54.                     console.log(testWord+" is only "+wordWidth+" so now '"+line+"' is ok. Go next?");
  55.                 }
  56.                 else{                      
  57.                     console.log(testWord+" wordWidth "+wordWidth+"<"+maxWidth+", so go next line writing "+line);
  58.                     addLine(line, lineHeight, offsetX + (lengthToEnd/2).clamp(0,maxWidth));
  59.                    
  60.                     line = testWord;// + " ";
  61.                 }
  62.             }
  63.             else {
  64.                 line = testLine;
  65.             }
  66.            
  67.         }
  68.         addLine(line, lineHeight, offsetX + ((maxWidth-measureText(line).width)/2).clamp(0,maxWidth));
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement