I don't have an idea on how to implement dynamic drawing box for text.
Basically here is the sample output:
Note:
The box should be infront if there is another object in the canvas.
Update
Here is my code so far:
MouseDown(){ //if first mousedown X1 = tempX; //tempX is for current positon Y1 = tempY; if (Input.style.display === "none") { Input.value = ""; Input.style.top = tempX + 'px'; Input.style.left = tempY + 'px'; Input.size = 1; Input.style.display = "block"; Draw(); } } Draw(){ var userInput = Input.value; rectangledText(X1, Y1, 150, userInput, 12, 'verdana'); //code by markE } function rectangledText(x, y, width, text, fontsize, fontface) { //code by markE self.TextWidth = ctx.measureText(Text).width; var height = wrapText(x, y, text, fontsize, fontface, width) ctx.strokeRect(x, y, width, height); } function wrapText(x, y, text, fontsize, fontface, maxwidth) { var startingY = y; var words = text.split(' '); var line = ''; var space = ''; var lineHeight = fontsize * 1.286; ctx.font = fontsize + "px " + fontface; ctx.textAlign = 'left'; ctx.textBaseline = 'top' for (var n = 0; n < words.length; n++) { var testLine = line + space + words[n]; space = ' '; if (ctx.measureText(testLine).width > maxwidth) { ctx.fillText(line, x, y); line = words[n] + ' '; y += lineHeight; space = ''; } else { line = testLine; } } ctx.fillText(line, x, y); return (y + lineHeight - startingY); } And sample output with explanation:
The issue is how I can make the first mousedown rectangle and text be wrapped using the Input.style
