html画布签名,移动端画布签名.html

h5 demo

clear

var canvas = document.getElementById('canvas');

canvas.addEventListener('mousemove', onMouseMove, false);

canvas.addEventListener('mousedown', onMouseDown, false);

canvas.addEventListener('mouseup', onMouseUp, false);

canvas.addEventListener('touchstart', onMouseDown, false);

canvas.addEventListener('touchmove', onMouseMove, false);

canvas.addEventListener('touchend', onMouseUp, false)

canvas.height = 500;

canvas.width = getWidth() - 25;

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

ctx.lineWidth = 3.0; // 设置线宽

ctx.strokeStyle = "#fc1741"; // 设置线的颜色

var flag = false;

function onMouseMove(evt) {

evt.preventDefault();

if (flag) {

var p = pos(evt);

ctx.lineTo(p.x, p.y);

ctx.lineWidth = 6.0; // 设置线宽

ctx.shadowColor = "#fc1741";

ctx.shadowBlur = 1;

//ctx.shadowOffsetX = 6;

ctx.stroke();

}

}

function onMouseDown(evt) {

evt.preventDefault();

ctx.beginPath();

var p = pos(evt);

ctx.moveTo(p.x, p.y);

flag = true;

}

function onMouseUp(evt) {

evt.preventDefault();

flag = false;

}

var clear = document.getElementById('c');

clear.addEventListener('click', function () {

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

}, false);

function pos(event) {

var x, y;

if (isTouch(event)) {

x = event.touches[0].pageX;

y = event.touches[0].pageY;

} else {

x = event.layerX;

y = event.layerY;

}

return {

x: x,

y: y

};

}

function isTouch(event) {

var type = event.type;

if (type.indexOf('touch') >= 0) {

return true;

} else {

return false;

}

}

function getWidth() {

var xWidth = null;

if (window.innerWidth !== null) {

xWidth = window.innerWidth;

} else {

xWidth = document.body.clientWidth;

}

return xWidth;

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值