HTML text field over canvas element

后端 未结 2 1204
庸人自扰
庸人自扰 2020-12-28 16:00

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 up

相关标签:
2条回答
  • 2020-12-28 16:04

    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.

    0 讨论(0)
  • 2020-12-28 16:13

    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>
    
    0 讨论(0)
提交回复
热议问题