Input form inside canvas element

2019-03-19 07:19发布

I'm wondering if there was any way to get an input from the user, preferably to a texbox (or anywhere a user can see what he's writting) in a canvas element through javascript.

4条回答
祖国的老花朵
2楼-- · 2019-03-19 08:02

Position a textbox over the top of the canvas element using absolute positioning.

my suggested layout is something like:

<div style="position:relative;width:800px;height:800px">
    <canvas width="800" height="800"></canvas>
    <input type="text" style="position:absolute;left:100px;top:300px;width:600px; etc...." />
</div>

with this you have the relative positioned <div> to base where your going to pop things up over, I would probably also add a modal backdrop...

hope this helps -ck

查看更多
姐就是有狂的资本
3楼-- · 2019-03-19 08:07

You can also try it with the following:

<div style="margin:0px;padding:0px;position:absolute;width:[amount by programmer];height:[amount by programmer];border-style:solid;border-color:[if wanted];">
<canvas style="width:700px;height:700px;"></canvas>
<input type="text" style="position:absolute;width:[amount by programmer];height:[amount by programmer];"/></div>
查看更多
聊天终结者
4楼-- · 2019-03-19 08:11

There is a canvas input library. It does not use any DOM elements, but is built purely on canvas. You can read more more about it and download it at http://goldfirestudios.com/blog/108/CanvasInput-HTML5-Canvas-Text-Input . It's open-source.

查看更多
小情绪 Triste *
5楼-- · 2019-03-19 08:12

Question is a bit old, but I wanted to provide an alternative to absolute positioning. You can set the background-image (presumably to a div bigger than your textbox). Here's an example:

HTML:

...<canvas id="canvas"></canvas> <div id="backDrop">...[your textbox]</div>...

Javascript:

$('#backDrop').css('background-image', 'url(' + document.getElementById('canvas').toDataURL() + ')');

Here's a jsfiddle as an example.

As noted here, this essentially takes a snapshot of the canvas at the point you set the background-image property. Any changes you make to the canvas after setting the background-image will not be reflected (unless you re-set it), but this is probably what you want in most cases.

查看更多
登录 后发表回答