Is there a way to resize the height of a canvas according to text line count? The text will reside at bottom of canvas. An image will also be drawn into canvas at top-left most position of canvas, at same time of text, so the text will always start at a fixed location on canvas just under the image. I just want for the canvas to resize vertically according to text line-count, not while I am typing, only after I click a button after the text has been typed in a textarea.
I'm using this for text:
...
var str = $('#TEXTAREA').val();
var splittext = $.map(str.split(" "), function (t) { // Split words > than 40 chars
return t.match(/[\s\S]{1,40}/g) || [];
}).join(" ");
qrc.font = 'normal normal 14px monospace';
qrc.textAlign = 'center';
qrc.fillStyle = '#000';
wrapText(qrc, splittext, x, y, maxWidth, lineHeight);
function wrapText(context, text, x, y, maxWidth, fontSize, fontFace){
var words = text.split(' ');
var line = '';
var lineHeight = fontSize;
context.font=fontSize+" "+fontFace;
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) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
if(++lineCount>6){return(y);} // Change to "Add 14px to canvas height at each line count" ?
}
else {
line = testLine;
}
}
context.fillText(line, x, y);
return(y);
}
Currently, this code doesn't print text beyond 6 lines of text. I don't know where in the code to modify / add (see comments in code). Any hints?
UPDATE w/ FIDDLE
Here's a fiddle to experiment with. Please use that fiddle in your answer. Read TODO comment in fiddle. I'm stumped, but we're nearly thar! Thx for input. http://jsfiddle.net/uL84x7vw/12/
UPDATE w/ FIDDLE
This fiddle is same as previous but moves the image placeholder code to bottom of fiddle code so it doesn't get cleared. It's looking better but all lines of text except last line of text are still being wiped out. Obviously some code missing. Thx for input. http://jsfiddle.net/uL84x7vw/15/