Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以烧瓶形式提交画布绘图_Javascript_Html_Jquery_Flask_Canvas - Fatal编程技术网

Javascript 以烧瓶形式提交画布绘图

Javascript 以烧瓶形式提交画布绘图,javascript,html,jquery,flask,canvas,Javascript,Html,Jquery,Flask,Canvas,我已经在Flask中创建了表单,我正在使用引导来帮助呈现文件。我的表单工作正常,但现在我想让用户画出他们的签名,并将签名与表单一起提交。但我一直很难做到这一点,因为当我将canvas标记放在表单标记中时,用户无法在画布中绘制任何东西。如果我将canvas标记放在表单标记之外,我就能够在其上进行绘制 {% extends "base.html" %} {% import 'wtf.html' as wtf %} <html> <body> {% block app_cont

我已经在Flask中创建了表单,我正在使用引导来帮助呈现文件。我的表单工作正常,但现在我想让用户画出他们的签名,并将签名与表单一起提交。但我一直很难做到这一点,因为当我将canvas标记放在表单标记中时,用户无法在画布中绘制任何东西。如果我将canvas标记放在表单标记之外,我就能够在其上进行绘制

{% extends "base.html" %}
{% import 'wtf.html' as wtf %}
<html>
<body>
{% block app_content %}
{% block content %}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<script>
      $(document).ready(function() {
      var flag, dot_flag = false,
        prevX, prevY, currX, currY = 0,
        color = 'black', thickness = 2;
      var $canvas = $('#canvas');
      var ctx = $canvas[0].getContext('2d');

      $canvas.on('mousemove mousedown mouseup mouseout', function(e) {
        prevX = currX;
        prevY = currY;
        currX = e.clientX - $canvas.offset().left;
        currY = e.clientY - $canvas.offset().top;
        if (e.type == 'mousedown') {
          flag = true;
        }
        if (e.type == 'mouseup' || e.type == 'mouseout') {
          flag = false;
        }
        if (e.type == 'mousemove') {
          if (flag) {
            ctx.beginPath();
            ctx.moveTo(prevX, prevY);
            ctx.lineTo(currX, currY);
            ctx.strokeStyle = color;
            ctx.lineWidth = thickness;
            ctx.stroke();
            ctx.closePath();
          }
        }
      });

      $('.canvas-clear').on('click', function(e) {
        c_width = $canvas.width();
        c_height = $canvas.height();
        ctx.clearRect(0, 0, c_width, c_height);
        $('#canvasimg').hide();
      });
    });
</script>

  //When canvas placed here, the canvas shows up, and it works fine as user can write to it
  <canvas id="canvas" width="300" height="50" style="border:2px solid;"></canvas>
  <input type="button" value="Clear" class="canvas-clear" />

  <script>
        function delete_flash(flash){$(flash).parent().remove()}
  </script>

    <div class="container">
        {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            {% for category, message in messages %}
            <div class="alert alert-{{ category }}" role="alert" onclick=delete_flash(this)>
                {{ message }}
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
                </button>
            </div>
            {% endfor %}
        {% endif %}
        {% endwith %}
    </div>

    <h1><center>Visitor Check In</center></h1>
    <form action="" method="post" novalidate>
        {{ form.hidden_tag() }}

        <h5> &emsp;<b>3. </b>{{ form.question1.label }}</h5>
        {% for subfield in form.question1%}
            <tr>&emsp;
                <td>{{ subfield }}</td>
                <td>{{ subfield.label }}</td> &emsp;
            </tr>
        {% endfor %}
        {% for error in form.question1.errors %}
            <span style="color: red;">[{{ error }}]</span>
        {% endfor %}


        <h5>&emsp;<b>4. </b>{{ form.question2.label }}</h5>
        {% for subfield in form.question2%}
            <tr>&emsp;
                <td>{{ subfield }}</td>
                <td>{{ subfield.label }}</td> &emsp;
            </tr>
        {% endfor %}
        {% for error in form.question2.errors %}
            <span style="color: red;">[{{ error }}]</span>
        {% endfor %}


        //When canvas placed here, the canvas shows up, but user cannot write anything to it
        <canvas id="canvas" width="300" height="50" style="border:2px solid;"></canvas>
        <input type="button" value="Clear" class="canvas-clear" />
        <p>{{ form.submit() }}</p>
    </form>

{% endblock %}
{% endblock %}
</body>
</html>
{%extends“base.html”%}
{%import'wtf.html'作为wtf%}
{%block app_content%}
{%block content%}
$(文档).ready(函数(){
var标志,dot_标志=false,
prevX,prevY,currX,currY=0,
颜色=黑色,厚度=2;
var$canvas=$(“#canvas”);
var ctx=$canvas[0]。getContext('2d');
$canvas.on('mousemove mousedown mouseup mouseout',函数(e){
prevX=currX;
prevY=咖喱;
currX=e.clientX-$canvas.offset().left;
currY=e.clientY-$canvas.offset().top;
如果(e.type=='mousedown'){
flag=true;
}
如果(e.type=='mouseup'| | e.type=='mouseout'){
flag=false;
}
如果(e.type==“mousemove”){
国际单项体育联合会(旗){
ctx.beginPath();
ctx.moveTo(prevX,prevY);
ctx.lineTo(咖喱,咖喱);
ctx.strokeStyle=颜色;
ctx.线宽=厚度;
ctx.stroke();
ctx.closePath();
}
}
});
$('.canvas clear')。在('click',函数(e)上{
c_width=$canvas.width();
c_height=$canvas.height();
ctx.clearRect(0,0,c_宽度,c_高度);
$('#canvasimg').hide();
});
});
//当画布放在这里时,画布就会显示出来,用户可以对其进行写入,它工作得很好
函数delete_flash(flash){$(flash.parent().remove()}
{%with messages=get\u flashed\u messages(with\u categories=true)%}
{%if消息%}
{%对于类别,消息中的消息%}
{{message}}
&时代;
{%endfor%}
{%endif%}
{%endwith%}
访客登记
{{form.hidden_tag()}}
&emsp;3.{{form.question1.label}
{表单中的子字段为%1%}
&emsp;
{{子字段}}
{{subfield.label}}&emsp;
{%endfor%}
{form.question1.errors%}中的错误为%
[{{error}}]
{%endfor%}
&emsp;4.{{form.question2.label}
{form.question2%}中的子字段为%
&emsp;
{{子字段}}
{{subfield.label}}&emsp;
{%endfor%}
{form.question2.errors%}中的错误为%
[{{error}}]
{%endfor%}
//当画布放置在这里时,画布显示出来,但用户不能向其中写入任何内容
{{form.submit()}}

{%endblock%} {%endblock%}
也许将画布放在表单标签中的问题是,我在后端没有任何东西可以提交画布上绘制的内容。因此,我还想知道,当用户单击submit按钮时,如何才能将绘制的内容保存到画布上