惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
U
Unit 42
WordPress大学
WordPress大学
Y
Y Combinator Blog
罗磊的独立博客
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
博客园 - 叶小钗
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
V
V2EX
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - jiduoduo

POST 方法是否能提交 @RequestParam的使用 Content-Type 是 HTTP 请求 / 响应头中核心的字段 mvn clean install -U Maven 不建议 利用 systemPath 引用本地文件jar ollama本地大模型的安装与使用 字符串 md5在线生成的小工具 如何使用剪映加入字幕 Navicat Premium Lite 17 免费的工具 nginx配置支持websocket协议wss nginx配制微信公众号业务域名,重启服务等,都不行 windows 新建bat来显示ip,并且不关闭 类型“any”的参数不能赋给类型“never”的参数。 mac code google chrome cpu占用大 风扇就开始转 springboot连接sqlserver2008r2 驱动版本问题。 vue3项目打包的时候报错'openBlock' is not exported by node_modules/... mac 截屏到指定的目录 系统里的查询奇慢无比,但是sqlserver中只要6s,在软件中要40几秒。 c++结构体、共用体(联合体) 无法打开“fsevents.node”,因为无法验证开发者。
vue3 优化ai生成的手写签名
jiduoduo · 2024-04-20 · via 博客园 - jiduoduo

下面是baidu ai生成的代码:

在Vue 3中实现手写签名功能,可以使用canvas元素来创建一个绘图区域,并监听鼠标事件来实现签名的记录。以下是一个简单的例子:

vue
<template>
  <div>
    <canvas
      ref="signatureCanvas"
      @mousedown="startSigning"
      @mousemove="updateSigning"
      @mouseup="endSigning"
      width="400"
      height="200"
      style="border: 1px solid #000;"
    ></canvas>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const signatureCanvas = ref(null);
const ctx = ref(null);

const startSigning = (event) => {
  event.preventDefault();
  ctx.value.beginPath();
  const pos = getCoordinates(event);
  ctx.value.moveTo(pos.x, pos.y);
  ctx.value.lineWidth = 2;
  ctx.value.lineCap = 'round';
  ctx.value.strokeStyle = '#000';
};

const updateSigning = (event) => {
  event.preventDefault();
  if (ctx.value) {
    const pos = getCoordinates(event);
    ctx.value.lineTo(pos.x, pos.y);
    ctx.value.stroke();
  }
};

const endSigning = () => {
  ctx.value.closePath();
};

const getCoordinates = (event) => {
  const rect = signatureCanvas.value.getBoundingClientRect();
  return { x: event.clientX - rect.left, y: event.clientY - rect.top };
};

onMounted(() => {
  ctx.value = signatureCanvas.value.getContext('2d');
});
</script>
在这个例子中,我们创建了一个canvas元素,并在组件挂载时获取其上下文。然后,我们定义了三个处理鼠标事件的函数:startSigning、updateSigning和endSigning。startSigning函数开始记录路径,updateSigning函数在鼠标移动时继续记录路径,而endSigning函数结束当前路径。getCoordinates函数用于获取正确的坐标,以便在canvas上绘制。

上面的代码,基本上是对的,但是在具体的环境中,有二个问题:

1、运行起来就是连笔画,一直不会停。

2、会直接报错:null根本就获取不到context

ctx.value = signatureCanvas.value.getContext('2d'); 

因为在

onMounted里这个ref还是没有值的情况。
 

以上的二个问题解决

1)定义 const isDrawing = ref(false);

mousedown的时候true

mouseup的时候false

2)watch中来监听这个值:然后取值:

  watch(signatureCanvas, (newValue, oldValue) => {
    ctx.value = signatureCanvas.value.getContext('2d');
  });

最后上一个开源的:

https://github.com/JaimeCheng/vue-esign