I am having problems reflecting the new line and multiple spaces entered by a user in a text area onto a canvas element. I cannot seem to find a away to convert the text to preformatted.
Essentially the way my site works is that I have a text box where the user enters text within an text area. This text is then passed to the canvas element.
I have provided a sample of my code. Essentially when the user types within the text area it is passed to a function called inputtextgo1. Input text 1 takes the text from the text area and reproduces the text onto the canvas element (Section1 canvas).
HTML:
<canvas id="Section1Canvas" width="500" height="95" >Your browser does not support the HTML5 canvas tag.</canvas>
<textarea class="bags" id="bag1areatext" onkeyup="inputtextgo1()" name="Text">Sample Text</textarea> <br/>
Javascript:
var canvas = document.getElementById('Section1Canvas'),
context = canvas.getContext('2d');
section1backgroundimage.onload = function(){
context.drawImage(section1backgroundimage, 0, 0);
context.fillText("Sample Text",250,50);
}
section1backgroundimage.src = 'images/Selection/Bag/Section1/LightBlue.jpg';
context.font="34px " + selfonttype;
context.textAlign="center";
context.fillStyle = seltextcolor;
function inputtextgo1() {
var y = document.getElementById("bag1areatext").value;
context.clearRect(0, 0, 500, 95)
context.font="34px " + selfonttype;
context.fillStyle = seltextcolor;
context.fillText(y,250,50);
}
selfontype is the font selected by the user, section1backgroundimage is an image file that is used for the background of the canvas and seltextcolor is the font colour selected by the user.
I would like the canvas element that I have created to reflect the new lines and spaces entered by the user in the text area. Also I would like the text to be wrapped (if the text touches the edge of the canvas to go onto the next line).
This is just a snippet of the code that I use to update the function. If you need additional code to help with the problem just let me know.
Thank you very much for your help.