I'm trying to display textarea information that has been stored in a MariaDB. I don't have a problem storing the text information. What I'm having a problem with is transition the formatting from the text area to the canvas I want it displayed in.
The goal is to have a user fill in the notes in a textarea and then have those displayed in the separate canvas report.
Right now, I can get the wordwrap working successfully using this code I have stored in a wordWrap.js file:
function wrapText (c, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
var lineCount = 0;
var test;
var metrics;
for (var i = 0; i < words.length; i++) {
test = words[i];
// add test for length of text
metrics = c.measureText(test);
while (metrics.width > maxWidth) {
test = test.substring(0, test.length - 1);
metrics = c.measureText(test);
}
if (words[i] != test) {
words.splice(i + 1, 0, words[i].substr(test.length))
words[i] = test;
}
test = line + words[i] + ' ';
metrics = c.measureText(test);
if (metrics.width > maxWidth && i > 0) {
c.fillText(line, x, y);
line = words[i] + ' ';
y += lineHeight;
lineCount++;
}
else {
line = test;
}
}
c.fillText(line, x, y);
}
I can add the text, which word wraps based on the size of the fillText area and the length of the words. What I need to add to this is the ability to support carriage returns. The users won't have a problem using \n to support carriage returns so I just need to get it to work.
I've seen other code out there that supports carriage returns. Example I've played with below.
ctx.font = '12px Courier';
var text = <?php echo json_encode($row['notes']);?>;
var x = 30;
var y = 30;
var lineheight = 15;
var lines = text.split('\n');
for (var i = 0; i<lines.length; i++) {
ctx.fillText(lines[i], x, y + (i*lineheight) );
}
These methods have similar attributes and I believe they can be aligned but I'm having trouble figuring out how to implement the key piece of both scripts which is what drives text split ...
text.split('\n')
text.split(' ')
This looks to me like a combination of for and while loops like the word wrap uses, but I need some help figuring out where.