java实现电子签名技术_电子签名类 js类

'use strict';

/* signer.js 签名类 iger 2019-03-16 18:46:34 */

class Signer {

constructor(id) {

this.canvas = document.getElementById(id);

this.ctx = this.canvas.getContext('2d');

this.width = this.canvas.width;

this.height = this.canvas.height;

}

/**

* 入口函数

*/

run() {

var _this = this;

var canvas = this.canvas;

var ctx = this.ctx;

ctx.fillStyle = '#fff';

ctx.fillRect(0, 0, this.width, this.height);

ctx.lineWidth = 1;

ctx.shadowBlur = 1;

ctx.lineCap = "round";

ctx.lineJoin = 'round';

ctx.save();

canvas.addEventListener('touchstart', function (e) {

_this.touchStart(e);

});

return this;

}

/**

* 手指接触的画布

* @param {事件对象} e

*/

touchStart(e) {

var _this = this;

var canvas = this.canvas;

var ctx = this.ctx;

var e = e || window.event;

var touches = e.touches[0];

var x = touches.clientX-40, y = touches.clientY-(document.documentElement.offsetHeight-272)/2-52;

ctx.beginPath();

ctx.moveTo(x, y);

canvas.addEventListener('touchmove', function(e){

_this.touchMove(e);

});

canvas.addEventListener('touchmend', this.touchEnd);

}

/**

* 触控移动

* @param {事件对象} e

*/

touchMove(e) {

var ctx = this.ctx;

var e = e || window.event;

var touches = e.touches[0];

var x = touches.clientX-40, y = touches.clientY-(document.documentElement.offsetHeight-272)/2-52;

ctx.lineTo(x, y);

ctx.stroke();

}

/**

* 触控移动结束

*/

touchEnd() {

var canvas = this.canvas;

canvas.removeEventListener('touchmove', this.touchMove);

canvas.removeEventListener('touchend', this.touchEnd);

}

/**

* 清空画布

*/

clear() {

this.ctx.fillStyle = '#fff';

this.ctx.clearRect(0, 0, this.width, this.height);

this.ctx.stroke();

}

/**

* 获取png格式图片

* @param {画布对象} canvas

*/

getPNGImage(canvas = this.canvas) {

return canvas.toDataURL('image/png');

}

/**

* 获取jpg格式图片

* @param {画布对象} canvas

*/

getJPGImage(canvas = this.canvas) {

return canvas.toDataURL('image/jpeg', 0.5);

}

/**

* 下载图片

* @param {图片对象} image

*/

downloadPNGImage(image) {

const url = image.replace('image/png', 'image/octet-stream;Content-Disposition:attachment;filename=userfile.png');

window.location.href = url;

}

/**

* 转换图片为bolb格式

* @param {图片对象} dataURL

*/

dataURLtoBlob(dataURL) {

var arr = dataURL.split(','),

mime = arr[0].match(/:(.*?);/)[1],

bstr = atob(arr[1]),

n = bstr.length,

u8arr = new Uint8Array(n);

while (n--) {

u8arr[n] = bstr.charCodeAt(n);

}

return new Blob([u8arr], { type: mime });

}

/**

* 上传图片

* @param {Bolb对象} blob

* @param {上传地址} url

* @param {上传成功回调函数} success

* @param {上传失败回调函数} failure

*/

upload(blob, url, success, failure) {

const xhr = new XMLHttpRequest();

xhr.withCredentials = true;

xhr.open('POST', url, true);

xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

xhr.send(blob);

xhr.onload = () => {

if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {

success( JSON.parse(xhr.responseText) );

} else {

failure();

}

};

xhr.onerror = (e) => {

failure(e);

};

}

}

export default Signer;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值