1

签名

需要一个 jquery 插件或任何其他可以帮助我绘制类似类型签名的插件。谁能告诉我在哪里可以找到这样的插件?

4

2 回答 2

4

使用MDN可以实现更平滑自然的“曲线”线context.quadraticCurveTo()

在此处输入图像描述

只是,quadraticCurveTo 只能有一个恒定的厚度,所以,通过使用下面的你不能轻易地改变线来制作阴影笔角和压力效果。

var board = {
  width: 560,
  height: 190
};
var pen = {
  color: "rgb(0, 0, 0)",
  size: 2
};
var pts = [];
var isDown = false;
var isTouch = false;
var cvs = document.getElementById('canvas');
var ctx = cvs.getContext('2d');
var cvs2 = document.createElement('canvas');
var ctx2 = cvs2.getContext('2d');

cvs.width = cvs2.width = board.width;
cvs.height = cvs2.height = board.height;

function penDown(ev) {
  ev.preventDefault();
  isTouch = ev.type === "touchstart";
  ev = isTouch ? ev.touches[0] : ev;
  isDown = true;
  pts.push({
    x: ev.clientX,
    y: ev.clientY
  });
  drawPoints();
}

function penMove(ev) {
  ev.preventDefault();
  ev = isTouch ? ev.touches[0] : ev;
  if (isDown) {
    ctx.clearRect(0, 0, board.width, board.height);
    ctx.drawImage(cvs2, 0, 0); // Draw to inmemory cvs2
    pts.push({
      x: ev.clientX,
      y: ev.clientY
    });
    drawPoints();
  }
}

function penUp(ev) {
  ev.preventDefault();
  isDown = isTouch = false;
  pts = [];
  // Save state to in-memory cvs2
  ctx2.clearRect(0, 0, board.width, board.height);
  ctx2.drawImage(cvs, 0, 0);
}

function clear() {
  ctx.clearRect(0, 0, board.width, board.height);
  ctx2.clearRect(0, 0, board.width, board.height);
}

function drawPoints() {
  var i = 0;
  var i2 = pts.length > 1 ? 1 : 0;
  ctx.beginPath();
  ctx.lineWidth = pen.size;
  ctx.lineJoin = 'round';
  ctx.lineCap = 'round';
  ctx.moveTo(pts[0].x, pts[0].y);
  for (; i < pts.length - i2; i++) {
    ctx.quadraticCurveTo(
      pts[i].x,
      pts[i].y,
      (pts[i].x + pts[i + i2].x) / 2,
      (pts[i].y + pts[i + i2].y) / 2
    );
  }
  ctx.strokeStyle = pen.color;
  ctx.stroke();
  ctx.closePath();
}

// EVENTS

cvs.addEventListener('touchstart', penDown);
cvs.addEventListener('touchmove', penMove);
cvs.addEventListener('touchend', penUp);
cvs.addEventListener('mousedown', penDown);
cvs.addEventListener('mousemove', penMove);
cvs.addEventListener('mouseup', penUp);
document.getElementById("clear").addEventListener("click", clear);
body {margin:0;}
#canvas{display: block; box-shadow: inset 0 0 0 2px #ccc;}
#clear {position: absolute; top:0; left: 0;}
<canvas id='canvas'></canvas>
<button id="clear">CLEAR</button>

于 2012-11-27T12:52:08.707 回答
2

这是您正在寻找的 jquery 插件。

结帐 JSignature

下载 jsignature 插件并添加到您的页面。

于 2012-11-27T12:16:53.250 回答