javascript - wrap text while user is typing -


i have text area user types in. text rendered on canvas while user typing in text area field. appears letter letter. i'm having trouble making text wrapping work since should happen while user typing.

i have function drawtext(); called on every keystroke. problem i'm having previous line disappears when text drawn on next line. know because i'm calling clearrect in loop. if don't text keeps rendering on each other. how can solve this?

function drawtext () {  var maxwidth = 500; var textareastring = $('textarea').val()+' '; var thecanvas = document.getelementbyid("mycanvas"); var ctx = thecanvas.getcontext('2d'); ctx.fillstyle = colors[currentbackground]; ctx.fillrect(0,0,598,335); ctx.font = "50px interstate"; ctx.textalign = 'center';  var x = 300; var y = 75; var lineheight = 50; var words = textareastring.split(' '); var line = '';  for(var n = 0; n < words.length; n++) {     var testline = line + words[n] + ' ';     var metrics = ctx.measuretext(testline);     ctx.clearrect(0,0,598,335);      ctx.fillstyle = '#ffffff';     var testwidth = metrics.width;      if (testwidth > maxwidth && n > 0) {         ctx.filltext(line, x, y);         line = words[n] + ' ';         y += lineheight;     } else {         ctx.filltext(line, x, y);         line = testline;     } } 

}

oops--my answer duplicate! @will anderson says (i typing answer when posted correctly)

clear canvas before for-loop , redraw lines of text again.

example code , demo: http://jsfiddle.net/m1erickson/7d5bs/

<!doctype html> <html> <head> <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css --> <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> <style>     body{ background-color: ivory; }     canvas{border:1px solid red;} </style> <script> $(function(){      var canvas=document.getelementbyid("canvas");     var ctx=canvas.getcontext("2d");      var $text=document.getelementbyid("sourcetext");     $text.onkeyup=function(e){         ctx.clearrect(0,0,canvas.width,canvas.height);         wraptext(ctx,$text.value,20,60,100,24,"verdana");     }      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;         }         else {           line = testline;         }       }       context.filltext(line, x, y);       return(y);     }  }); // end $(function(){}); </script> </head> <body>     <h4>type text wrap canvas.</h4>     <input id=sourcetext type=text><br>     <canvas id="canvas" width=300 height=300></canvas> </body> </html> 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -