Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make Canvas Text Selectable?

Any suggestion is highly appreciated.

like image 707
collimarco Avatar asked Sep 20 '09 18:09

collimarco


People also ask

Can you use text on canvas?

Visuals make sense, but using text in canvas prints can be a powerful way to make a statement. Text is a strong design choice, especially used cleverly and combined with affordable, high-quality canvas prints.

How do I select an element in canvas?

Canvas does not have an interface for the elements on it like the DOM. It is solely used for drawing. You need to create your assets as objects and use a drawing loop to paint them. You then forget about the canvas element, you work with your objects, with their offsets, etc.

How do I edit text in canvas?

In the canvas toolbar, click the Select/Transform tool, then double-click text in the canvas. In the Layer's list, select a text layer, then in the Text editor (at the bottom of the Text Inspector's Format pane), drag within or double-click text.


1 Answers

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.

like image 151
Charles Pritchard Avatar answered Oct 06 '22 00:10

Charles Pritchard