Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
124
rated 0 times [  125] [ 1]  / answers: 1 / hits: 24237  / 15 Years ago, sun, september 20, 2009, 12:00:00

Any suggestion is highly appreciated.


More From » text

 Answers
27

Text selection has many components to it some visual, some non-visual.



First, make text selectable you must keep an array, of where the text is, what the text is, and what font was used. You will use this information with the Canvas function measureText.



By using measureText, with your text string, you can identify what letter the cursor should land on when you click on an image.



ctx.fillText(My String, 100, 100);
textWidth = ctx.measureText(My String).width;


You will still have to parse the font height from the font property,
as it is not currently included in text metrics. Canvas text is aligned
to the baseline by default.



With this information, you now have a bounding box, which you can check against.
If the cursor is inside of the bounding box, you now have the unfortunate task of deducing
which letter was intentionally selected; where the start of your cursor should be placed. This may involve calling measureText several times.



At that point you know where the cursor should go; you will need to store your
text string as a text string, in a variable, of course.



Once you have defined the start and stop points of your range, you have to draw
a selection indicator. This can be done in a new layer (a second canvas element),
or by drawing a rectangle using the XOR composition mode. It can also be done by
simply clearing and redrawing the text on top of a filled rectangle.



All told, text selection, text editing in Canvas are quite laborious to program, and it would be wise to re-use components already written, Bespin being an excellent example.



I'll edit my post should I come across other public examples. I believe that Bespin uses a grid-based selection method, possibly requiring a monospaced font. Ligatures, kerning, bi-directionality and other advanced features of font rendering require additional programming; it's a complex problem.


[#98654] Wednesday, September 16, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breap

Total Points: 606
Total Questions: 96
Total Answers: 108

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
breap questions
Thu, Jun 24, 21, 00:00, 3 Years ago
Wed, Mar 18, 20, 00:00, 4 Years ago
Mon, Oct 7, 19, 00:00, 5 Years ago
;