I'm trying to spell a word in HTML5 canvas, but I'm scared!
I created a TextTyper object that will manage multiple lines of text enclosed in text.
The problem I am facing is getting an endless loop!
I need a solution that uses pure JavaScript (without jQuery) and HTML5 Canvas.
I created a JSFiddle to solve my problems and test various strategies: http://jsfiddle.net/Jamesking56/eECar/
Here is my TextTyper object:
function TextTyper(text, x, y, font, colour)
{
this.text = text || "";
this.x = x || 20;
this.y = y || 20;
this.font = font || false;
this.colour = colour || [0, 0, 0];
this.lines = 0;
this.draw = function ()
{
canvas.width = canvas.width;
var maxWidth = (canvas.width - 40);
var words = this.text.split(' ');
var line = [words[0]];
for (var i = 1; i < words.length; i++)
{
while (ctx.measureText(line.join(' ')) < maxWidth && i < words.length - 1)
{
line.push(words[i++]);
}
if (i < words.length - 1)
{
line.pop();
i--;
}
this.lines++;
}
}
}
Any ideas?
source
share