首页 > 其他分享 >js---手绘签名

js---手绘签名

时间:2023-12-05 15:33:09浏览次数:28  
标签:function canvas point js --- addEventListener var 手绘 event

最近在做项目,在做电子签章的时候,需要用到电子签名,下面是用JS进行手绘签名:

<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>手写签名</title>
<style type="text/css">
*{margin: 0; padding: 0;}
.canvas-box{width: 500px; margin: 50px auto;}
#canvas{width: 500px; height: 200px; border: 1px solid red;}
#qmimg{border: 1px solid red;}
</style>
</head>
<body>

  <div class="canvas-box">
    <button id="btn_submit">提交</button>
    <button id="btn_clear">清除</button>
    <br><br>
    <div id="canvasDiv" ></div>
    <br><br>
    <img id="qmimg" />
  </div>

  <script type="text/javascript">

  function onDocumentTouchStart( event ) {
    if( event.touches.length == 1 ) {
      event.preventDefault();
      var now = new Date().getTime();
      if ( now - timeOfLastTouch  < 250 ) {
        reset();
        return;
      }
      timeOfLastTouch = now;
      mouseX = event.touches[ 0 ].pageX;
      mouseY = event.touches[ 0 ].pageY;
      isMouseDown = true;
    }
  }

  function onDocumentTouchMove( event ) {
    if ( event.touches.length == 1 ) {
      event.preventDefault();
      mouseX = event.touches[ 0 ].pageX;
      mouseY = event.touches[ 0 ].pageY;
    }
  }

  function onDocumentTouchEnd( event ) {
    if ( event.touches.length == 0 ) {
      event.preventDefault();
      isMouseDown = false;
    }
  }

  var canvasDiv = document.getElementById('canvasDiv');
  var canvas = document.createElement('canvas');
  var canvasWidth = 500;
  var canvasHeight = 200;
  document.addEventListener( 'touchmove', onDocumentTouchMove, false); 

  var point = {};
  point.notFirst = false;

  canvas.setAttribute('width', canvasWidth);
  canvas.setAttribute('height', canvasHeight);
  canvas.setAttribute('id', 'canvas');
  canvasDiv.appendChild(canvas);
  if(typeof G_vmlCanvasManager != 'undefined') {
    canvas = G_vmlCanvasManager.initElement(canvas);
  }
  var context = canvas.getContext("2d");

  canvas.addEventListener("touchstart", function(e){
    var mouseX = e.pageX - this.offsetLeft;
    var mouseY = e.pageY - this.offsetTop;
    paint = true;
    addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
    redraw();
  });

  canvas.addEventListener("touchend", function(e){
    paint = false;
  });

  canvas.addEventListener("touchmove", function(e){
   if(paint){
      addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
      redraw();
    }
  });

  canvas.addEventListener("mousedown", function(e){
    var mouseX = e.pageX - this.offsetLeft;
    var mouseY = e.pageY - this.offsetTop;
    paint = true;
    addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
    redraw();
  });

  canvas.addEventListener("mousemove", function(e){
    if(paint){
      addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
      redraw();
    }
  });

  canvas.addEventListener("mouseup", function(e){
    paint = false;
  });

  canvas.addEventListener("mouseleave", function(e){
    paint = false;
  });

  var clickX = new Array();
  var clickY = new Array();
  var clickDrag = new Array();
  var paint;

  function addClick(x, y, dragging){
    clickX.push(x);
    clickY.push(y);
    clickDrag.push(dragging);
  }

  function redraw(){
    //canvas.width = canvas.width; // Clears the canvas
    context.strokeStyle = "#df4b26";
    context.lineJoin = "round";
    context.lineWidth = 2;
    while (clickX.length > 0 ) {
      point.bx = point.x;
      point.by = point.y;
      point.x = clickX.pop();
      point.y = clickY.pop();
      point.drag = clickDrag.pop();
      context.beginPath();
      if (point.drag && point.notFirst) {
        context.moveTo(point.bx, point.by);
      } else {
        point.notFirst = true;
        context.moveTo(point.x - 1, point.y);
      }
      context.lineTo(point.x, point.y);
      context.closePath();
      context.stroke();
    }
  }

  var clear = document.getElementById("btn_clear");
  var submit = document.getElementById("btn_submit");

  clear.addEventListener("click", function(){
    canvas.width = canvas.width;
  });

  submit.addEventListener("click", function(){
    document.getElementById("qmimg").setAttribute('src',canvas.toDataURL("image/png"));
  });

  </script>
</body>
</html>

打完收工!

标签:function,canvas,point,js,---,addEventListener,var,手绘,event
From: https://www.cnblogs.com/e0yu/p/17877393.html

相关文章

  • java字符串 加上n个"|--",与过滤处理
    /******original,左边扩充n个"-"*@paramn*@paramoriginal*@return*/privateStringfullStr(intn,Stringoriginal){StringBuildersb=newStringBuilder();for(inti=0;i<n;i++){......
  • start-dfs.sh启动hadoop,jps没显示
    查看当前系统的名称[root@masterdfs]#cat/etc/hosts192.168.128.78hadoop01查看core-site.xml<property><name>fs.defaultFS</name><value>hdfs://hadoop01:9000</value></property>删除文件夹#先停止ha......
  • Spring Boot2 开启系统日志(2)- 在application中配置日志
    在application.yml中通过logging.level来配置root是所有日志级别的打印;sql、web是分别设置sql或web相关的日志级别2.另外还可以分别对不同的包设置不同的日志打印级别#Logconfigurationlogging:level:com.*:debugorg.springframework:warn如上所示,分别......
  • js---生成印章
    最近在做电子合同的功能,需要用到电子印章,那么如何通过js生成印章呢?下面是具体的代码:<!doctypehtml><htmllang="en"><head><metacharset="UTF-8"><title>Document</title></head><body><canvasid="canvas"width......
  • quickjs模块封装、类封装
    一、模块封装、类封装通过quickjs,封装JS模块,然后可以通过import导入对应模块,还可以导入具体的类。全部代码如下。1#include"quickjs-libc.h"2#include<stdio.h>3#include<inttypes.h>4#include<string.h>5#include"cutils.h"67staticJSC......
  • Langchain-Chatchat+Qwen实现本地知识库
    1.基础介绍Langchain-Chatchat一种利用 langchain 思想实现的基于本地知识库的问答应用,目标期望建立一套对中文场景与开源模型支持友好、可离线运行的知识库问答解决方案。大致过程包括加载文件->读取文本->文本分割->文本向量化->问句向量化->在文本向量中匹配出......
  • Linux命令-sz
    1.sz介绍linux命令sz是用来把文件从Linux平台下载到Windows上2.sz用法sz[参数]file3.实例3.1.下载文件,相同文件名,进行覆盖命令:sz-bya.txt[root@centos79-3~]#sz-bya.txt--把a.txt下载到windows平台[root@centos79-3~]#3.2.下载文件,相同文件名,......
  • JS(JavaScript)
    1.介绍:JavaScript语言主要是完成页面的数据验证,因此运行在客户端,需运用浏览器来解析执行JavaScript代码。ECMAScript是JS的一个标准。 一个完整的JS是由ECMAScript,DOM(文档对象模型),Bom(浏览器对象模型)组成的。 JS是弱类型(类型可变)Java是强类型。......
  • 汇编-or逻辑或
     OR指令在两个操作数的对应位之间进行(按位)逻辑或(OR)操作,并将结果存放在目标操作数中:格式:ORreg,regORreg,memORreg,immORmem,regORmem,imm操作数可以是8位、16位、32位和64位,但是两个操作数必须是同样大小。对两个操作数的每一对对应位而言,只要有一个输入位是1,则......
  • Spring Boot2 开启系统日志(1)- 了解日志
    1.日志显示在SpringBoot应用中,日志会默认输出到控制台,日志格式如下:2023-12-0413:37:22.518INFO65136---[main]com.x.Application:StartedApplicationin2.192seconds(JVMrunningfor3.957)|----1--------------|-2-|-3-|-4......