[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Capturing 'new line' info from div

Ed Jay

10/24/2014 12:37:00 AM

My application creates contenteditable divs as child elements of a
another div. Each created div is given the id of 'mrkr'+i, where i is
the number of divs created, i.e., the first created div has
id='mrkr0,' etc. I enter text into those divs. If the text is longer
than the div min-width dimension, the text is word-wrapped to a new
line. Alternatively, I hit ctrl-return to manually create the new
line.

I want to place the contents of each div onto a canvas
("save_canvas"), at the same location as the original text,
which locations are stored in two arrays (x_pos_array and
y_pos_array), and with the same line breaks as in the created divs. I
[attempt] this with the following:

cnt = x_pos_array.length;
for (var i = 0; i < cnt; i++) { //look at all child divs
test_div = document.getElementById('mrkr'+i).innerHTML;
if(test_div !='') { //if there's text in the div
x = x_pos_array[i];
y = y_pos_array[i];
save_canvas.fillStyle = 'white';
save_canvas.font = "12pt 'Arial Narrow'";
save_canvas.fillText(test_div, x, y);
}
}

This works, with the exception that none of the line breaks,
whether word wrapped or manually entered, are effected on the
canvas. The text flows to the right, out of the canvas.

Is there a method for capturing the line breaks in the original
divs on the canvas?

I needn't be provided with code...a simple yes or no will guide me.

Thanks,
--
Ed Jay (remove 'M' to respond by email)
2 Answers

Chris Winter

10/24/2014 11:24:00 AM

0

Am 24.10.2014 02:36, schrieb Ed Jay:
[...]
> I want to place the contents of each div onto a canvas
> ("save_canvas"), at the same location as the original text,
> which locations are stored in two arrays (x_pos_array and
> y_pos_array), and with the same line breaks as in the created divs.
[...]
>
> This works, with the exception that none of the line breaks,
> whether word wrapped or manually entered, are effected on the
> canvas. The text flows to the right, out of the canvas. [...]

fillText ignores line wrapping. You'll probably have to roll
your own solution that splits the text appropriately and calls
fillText for each line. Be wary of solutions out there in
the web, as they often use unreliable shortcuts to "calculate"
line heights. If you're making a one-platform, one-font solution,
you can probably survive with hard-coding line offsets. Otherwise
you'll have to decide which downsides of the approaches you can
live with.

-Chris

Ed Jay

10/24/2014 3:31:00 PM

0

On Fri, 24 Oct 2014 13:23:57 +0200, Chris Winter wrote:

>Am 24.10.2014 02:36, schrieb Ed Jay:
>[...]
>> I want to place the contents of each div onto a canvas
>> This works, with the exception that none of the line breaks,
>
>fillText ignores line wrapping. You'll probably have to roll
>your own solution that splits the text appropriately and calls
>fillText for each line. Be wary of solutions out there in
>the web, as they often use unreliable shortcuts to "calculate"
>line heights. If you're making a one-platform, one-font solution,
>you can probably survive with hard-coding line offsets. Otherwise
>you'll have to decide which downsides of the approaches you can
>live with.
>
Thanks, Chris.

I am using a single-platform/font, so I guess my weekend is cut out
for me. :-) Thanks, again.
--
Ed Jay (remove 'M' to respond by email)