JavaScript to implement mobile signature function

JavaScript to implement mobile signature function

This article shares the specific code of JavaScript to implement the mobile signature function for your reference. The specific content is as follows

1. HTML part

<div class="mui-content-padded">
 <div class="mui-inline"><font style="font-family: '微软雅黑';font-size: 1.2rem;">Signature of the acceptor:</font></div>
</div>
<div class="mui-content-canvasDiv" style="overflow: hidden;">
 <canvas id="myCanvas" width="660" height="360" style="border:1px solid #f2f2f2;"></canvas>
 <div class="saveimgs" id="saveImgDiv"></div>
</div>

myCanvas is the signed div, saveImgDiv is the div that is echoed after saving.

2. Page initialization, add div signature function

InitThis();
var mousePressed = false;
var lastX, lastY;
var ctx = document.getElementById('myCanvas').getContext("2d");
var c = document.getElementById("myCanvas");
var selected1, selected2;
function InitThis() {
 // Touch screen c.addEventListener('touchstart', function(event) {
  console.log(1)
  if (event.targetTouches.length == 1) {
   event.preventDefault(); // Prevent browser default events, important mousePressed = true;
   Draw(event.touches[0].pageX - this.offsetLeft, event.touches[0].pageY - this.offsetTop, false);
  }
 }, false);
 c.addEventListener('touchmove', function(event) {
  console.log(2)
  if (event.targetTouches.length == 1) {
   event.preventDefault(); // Prevent browser default events, important if(mousePressed) { 
    Draw(event.touches[0].pageX - this.offsetLeft, event.touches[0].pageY - this.offsetTop, true);
 
   }
  }
 }, false);
 c.addEventListener('touchend', function(event) {
  console.log(3)
  if (event.targetTouches.length == 1) {
   event.preventDefault(); // Prevent browser default events to prevent screen dragging during handwriting, important mousePressed = false;
  }
 }, false);
 // Mouse c.onmousedown = function(event) {
  mousePressed = true;
  Draw(event.pageX - this.offsetLeft, event.pageY - this.offsetTop, false);
 };
 c.onmousemove = function(event) {
  if(mousePressed) {
   Draw(event.pageX - this.offsetLeft, event.pageY - this.offsetTop, true);
  }
 };
 c.onmouseup = function(event) {
  mousePressed = false;
 };
}
 
function Draw(x, y, isDown) {
 if(isDown) {
  ctx.beginPath();
  ctx.strokeStyle = selected2;
  ctx.lineWidth = selected1;
  ctx.lineJoin = "round";
  ctx.moveTo(lastX, lastY);
  ctx.lineTo(x, y);
  ctx.closePath();
  ctx.stroke();
 }
 lastX = x;
 lastY = y;
}

3. Get the image path and put it in saveImgDiv, signature echo logic

var file = "http://10.1.31.173:8097/upload/" + iv[0].zjqm + "?v=" + new Date().getTime();
 
$("#saveImgDiv").append('<img src="'+ file + '" style="background:white" width="660" height="360">');

4. Save the user signature, which can be placed in the callback of saving successful submission

var saveimgs = document.getElementsByClassName("saveimgs")[0];
 
//Save signature image var image = c.toDataURL("image/png");
var ctximg = document.createElement("span");
ctximg.innerHTML = "<img src='" + image + "' alt='from canvas'/>";
if (saveimgs.getElementsByTagName('span').length >= 1) {
 var span_old = saveimgs.getElementsByTagName("span")[0];
 saveimgs.replaceChild(ctximg,span_old)
} else {
 saveimgs.appendChild(ctximg);
}

Effect

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JS canvas realizes the functions of drawing board and signature board

<<:  Robots.txt detailed introduction

>>:  Beginners learn some HTML tags (2)

Recommend

Implementation of MYSQL (telephone number, ID card) data desensitization

1. Data desensitization explanation In daily deve...

Sample code using vue-router in html

Introducing vue and vue-router <script src=&qu...

Tomcat Server Getting Started Super Detailed Tutorial

Table of contents 1. Some concepts of Tomcat –1, ...

A brief analysis of how to set the initial value of Linux root

Ubuntu does not allow root login by default, so t...

Detailed process of configuring NIS in Centos7

Table of contents principle Network environment p...

Detailed explanation of MySQL database triggers

Table of contents 1 Introduction 2 Trigger Introd...

Teach you how to build Tencent Cloud Server (graphic tutorial)

This article was originally written by blogger We...

JS function call, apply and bind super detailed method

Table of contents JS function call, apply and bin...

Hover zoom effect made with CSS3

Result:Implementation code: html <link href=&#...

Detailed explanation of the use of Vue.js render function

Vue recommends using templates to create your HTM...

How to install PHP7 Redis extension on CentOS7

Introduction In the previous article, we installe...

Summary of Form Design Techniques in Web Design

“Inputs should be divided into logical groups so ...