// 加载画布
$(function(){
$("body").append(
'<div id="canvasParcel" style="position: absolute;top: 25%;left:25%;cursor:default;z-index:999999999">'+
'<canvas id="canvas" width="800px" height="300px"></canvas>'+
'<button onclick="exportSignature(this)">确定</button>'+
'<button onclick="empty(this)">清空</button>'+
'</div>'
);
}
);
var canvaDom = document.getElementById("canvas"); // 画布对象
var context = canvaDom.getContext("2d"); // 画板的上下文
var canvasX = canvaDom.getBoundingClientRect().left;// 画板的坐标x
var canvasY = canvaDom.getBoundingClientRect().top; // 画板的坐标y
context.fillStyle = "black"; // 画布背景色
context.lineWidth = 4; // 线的宽度
context.strokeStyle = "white"; // 线的颜色
context.fillRect(0, 0, canvaDom.width, canvaDom.height);// 画板的范围
canvaDom.addEventListener("mousedown", down, false);// 鼠标按下去的事件
canvaDom.addEventListener("mousemove", draw, false);// 鼠标移动事件
canvaDom.addEventListener("mouseup", up, false); // 鼠标松开事件
var onoff = false; // 锁定开关
var oldx = canvasX; // 起始坐标x
var oldy = canvasY; // 起始坐标y
var newx; // 结束坐标x
var newy; // 结束坐标y
// 鼠标按下
function down(event) {
onoff = true; // 打开开关
oldx = event.clientX -canvasX; // 鼠标在画板中点击的X的坐标
oldy = event.clientY -canvasY; // 鼠标在画板中点击的Y的坐标
context.beginPath(); // 开始路径
}
// 鼠标移动
function draw(event) {
// 开关
if (onoff) {
newx = event.clientX-canvasX;
newy = event.clientY-canvasY;
context.moveTo(oldx, oldy); // 线的起点坐标
context.lineTo(newx, newy); // 线的始点坐标
context.stroke(); // 初始化到画布中
oldx = newx;
oldy = newy;
}
}
// 鼠标离开
function up() {
onoff = false; // 关闭开关
context.closePath();// 关闭路径
}
// 清空
function empty() {
context.clearRect(0,0,canvaDom.width,canvaDom.height); // 清空的范围
context.fillRect(0, 0, canvaDom.width, canvaDom.height);// 重设的范围
}
//导出信息
function exportSignature() {
// 作者标记
context.font = "20px 宋体";
context.fillStyle = 'white';
context.fillText('草先生'+new Date().toLocaleString(), 5, canvaDom.height-5);
var exportImg = canvas.toDataURL("image/jpeg",0.1);// 0.1代表压缩的比率,默认为1
console.info(exportImg);
}
js实现鼠标签名,不用任何插件,超值
最新推荐文章于 2024-05-10 09:16:43 发布