Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML text field over canvas element

Tags:

I have been playing around with text in the canvas, and although it is easy to draw, it is not easy to interact with. I began implementing keyboard press functionality to update text on the canvas, but then gave up when I realized I would have to incorporate cut,copy,past and undo functionality.

Is there anyway to "float" html elements on top of eachother? For example can I float a text field ontop of certain parts of my canvas, disable the border and set the color to the canvas color?

Thank You

like image 539
puk Avatar asked Mar 11 '11 21:03

puk


People also ask

How do I put text on a canvas in HTML?

To add a text to the HTML <canvas> element, you need to use the fillText() or strokeText() methods, or you can use the combination of the two. You can also give color to your text, define the font and size, and even add gradients.

How do you put text on canvas?

Drawing Text on the Canvas To draw text on a canvas, the most important property and methods are: font - defines the font properties for the text. fillText(text,x,y) - draws "filled" text on the canvas. strokeText(text,x,y) - draws text on the canvas (no fill)

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.

Which of the following method is use to write text on the canvas?

There are two methods fillText() and strokeText() to draw text on canvas.


2 Answers

You may use the CSS position: absolute property with z-index: 1 to place elements above the canvas.

EDIT: added an example: here the div that contains "1234" floats on top of the div that contains "abcd" (that could be replaced with a canvas element)

<div id="wrapper">     <div style="position: absolute; left: 10px; top: 10px; width:200px; height:100px; background-color: yellow;">         abcd     </div>     <div style="position: absolute; z-index: 1; left: 50px; top: 20px; width:100px; height:20px; background-color: green;">         1234     </div> </div> 
like image 68
ChrisJ Avatar answered Oct 11 '22 03:10

ChrisJ


You can use 'somehow' invisible text-control to gain control over the text being input and copy innerHTML on the canvas on keypress. Im doing similar stuff, copying computed font css attributes of the text present in DOM and more. Z-indexes work pretty straight. The setFocus() function can help too.

like image 32
tomasb Avatar answered Oct 11 '22 02:10

tomasb