Text in 3D/VR advice, thoughts (design and code)

textureContext.font = "bold 40px Calibri";	
textureContext.save();
textureContext.fillStyle = "white";	
textureContext.textAlign="centre";	
wrapText(textureContext,"Lots of really long and exciting text",null,80,300,25);	
textureContext.restore();	
textureGround.update(); 
function wrapText(context, text, x, y, maxWidth, lineHeight){	
  var words = text.split(' ');	
  var line = '';		
  for(var n = 0; n < words.length; n++){	  
    var testLine = line + words[n] + ' ';
    var metrics = context.measureText(testLine);
    var testWidth = metrics.width;	  
    if (testWidth > maxWidth && n > 0) {		
      context.fillText(line, x, y);
      line = words[n] + ' ';
      y += lineHeight;	  
    }	else {		
      line = testLine;	  
    }}	
    context.fillText(line, x, y);
};

In case anybody ever meets this problem, this appears to wrap text for me.

1 Like