如何在 Bootstrap 模态中显示画布

2023-12-24

我创建了一个地图,您可以在其中通过 Javascript 预订自行车。 用户应该: 1)选择一个自行车站(绿色站=可以使用自行车) 2)点击一个按钮(预留按钮) 3)登录画布(在模式中)

页面在这里:http://p4547.phpnet.org/bikes/reservation.html http://p4547.phpnet.org/bikes/reservation.html

在我的 javascript 中,类对象的调用方式如下:


      document.addEventListener("DOMContentLoaded", event => {
        new Signature();

如果画布位于页面正文中,则此代码可以正常工作。 但是,如果画布位于模态中,则代码将不起作用。

我尝试这样编码:

$('#bookingmodal').on('shown.bs.modal',function(event){
new Signature();

    });


我的模式 ID 是:#bookingmodal


您的问题在于计算画布内鼠标位置的坐标。如果将页面大小调整为非常小的窗口,绘图有时会起作用(带有难看的偏移量)。

我拿走了你的Signature-class 并用一个函数替换了画布内鼠标位置的计算,该函数计算鼠标的正确位置,并处理画布使用的位图的可能缩放:

updateMousePosition(mX, mY) {
  let rect = this.canvas.getBoundingClientRect();
  let scaleX = this.canvas.width / rect.width;
  let scaleY = this.canvas.height / rect.height;
  this.cursorX = (mX - rect.left) * scaleX;
  this.cursorY = (mY - rect.top) * scaleY;
}

Example:

class Signature {
  constructor() {
    this.color = "#000000";
    this.sign = false;
    this.begin_sign = false;
    this.width_line = 5;
    this.canvas = document.getElementById('canvas');
    this.offsetLeft = this.canvas.offsetLeft;
    this.offsetTop = this.canvas.offsetTop;
    this.context = canvas.getContext('2d');
    this.context.lineJoin = 'round';
    this.context.lineCap = 'round';
    this.whenMouseDown();
    this.whenMouseUp();
    this.whenMouseMove();
    this.createSignature();
    this.clearCanvas();
    this.resetCanvas();
  }

  updateMousePosition(mX, mY) {
    let rect = this.canvas.getBoundingClientRect();
    let scaleX = this.canvas.width / rect.width;
    let scaleY = this.canvas.height / rect.height;
    this.cursorX = (mX - rect.left) * scaleX;
    this.cursorY = (mY - rect.top) * scaleY;
  }
  
  whenMouseDown() {
    document.addEventListener("mousedown", ({
      pageX,
      pageY
    }) => {
      this.sign = true;
      this.updateMousePosition(pageX, pageY);
    })
  }

  whenMouseUp() {
    document.addEventListener("mouseup", () => {
      this.sign = false;
      this.begin_sign = false;
    })
  }

  whenMouseMove() {
    this.canvas.addEventListener('mousemove', ({
      pageX,
      pageY
    }) => {
      if (this.sign) {
        this.updateMousePosition(pageX, pageY);
        this.createSignature();
      }
    })
  }

  createSignature() {
    if (!this.begin_sign) {
      this.context.beginPath();
      this.context.moveTo(this.cursorX, this.cursorY);
      this.begin_sign = true;
    } else {
      this.context.lineTo(this.cursorX, this.cursorY);
      this.context.strokeStyle = this.color;
      this.context.lineWidth = this.width_line;
      this.context.stroke();
    }
  }

  clearCanvas() {
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  }

  resetCanvas() {
    document.getElementById("reset").addEventListener("click", () => {
      this.clearCanvas();
    })
  }
}

document.addEventListener("DOMContentLoaded", event => {
  new Signature();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<button type="button" class="btn btn-success" data-target="#bookingmodal" data-toggle="modal">Réserver</button>

<div aria-labelledby="exampleModalLongTitle" class="modal fade" id="bookingmodal" role="dialog" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLongTitle">Réservation</h5><button aria-label="Close" class="close" data-dismiss="modal" type="button"><span aria-hidden="true">×</span></button>
        </div>
        <div class="modal-body">
          <div class="guide">
            <div class="row item">
              <div class="col-md-12 order-md-2">
                <h2 class="item-heading">Signature. <span class="text-muted">Signez pour valider votre réservation.</span></h2>
                  <canvas id="canvas" width="250" height="250">
                  </canvas>
                  <form>
                       <input type="button" id="reset" value="Réinitialiser" class="btn btn-danger">
                  </form>
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" data-dismiss="modal" type="button">Fermer</button>
        </div>
      </div>
    </div>
  </div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Bootstrap 模态中显示画布 的相关文章

随机推荐

  • 玩!框架 1.2.4 --- C3P0 设置以避免通信链路故障影响空闲时间

    我正在尝试自定义我的 C3P0 设置以避免本文底部显示的错误 这是在这个网址上建议的 http make it open blogspot com 2008 12 sql error 0 sqlstate 08s01 html http m
  • Vanilla Javascript 类中的“计算属性”

    The 计算属性该功能在流行的 JS 框架 React VueJS 中很常见 但是我们如何在普通 JS 中实现这个功能呢 假设给定一个User类 具有dateOfBirth属性 我们想计算它的age 有没有比下面的代码更好的方法来执行此任务
  • 在 View Pager 中的 Fragment 之间传递数据

    在视图分页器中的片段之间传递数据时需要帮助 我们尝试在片段内将数据作为包传递 在 Fragment getInstance 方法内部 尝试从其他 Fragment getArguments 获取数据 活动代码 package com nor
  • jQuery ajax 请求因跨源而被阻止

    如何通过ajax从远程url获取内容 jQuery ajax 请求因跨源而被阻止 控制台日志 跨源请求被阻止 同源策略不允许读取 远程资源位于http www dailymotion com embed video x28j5hv http
  • clang 构建 qt 的 mkspecs 是什么?

    如果我想在Windows下使用gcc构建qt 配置选项是 platform win32 g 但是当我想使用clang构建qt时 该选项是什么 clang windows 组合没有 我认为根据 linux g 和 linux clang 之间
  • Twitter Fabric - 无法解析符号

    我已经为 Android Studio 安装了 Twitter Fabric 插件 这很简单直接 但是当我从 Fabric 对话框复制并粘贴代码时 我的项目无法识别任何 Twitter 对象 例如以下行 private TwitterLog
  • Express 中间件、next 和 Promise

    有一个非常简单的带处理程序的 Express 路由器 router get users userId roles roleId function req res next const roleId req params roleId res
  • 如何知道客户端是否已在套接字中终止

    假设 写完这段代码后我有一个已连接的套接字 if sd accept socket d struct sockaddr client addr alen lt 0 perror accept failed n exit 1 我如何在服务器端
  • 使用 jsdom 加载 ajax 应用程序

    我正在寻找一种解决方案来在服务器上引导客户端应用程序 用 Backbone js 编写 以便我可以为爬虫和非 js 消费者提供正确的内容 我一直在尝试使用 jsdom 和 Node js 来引导应用程序 并且可以加载基本模板内容 但应用程序
  • Sonarqube 6.7x 的安全插件

    我们正在使用 sonarqube 我们喜欢它的工作方式 我们正在尝试扩展 sonarqube 以增强安全性 我尝试为sonarqube 6 x找到一些安全插件来检测Java语言的漏洞 但我找不到任何插件 我想知道是否有任何插件可以查找 so
  • 如何检查麦克风是否可用于录音

    我正在开发一个 WPF 应用程序 需要录制用户的音频消息 我按照代码here http channel9 msdn com coding4fun articles NET Voice Recorder并且它工作正常 现在的问题是 如果它是台
  • 如何比较两个 OrderedDict 字典?

    如何比较两个 OrderedDict 字典 我的结构如下 dict a OrderedDict 1 4 2 5 3 3 4 5 5 4 6 4 7 4 8 3 9 4 dict b OrderedDict 1 4 2 2 3 1 4 4 5
  • 在音频分析中绘制频谱图

    我正在研究使用神经网络的语音识别 为此 我需要获取这些训练音频文件 wav 的频谱图 如何在 python 中获取这些频谱图 有很多方法可以做到这一点 最简单的方法是查看中提出的方法关于 Kaggle 竞赛TensorFlow 语音识别挑战
  • 按值对多维哈希进行排序并打印最高的

    我有一个存储的多维哈希 info 具有以下结构 info os id length foreach os keys info foreach id keys info os print os id gt info os id n if ke
  • 常量截断为整数

    下面的GO程序会报错 fft go 13 constant 6 28319 truncated to integer fft go 13 cannot use 7 k N type int as type float64 in assign
  • 如何在 php 中创建我的网站的日志文件? [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我想知道如何用 php 创建我的网站
  • UICollectionView 对陈旧数据的断言错误

    在尝试从我的集合视图中卸载一批图像 然后用另一批图像替换它们的过程中 我遇到了一个错误 其中 根据原始图像组或后续图像组是大于还是小于预期的替换图像 发生断言错误 表示 Assertion failure in UICollectionVi
  • CUDA 中的全局内存与共享内存

    我有两个 CUDA 内核可以计算类似的东西 一种是使用全局内存 myfun是一个设备函数 它从全局内存中读取大量数据并进行计算 第二个内核将该数据块从全局内存传输到共享内存 以便数据可以在块的不同线程之间共享 我使用全局内存的内核比使用共享
  • 从精明的边缘获取边界并删除图像的背景

    我正在尝试删除我正在尝试训练神经网络的图像的背景 我使用此处描述的方法运气不佳 如何从此类图像中删除背景 https stackoverflow com questions 29313667 how do i remove the back
  • 如何在 Bootstrap 模态中显示画布

    我创建了一个地图 您可以在其中通过 Javascript 预订自行车 用户应该 1 选择一个自行车站 绿色站 可以使用自行车 2 点击一个按钮 预留按钮 3 登录画布 在模式中 页面在这里 http p4547 phpnet org bik